Skip to content

Commit

Permalink
some enhancements to ERXMultiKey
Browse files Browse the repository at this point in the history
• use constant empty array for empty multi key
• keys() returns copy of keys
• keysNoCopy() for those who know what they do ;)
• equals tests first for object equality
  • Loading branch information
darkv committed Oct 9, 2012
1 parent 7842a66 commit d7e3052
Showing 1 changed file with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.webobjects.eoaccess.EOEntity;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation._NSCollectionPrimitives;

/**
* Simple class to use multiple objects as
Expand Down Expand Up @@ -40,7 +41,7 @@ private ERXMultiKey(short keyCount) {
*/
public ERXMultiKey() {
_keyCount=0;
_keys=new Object[0];
_keys=_NSCollectionPrimitives.EmptyArray;
recomputeHashCode();
}

Expand All @@ -51,7 +52,7 @@ public ERXMultiKey() {
*/
public ERXMultiKey(Object[] keys) {
this((short)keys.length);
System.arraycopy(keys,0,_keys,0,(int)_keyCount);
System.arraycopy(keys,0,_keys,0,_keyCount);
recomputeHashCode();
}

Expand All @@ -60,8 +61,8 @@ public ERXMultiKey(Object[] keys) {
* array.
* @param keys array of keys
*/
public ERXMultiKey(NSArray keys) {
this ((short)keys.count());
public ERXMultiKey(NSArray<Object> keys) {
this((short)keys.count());
for (int i=0; i<keys.count(); i++) _keys[i]=keys.objectAtIndex(i);
recomputeHashCode();
}
Expand All @@ -71,7 +72,7 @@ public ERXMultiKey(NSArray keys) {
* vector.
* @param keys vector of keys
*/
public ERXMultiKey(Vector keys) {
public ERXMultiKey(Vector<Object> keys) {
this ((short)keys.size());
for (int i=0; i<keys.size(); i++) _keys[i]=keys.elementAt(i);
recomputeHashCode();
Expand All @@ -86,25 +87,43 @@ public ERXMultiKey(Vector keys) {
public ERXMultiKey(Object key, Object ... keys) {
this((short)(keys.length + 1));
_keys[0] = key;
System.arraycopy(keys,0,_keys,1,(int)_keyCount-1);
System.arraycopy(keys,0,_keys,1,_keyCount-1);
recomputeHashCode();
}

/**
* Method used to return a copy of the object array
* of keys for the current multi-key.
* @return object array of keys
*/
public final Object[] keys() {
Object[] keys;
if (_keyCount == 0) {
keys = _keys;
} else {
keys = new Object[_keyCount];
System.arraycopy(_keys, 0, keys, 0, _keyCount);
}
return keys;
}

/**
* Method used to return the object array
* of keys for the current multi-key.<br />
* DO NOT MODIFY!
* @return object array of keys
*/
// FIXME: should return a copy
public final Object[] keys() { return _keys; }
*/
public final Object[] keysNoCopy() {
return _keys;
}

/**
* Calculates a unique hash code for
* the given array of keys.
* @return unique hash code for the array
* of keys.
*/
@Override
public final int hashCode() {
return _hashCode;
}
Expand Down Expand Up @@ -136,9 +155,12 @@ public final void recomputeHashCode() {
* @param o object to be compared
* @return result of comparison
*/
@Override
public final boolean equals(Object o) {
if (o instanceof ERXMultiKey) {
ERXMultiKey o2 = (ERXMultiKey) o;
if (this == o2)
return true;
if (_keyCount!=o2._keyCount)
return false;
if (hashCode()!=o2.hashCode())
Expand All @@ -158,15 +180,16 @@ public final boolean equals(Object o) {
* String representation of the multi-key.
* @return string representation of key.
*/
@Override
public String toString() {
StringBuffer result=new StringBuffer("(");
StringBuilder result = new StringBuilder('(');
for (short i=0; i<_keys.length; i++) {
Object o=_keys[i];
result.append(o instanceof EOEntity ? ((EOEntity)o).name() : o != null ? o.toString() : "<NULL>");
if(i != _keys.length-1)
result.append(", ");
}
result.append(")");
result.append(')');
return result.toString();
}
}

0 comments on commit d7e3052

Please sign in to comment.