Skip to content

Commit

Permalink
SimpleEntry enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heromyth committed May 17, 2021
1 parent c03297b commit ec7a37f
Showing 1 changed file with 152 additions and 137 deletions.
289 changes: 152 additions & 137 deletions source/hunt/collection/AbstractMap.d
Original file line number Diff line number Diff line change
Expand Up @@ -494,143 +494,6 @@ abstract class AbstractMap(K, V) : Map!(K, V) {
// return o1 is null ? o2 is null : o1.equals(o2);
// }

// Implementation Note: SimpleEntry and SimpleImmutableEntry
// are distinct unrelated classes, even though they share
// some code. Since you can't add or subtract final-ness
// of a field in a subclass, they can't share representations,
// and the amount of duplicated code is too small to warrant
// exposing a common abstract class.

/**
* An Entry maintaining a key and a value. The value may be
* changed using the <tt>setValue</tt> method. This class
* facilitates the process of building custom map
* implementations. For example, it may be convenient to return
* arrays of <tt>SimpleEntry</tt> instances in method
* <tt>Map.entrySet().toArray</tt>.
*
*/
// static class SimpleEntry!(K,V)
// implements Entry!(K,V), java.io.Serializable
// {
// private static final long serialVersionUID = -8499721149061103585L;

// private final K key;
// private V value;

// /**
// * Creates an entry representing a mapping from the specified
// * key to the specified value.
// *
// * @param key the key represented by this entry
// * @param value the value represented by this entry
// */
// SimpleEntry(K key, V value) {
// this.key = key;
// this.value = value;
// }

// /**
// * Creates an entry representing the same mapping as the
// * specified entry.
// *
// * @param entry the entry to copy
// */
// SimpleEntry(Entry!(K, V) entry) {
// this.key = entry.getKey();
// this.value = entry.getValue();
// }

// /**
// * Returns the key corresponding to this entry.
// *
// * @return the key corresponding to this entry
// */
// K getKey() {
// return key;
// }

// /**
// * Returns the value corresponding to this entry.
// *
// * @return the value corresponding to this entry
// */
// V getValue() {
// return value;
// }

// /**
// * Replaces the value corresponding to this entry with the specified
// * value.
// *
// * @param value new value to be stored in this entry
// * @return the old value corresponding to the entry
// */
// V setValue(V value) {
// V oldValue = this.value;
// this.value = value;
// return oldValue;
// }

// /**
// * Compares the specified object with this entry for equality.
// * Returns {@code true} if the given object is also a map entry and
// * the two entries represent the same mapping. More formally, two
// * entries {@code e1} and {@code e2} represent the same mapping
// * if<pre>
// * (e1.getKey()==null ?
// * e2.getKey()==null :
// * e1.getKey().equals(e2.getKey()))
// * &amp;&amp;
// * (e1.getValue()==null ?
// * e2.getValue()==null :
// * e1.getValue().equals(e2.getValue()))</pre>
// * This ensures that the {@code equals} method works properly across
// * different implementations of the {@code MapEntry} interface.
// *
// * @param o object to be compared for equality with this map entry
// * @return {@code true} if the specified object is equal to this map
// * entry
// * @see #toHash
// */
// bool equals(Object o) {
// if (!(o instanceof MapEntry))
// return false;
// MapEntry<?,?> e = (MapEntry<?,?>)o;
// return eq(key, e.getKey()) && eq(value, e.getValue());
// }

// /**
// * Returns the hash code value for this map entry. The hash code
// * of a map entry {@code e} is defined to be: <pre>
// * (e.getKey()==null ? 0 : e.getKey().toHash()) ^
// * (e.getValue()==null ? 0 : e.getValue().toHash())</pre>
// * This ensures that {@code e1.equals(e2)} implies that
// * {@code e1.toHash()==e2.toHash()} for any two Entries
// * {@code e1} and {@code e2}, as required by the general
// * contract of {@link Object#toHash}.
// *
// * @return the hash code value for this map entry
// * @see #equals
// */
// size_t toHash() @trusted nothrow {
// return (key is null ? 0 : key.toHash()) ^
// (value is null ? 0 : value.toHash());
// }

// /**
// * Returns a string representation of this map entry. This
// * implementation returns the string representation of this
// * entry's key followed by the equals character ("<tt>=</tt>")
// * followed by the string representation of this entry's value.
// *
// * @return a string representation of this map entry
// */
// string toString() {
// return key ~ "=" ~ value;
// }

// }
}

