Skip to content

Commit e02e00e

Browse files
authored
Add some methods from ArrayList (#3)
- **Add addAll, clear methods** - **Update equals, isEmpty and set methods**
2 parents bdf948d + e410347 commit e02e00e

File tree

5 files changed

+152
-66
lines changed

5 files changed

+152
-66
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=1.5
1+
version=1.6
22
release=false

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

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.Serializable;
2424
import java.util.ArrayList;
25+
import java.util.Collection;
2526
import java.util.Comparator;
2627
import java.util.Iterator;
2728
import java.util.List;
@@ -1001,8 +1002,7 @@ public E set(long index, E element) {
10011002
* @return The new element at the specified index
10021003
*/
10031004
public E set(int index, E element) {
1004-
long longIndex = index;
1005-
return set(longIndex, element);
1005+
return set((long)index, element);
10061006
}
10071007

10081008
/**
@@ -1011,49 +1011,36 @@ public E set(int index, E element) {
10111011
* @return True is the list is empty, false otherwise
10121012
*/
10131013
public boolean isEmpty() {
1014-
boolean empty = true;
1015-
1016-
if (size() > 0) {
1017-
empty = false;
1018-
}
1019-
1020-
return empty;
1014+
return wholeListSize == 0;
10211015
}
10221016

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

1025-
/* (non-Javadoc)
1026-
* @see java.lang.Object#equals(java.lang.Object)
1027-
*/
1028-
@SuppressWarnings("rawtypes")
10291019
@Override
10301020
public boolean equals(Object otherObject) {
10311021

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

1056-
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());
10571044
}
10581045

10591046
@Override
@@ -1078,4 +1065,33 @@ public E next() {
10781065
}
10791066
};
10801067
}
1068+
1069+
/**
1070+
* @param other The other BigArrayList
1071+
* @see ArrayList#addAll(Collection)
1072+
*/
1073+
public void addAll(BigArrayList<? extends E> other) {
1074+
for (E element : other) {
1075+
add(element);
1076+
}
1077+
}
1078+
1079+
/**
1080+
* @param other The other collection
1081+
* @see ArrayList#addAll(Collection)
1082+
*/
1083+
public void addAll(Collection<? extends E> other) {
1084+
for (E element : other) {
1085+
add(element);
1086+
}
1087+
}
1088+
1089+
/**
1090+
* Clears all elements without clearing the memory
1091+
*/
1092+
public void clear() {
1093+
for (long i = wholeListSize-1; i >= 0; i--) {
1094+
remove(i);
1095+
}
1096+
}
10811097
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,4 @@ protected void clearMemory() throws IOException
499499
{
500500
fileAccessor.clearMemory();
501501
}
502-
}
502+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,6 @@ protected void clearMemory() throws IOException
368368
}
369369
}
370370
}
371-
372371
//don't delete the folder, other things may be using it
373-
374372
}
375-
376-
}
373+
}

0 commit comments

Comments
 (0)