Privatise key & value; add protected setters (fields were protected)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1477599 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2013-04-30 12:44:12 +00:00
parent 63252b4946
commit 5ce18d6487
3 changed files with 21 additions and 12 deletions

View File

@ -28,9 +28,9 @@ import org.apache.commons.collections4.KeyValue;
public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
/** The key */
protected K key;
private K key;
/** The value */
protected V value;
private V value;
/**
* Constructs a new pair with the specified key and given value.
@ -53,6 +53,12 @@ public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
return key;
}
protected K setKey(K key) {
final K old = this.key;
this.key = key;
return old;
}
/**
* Gets the value from the pair.
*
@ -62,6 +68,12 @@ public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
return value;
}
protected V setValue(V value) {
final V old = this.value;
this.value = value;
return old;
}
/**
* Gets a debugging String view of the pair.
*

View File

@ -39,7 +39,7 @@ public abstract class AbstractMapEntry<K, V> extends AbstractKeyValue<K, V> impl
// Map.Entry interface
//-------------------------------------------------------------------------
/**
/**
* Sets the value stored in this <code>Map.Entry</code>.
* <p>
* This <code>Map.Entry</code> is not connected to a Map, so only the
@ -48,10 +48,9 @@ public abstract class AbstractMapEntry<K, V> extends AbstractKeyValue<K, V> impl
* @param value the new value
* @return the previous value
*/
@Override
public V setValue(final V value) {
final V answer = this.value;
this.value = value;
return answer;
return super.setValue(value);
}
/**

View File

@ -77,14 +77,13 @@ public class DefaultKeyValue<K, V> extends AbstractKeyValue<K, V> {
* @return the old key
* @throws IllegalArgumentException if key is this object
*/
@Override
public K setKey(final K key) {
if (key == this) {
throw new IllegalArgumentException("DefaultKeyValue may not contain itself as a key.");
}
final K old = this.key;
this.key = key;
return old;
return super.setKey(key);
}
/**
@ -94,14 +93,13 @@ public class DefaultKeyValue<K, V> extends AbstractKeyValue<K, V> {
* @param value the new value
* @throws IllegalArgumentException if value is this object
*/
@Override
public V setValue(final V value) {
if (value == this) {
throw new IllegalArgumentException("DefaultKeyValue may not contain itself as a value.");
}
final V old = this.value;
this.value = value;
return old;
return super.setValue(value);
}
//-----------------------------------------------------------------------