/**
Expand Down Expand Up @@ -816,3 +679,155 @@ class EmptyMap(K, V) : AbstractMap!(K, V) {
// return EMPTY_MAP;
// }
}



// Implementation Note: SimpleEntry and SimpleImmutableEntry
// are distinct unrelated classes, even though they share
// some code. Since you can't add or subtract final-ness
// of a field in a subclass, they can't share representations,
// and the amount of duplicated code is too small to warrant
// exposing a common abstract class.

/**
* An Entry maintaining a key and a value. The value may be
* changed using the <tt>setValue</tt> method. This class
* facilitates the process of building custom map
* implementations. For example, it may be convenient to return
* arrays of <tt>SimpleEntry</tt> instances in method
* <tt>Map.entrySet().toArray</tt>.
*
*/
class SimpleEntry(K,V) : MapEntry!(K,V)
{

private K key;
private V value;

/**
* Creates an entry representing a mapping from the specified
* key to the specified value.
*
* @param key the key represented by this entry
* @param value the value represented by this entry
*/
this(K key, V value) {
this.key = key;
this.value = value;
}

/**
* Creates an entry representing the same mapping as the
* specified entry.
*
* @param entry the entry to copy
*/
this(MapEntry!(K, V) entry) {
this.key = entry.getKey();
this.value = entry.getValue();
}

/**
* Returns the key corresponding to this entry.
*
* @return the key corresponding to this entry
*/
K getKey() {
return key;
}

/**
* Returns the value corresponding to this entry.
*
* @return the value corresponding to this entry
*/
V getValue() {
return value;
}

/**
* Replaces the value corresponding to this entry with the specified
* value.
*
* @param value new value to be stored in this entry
* @return the old value corresponding to the entry
*/
V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}

/**
* Compares the specified object with this entry for equality.
* Returns {@code true} if the given object is also a map entry and
* the two entries represent the same mapping. More formally, two
* entries {@code e1} and {@code e2} represent the same mapping
* if<pre>
* (e1.getKey()==null ?
* e2.getKey()==null :
* e1.getKey().equals(e2.getKey()))
* &amp;&amp;
* (e1.getValue()==null ?
* e2.getValue()==null :
* e1.getValue().equals(e2.getValue()))</pre>
* This ensures that the {@code equals} method works properly across
* different implementations of the {@code MapEntry} interface.
*
* @param o object to be compared for equality with this map entry
* @return {@code true} if the specified object is equal to this map
* entry
* @see #toHash
*/
override bool opEquals(Object o) {
if (o is this)
return true;

MapEntry!(K, V) e = cast(MapEntry!(K, V))o;
if (e !is null) {
if (key == e.getKey() && value == e.getValue())
return true;
}
return false;
}

bool opEquals(IObject o) {
return opEquals(cast(Object) o);
}

int opCmp(MapEntry!(K, V) o) {
throw new NotImplementedException("opCmp");
}

alias opCmp = Object.opCmp;

/**
* Returns the hash code value for this map entry. The hash code
* of a map entry {@code e} is defined to be: <pre>
* (e.getKey()==null ? 0 : e.getKey().toHash()) ^
* (e.getValue()==null ? 0 : e.getValue().toHash())</pre>
* This ensures that {@code e1.equals(e2)} implies that
* {@code e1.toHash()==e2.toHash()} for any two Entries
* {@code e1} and {@code e2}, as required by the general
* contract of {@link Object#toHash}.
*
* @return the hash code value for this map entry
* @see #equals
*/
override size_t toHash() @trusted nothrow {
return hashOf(key) ^ hashOf(value);
}

/**
* Returns a string representation of this map entry. This
* implementation returns the string representation of this
* entry's key followed by the equals character ("<tt>=</tt>")
* followed by the string representation of this entry's value.
*
* @return a string representation of this map entry
*/
override string toString() {
return key.to!string() ~ "=" ~ value.to!string();
}

}

0 comments on commit ec7a37f

Please sign in to comment.