Basic Generic part 2

From the old code thank you rpgkan, but it is not a generic code so flexible that we can convert it to a generic version is better.

This’s old code version

package org.my.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class OldBatchObjectModel {
	/**
	 * for key set
	 */
	private Set idSet;
	/**
	 * map of value
	 */
	private Map batchDataMap;

	public OldBatchObjectModel() {
		idSet = new HashSet();
		batchDataMap = new HashMap();
	}

	/**
	 * put key and object value
	 *
	 * @param id
	 * @param obj
	 */
	public void putData(String id, Object obj) {
		idSet.add(id);
		Object item = batchDataMap.get(id);
		if (null == item) {
			batchDataMap.put(id, obj);
		}
	}

	/**
	 * get data from id
	 *
	 * @param id
	 * @return
	 */
	public Object getData(String id) {
		return batchDataMap.get(id);
	}

	/**
	 * get all key
	 *
	 * @return
	 */
	public Set getId() {
		return idSet;
	}

	/**
	 * remove value
	 *
	 * @param id
	 */
	public void removeDataById(String id) {
		batchDataMap.remove(id);
	}

	/**
	 * remove all data
	 */
	public void clear() {
		this.clearId();
		this.clearData();
	}

	/**
	 * remove all key set
	 */
	public void clearId() {
		idSet.clear();
	}

	/**
	 * remove all map value
	 */
	public void clearData() {
		batchDataMap.clear();
	}

	public int size() {
		return batchDataMap.size();
	}

}

 

Let’s see the test code.

 

package org.my.test;

import junit.framework.Assert;

import org.junit.Test;

public class OldBathObjectModelTest {
	@Test()
	public void testAdd() {
		OldBatchObjectModel ob = new OldBatchObjectModel();
		ob.putData("1", 2);
		ob.putData("2", "3");
		ob.putData("3", 2.0);

		if (ob.getData("1") instanceof Integer) {
			int x1 = ((Integer) ob.getData("1")).intValue();
			Assert.assertEquals(2, x1);
		}
		if (ob.getData("2") instanceof String) {
			String x2 = ((String) ob.getData("2")).toString();
			Assert.assertEquals("3", x2);
		}
		if (ob.getData("3") instanceof Double) {
			double x3 = ((Double) ob.getData("3")).doubleValue();
			Assert.assertEquals(2.0, x3);
		}
		Set ids = ob.getId();
		for (Iterator iterator = ids.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			if (ob.getData(key) instanceof Integer) {
				Assert.assertTrue(ob.getData(key) instanceof Integer);
			}else if (ob.getData(key)  instanceof String) {
				Assert.assertTrue(ob.getData(key) instanceof String);
			}
			if (ob.getData(key)  instanceof Double) {
				Assert.assertTrue(ob.getData(key) instanceof Double);
			}
		}
	}
}

You will see that it’s quite a lot of casting, and it does not help me much in terms of reuse, because you will need to check before pulling up to the time that it takes a data type that you want to do it. Let’s convert it to a generic version.

 

package org.my.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class NewBatchObjectModel  {
	/**
	 * for key set
	 */
	private Set idSet;
	/**
	 * map of value
	 */
	private Map batchDataMap;

	public NewBatchObjectModel() {
		idSet = new HashSet();
		batchDataMap = new HashMap();
	}

	/**
	 * put key and object value
	 *
	 * @param id
	 * @param obj
	 */
	public void putData(K id, V obj) {
		idSet.add(id);
		Object item = batchDataMap.get(id);
		if (null == item) {
			batchDataMap.put(id, obj);
		}
	}

	/**
	 * get data from id
	 *
	 * @param id
	 * @return
	 */
	public V getData(K id) {
		return batchDataMap.get(id);
	}

	/**
	 * get all key
	 *
	 * @return
	 */
	public Set getId() {
		return idSet;
	}

	/**
	 * remove value
	 *
	 * @param id
	 */
	public void removeDataById(K id) {
		batchDataMap.remove(id);
	}

	/**
	 * remove all data
	 */
	public void clear() {
		this.clearId();
		this.clearData();
	}

	/**
	 * remove all key set
	 */
	public void clearId() {
		idSet.clear();
	}

	/**
	 * remove all map value
	 */
	public void clearData() {
		batchDataMap.clear();
	}

	public int size() {
		return batchDataMap.size();
	}
	/**
	 * contain key
	 * @param id
	 * @return
	 */
	public boolean containsKey(K id){
		return this.idSet.contains(id);
	}
}

for String

 

package org.my.test;

public class StringBatchObjectModel extends NewBatchObjectModel{

}

for Integer

 

package org.my.test;

public class IntegerBatchObjectModel extends NewBatchObjectModel{

}

 

for Double

package org.my.test;

public class DoubleBatchObjectModel extends NewBatchObjectModel{

}

 

Test,Test,Test

package org.my.test;

import org.junit.Assert;
import org.junit.Test;

public class OldBathObjectModelTest {
	@Test()
	public void testInteger() {
		IntegerBatchObjectModel ob = new IntegerBatchObjectModel();
		ob.putData("1", 2);
		int x1 = ob.getData("1");
		Assert.assertTrue(2 == x1);
		Assert.assertTrue(ob.getData("1") instanceof Integer);
		Assert.assertTrue(ob.getData("1") == 2);
	}
	@Test()
	public void testString() {
		StringBatchObjectModel ob = new StringBatchObjectModel();
		ob.putData("1", "Some test string");
		String x1 = ob.getData("1");
		Assert.assertTrue("Some test string".equals(x1));
		Assert.assertTrue(ob.getData("1") instanceof String);
		Assert.assertTrue("Some test string".equals(ob.getData("1") ));
	}
	@Test()
	public void testDouble() {
		DoubleBatchObjectModel ob = new DoubleBatchObjectModel();
		ob.putData("1", 2.01);
		double x1 = ob.getData("1");
		Assert.assertTrue(2.01 == x1);
		Assert.assertTrue(ob.getData("1") instanceof Double);
		Assert.assertTrue(ob.getData("1") == 2.01);
	}
}

 

I hope you will continue to be applied to your code. ตกหลุมรัก

, ,

  1. ให้ความเห็น

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Connecting to %s

Follow

Get every new post delivered to your Inbox.