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> { public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
/** The key */ /** The key */
protected K key; private K key;
/** The value */ /** The value */
protected V value; private V value;
/** /**
* Constructs a new pair with the specified key and given 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; return key;
} }
protected K setKey(K key) {
final K old = this.key;
this.key = key;
return old;
}
/** /**
* Gets the value from the pair. * Gets the value from the pair.
* *
@ -62,6 +68,12 @@ public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
return value; 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. * 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 // Map.Entry interface
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
/** /**
* Sets the value stored in this <code>Map.Entry</code>. * Sets the value stored in this <code>Map.Entry</code>.
* <p> * <p>
* This <code>Map.Entry</code> is not connected to a Map, so only the * 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 * @param value the new value
* @return the previous value * @return the previous value
*/ */
@Override
public V setValue(final V value) { public V setValue(final V value) {
final V answer = this.value; return super.setValue(value);
this.value = value;
return answer;
} }
/** /**

View File

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