From 6e07a214c6c99f647a901796ad44334b0b0d68d4 Mon Sep 17 00:00:00 2001 From: javasabr Date: Sun, 6 Aug 2017 12:03:06 +0300 Subject: [PATCH] Added a lit bit improvements for SafeArrayList: 1. improved comparing a SafeArrayList with another SafeArrayList. 2. improved the constructor by other collection 3. added a new constructor with init capacity. --- .../java/com/jme3/util/SafeArrayList.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/util/SafeArrayList.java b/jme3-core/src/main/java/com/jme3/util/SafeArrayList.java index 30593cd4b..7fb08ad6c 100644 --- a/jme3-core/src/main/java/com/jme3/util/SafeArrayList.java +++ b/jme3-core/src/main/java/com/jme3/util/SafeArrayList.java @@ -92,9 +92,21 @@ public class SafeArrayList implements List, Cloneable { this.elementType = elementType; } - public SafeArrayList(Class elementType, Collection c) { + public SafeArrayList(final Class elementType, final int capacity) { this.elementType = elementType; - addAll(c); + this.buffer = new ArrayList<>(capacity); + } + + public SafeArrayList(final Class elementType, final Collection collection) { + this.elementType = elementType; + + if (collection instanceof SafeArrayList) { + this.buffer = Arrays.asList(((SafeArrayList) collection).getArray()); + } else { + this.buffer = new ArrayList<>(collection); + } + + this.size = buffer.size(); } public SafeArrayList clone() { @@ -150,12 +162,12 @@ public class SafeArrayList implements List, Cloneable { return buffer; if( backingArray == null ) { - buffer = new ArrayList(); + buffer = new ArrayList<>(); } else { // Only keep the array or the buffer but never both at // the same time. 1) it saves space, 2) it keeps the rest // of the code safer. - buffer = new ArrayList( Arrays.asList(backingArray) ); + buffer = new ArrayList<>( Arrays.asList(backingArray) ); backingArray = null; } return buffer; @@ -243,10 +255,19 @@ public class SafeArrayList implements List, Cloneable { } public boolean equals(Object o) { - if( o == this ) + + if (o == this) { return true; - if( !(o instanceof List) ) //covers null too + } else if (o instanceof SafeArrayList) { + + final Object[] targetArray = ((SafeArrayList) o).getArray(); + final E[] array = getArray(); + + return Arrays.equals(targetArray, array); + } else if (!(o instanceof List)) {//covers null too return false; + } + List other = (List)o; Iterator i1 = iterator(); Iterator i2 = other.iterator();