Skip to content

Commit e410347

Browse files
committed
Update equals, isEmpty and set methods
1 parent d1d455e commit e410347

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

src/main/java/com/dselent/bigarraylist/BigArrayList.java

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,7 @@ public E set(long index, E element) {
10021002
* @return The new element at the specified index
10031003
*/
10041004
public E set(int index, E element) {
1005-
long longIndex = index;
1006-
return set(longIndex, element);
1005+
return set((long)index, element);
10071006
}
10081007

10091008
/**
@@ -1012,49 +1011,36 @@ public E set(int index, E element) {
10121011
* @return True is the list is empty, false otherwise
10131012
*/
10141013
public boolean isEmpty() {
1015-
boolean empty = true;
1016-
1017-
if (size() > 0) {
1018-
empty = false;
1019-
}
1020-
1021-
return empty;
1014+
return wholeListSize == 0;
10221015
}
10231016

10241017
//hashCode cannot be implemented correctly due to contents being on disk and out of sight from memory
10251018

1026-
/* (non-Javadoc)
1027-
* @see java.lang.Object#equals(java.lang.Object)
1028-
*/
1029-
@SuppressWarnings("rawtypes")
10301019
@Override
10311020
public boolean equals(Object otherObject) {
10321021

1033-
boolean isEqual = true;
1034-
1035-
if (otherObject == null) {
1036-
isEqual = false;
1037-
} else if (this == otherObject) {
1038-
isEqual = true;
1039-
} else if (!(otherObject instanceof BigArrayList)) {
1040-
isEqual = false;
1041-
} else {
1042-
BigArrayList otherBigArrayList = (BigArrayList) otherObject;
1043-
1044-
if (wholeListSize != otherBigArrayList.size()) {
1045-
isEqual = false;
1046-
} else if (liveObject != otherBigArrayList.isLive()) {
1047-
isEqual = false;
1048-
} else {
1049-
for (long i = 0; i < wholeListSize && isEqual; i++) {
1050-
if (!get(i).equals(otherBigArrayList.get(i))) {
1051-
isEqual = false;
1052-
}
1053-
}
1054-
}
1022+
if (!(otherObject instanceof BigArrayList)) {
1023+
return super.equals(otherObject);
10551024
}
1025+
1026+
BigArrayList<?> otherBigArrayList = (BigArrayList<?>) otherObject;
10561027

1057-
return isEqual;
1028+
if (wholeListSize != otherBigArrayList.size()) {
1029+
return false;
1030+
}
1031+
if (liveObject != otherBigArrayList.isLive()) {
1032+
return false;
1033+
}
1034+
1035+
Iterator<E> thisIterator = this.iterator();
1036+
Iterator<?> otherIterator = otherBigArrayList.iterator();
1037+
while (thisIterator.hasNext() && otherIterator.hasNext()) {
1038+
E o1 = thisIterator.next();
1039+
Object o2 = otherIterator.next();
1040+
if (!(o1==null ? o2==null : o1.equals(o2)))
1041+
return false;
1042+
}
1043+
return !(thisIterator.hasNext() || otherIterator.hasNext());
10581044
}
10591045

10601046
@Override

0 commit comments

Comments
 (0)