Refactored out small utility class, feel free to use it when you need immutable kvp's for whatever reason.

This commit is contained in:
Robin Eklund 2011-07-13 15:52:25 +02:00
parent ef88a11790
commit 1349534007
3 changed files with 102 additions and 39 deletions
src
main/java/se/su/dsv/scipro
test/java/se/su/dsv/scipro/util

@ -1,6 +1,5 @@
package se.su.dsv.scipro.admin.pages.settings;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
@ -14,6 +13,7 @@ import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import se.su.dsv.scipro.admin.pages.AbstractAdminSettingsPage;
import se.su.dsv.scipro.util.KeyValuePair;
public class AdminServerEnvironmentSettingsPage extends AbstractAdminSettingsPage {
public AdminServerEnvironmentSettingsPage(final PageParameters pp) {
@ -28,44 +28,6 @@ public class AdminServerEnvironmentSettingsPage extends AbstractAdminSettingsPag
}
});
}
private final class KeyValuePair<V> implements Serializable{
private static final long serialVersionUID = 1L;
private final String key;
private final V value;
public KeyValuePair(final String key, final V value){
this.key = key;
this.value = value;
}
public String getKey(){
return key;
}
public V getValue(){
return value;
}
@Override
public boolean equals(final Object o){
if(o==this)
return true;
if(!(o instanceof KeyValuePair))
return false;
@SuppressWarnings("unchecked")
final KeyValuePair<V> other = (KeyValuePair<V>)o;
return ((key==null?other.key==null:key.equals(other.key)) &&
(value==null?other.value==null:value.equals(other.value)));
}
@Override
public int hashCode(){
final int w = 31;
int result = 17;//Why hello there Mr Bloch, how are we feeling today ?
result = w * result + (key==null?0:key.hashCode());
result = w * result + (value==null?0:value.hashCode());
return result;
}
@Override
public String toString(){
return ("{"+key+":"+value+"}");
}
}
private List<KeyValuePair<String>> getRequestAttributes(){
final HttpServletRequest rawRequest = getWebRequestCycle().getWebRequest().getHttpServletRequest();
List<KeyValuePair<String>> list = new ArrayList<KeyValuePair<String>>();

@ -0,0 +1,79 @@
package se.su.dsv.scipro.util;
import java.io.Serializable;
/**
* Small utility class for immutable (yes it's immutable and no it's never going to be otherwise) key/value pairs.
* The "value" field is parameterized, but the key is always intended to be a simple String.
* The Serializable properties of this depends on those of the parameterized type.
* Typical usage:
* <code>
* KeyValuePair<SomeType> pair = new KeyValuePair<SomeType>("thisIsAKey",someObject);
* pair.getKey();
* pair.getValue();
* </code>
* @param <V>
*/
public final class KeyValuePair<V> implements Serializable{
private static final long serialVersionUID = 1L;
private final String key;
private final V value;
/**
* Default and only constructor, provide key and value.
* null values are accepted for both key and value, and should be safe to use (yet nonsensical).
* @param key
* @param value
*/
public KeyValuePair(final String key, final V value){
this.key = key;
this.value = value;
}
/**
* Getter for wrapped key String
* @return The key
*/
public String getKey(){
return key;
}
/**
* Getter for wrapped Value
* @return The value
*/
public V getValue(){
return value;
}
/**
* Equals override, behaves in a logical fashion (two objects are equal if both key and value matches).
* Null values are accounted for, ie: two objects with null key+value are considered equal.
*/
@Override
public boolean equals(final Object o){
if(o==this)
return true;
if(!(o instanceof KeyValuePair))
return false;
@SuppressWarnings("unchecked")
final KeyValuePair<V> other = (KeyValuePair<V>)o;
return ((key==null?other.key==null:key.equals(other.key)) &&
(value==null?other.value==null:value.equals(other.value)));
}
/**
* Hash-code override, uses both components in a standard fashion.
*/
@Override
public int hashCode(){
final int w = 31;
int result = 17;//Why hello there Mr Bloch, how are we feeling today ?
result = w * result + (key==null?0:key.hashCode());
result = w * result + (value==null?0:value.hashCode());
return result;
}
/**
* Standard override, return String format not exposed and subject to change.
* Any parsing of this representation is strongly advised against and done at your own risk.
*/
@Override
public String toString(){
return ("{"+key+":"+value+"}");
}
}

@ -0,0 +1,22 @@
package se.su.dsv.scipro.util;
import org.junit.Assert;
import org.junit.Test;
public class TestKeyValuePair {
@Test
public void testEqualsOperations() {
KeyValuePair<String> kvpOne = new KeyValuePair<String>("key","value");
KeyValuePair<String> kvpTwo = new KeyValuePair<String>("key","value");
KeyValuePair<String> kvpThree = new KeyValuePair<String>("key3","value");
KeyValuePair<String> kvpFour = new KeyValuePair<String>(null,null);
KeyValuePair<TestKeyValuePair> kvpFive = new KeyValuePair<TestKeyValuePair>("key",this);
Assert.assertTrue(kvpOne.equals(kvpOne));
Assert.assertTrue(kvpFour.equals(kvpFour));
Assert.assertTrue(kvpOne.equals(kvpTwo) && kvpTwo.equals(kvpOne));
Assert.assertFalse(kvpOne.equals(kvpThree) && kvpThree.equals(kvpOne));
Assert.assertFalse(kvpFour.equals(kvpThree) && kvpThree.equals(kvpFour));
Assert.assertFalse(kvpOne.equals(kvpFive) && kvpFive.equals(kvpOne));
}
}