Only allocate the array list when there are results.
A small thing... but significant in some use-cases.
This commit is contained in:
parent
a47fb27e56
commit
079c4c6482
@ -34,6 +34,7 @@ package com.jme3.collision;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <code>CollisionResults</code> is a collection returned as a result of a
|
* <code>CollisionResults</code> is a collection returned as a result of a
|
||||||
@ -43,15 +44,17 @@ import java.util.Iterator;
|
|||||||
*/
|
*/
|
||||||
public class CollisionResults implements Iterable<CollisionResult> {
|
public class CollisionResults implements Iterable<CollisionResult> {
|
||||||
|
|
||||||
private final ArrayList<CollisionResult> results = new ArrayList<CollisionResult>();
|
private ArrayList<CollisionResult> results = null;
|
||||||
private boolean sorted = true;
|
private boolean sorted = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears all collision results added to this list
|
* Clears all collision results added to this list
|
||||||
*/
|
*/
|
||||||
public void clear(){
|
public void clear(){
|
||||||
|
if (results != null) {
|
||||||
results.clear();
|
results.clear();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterator for iterating over the collision results.
|
* Iterator for iterating over the collision results.
|
||||||
@ -59,6 +62,11 @@ public class CollisionResults implements Iterable<CollisionResult> {
|
|||||||
* @return the iterator
|
* @return the iterator
|
||||||
*/
|
*/
|
||||||
public Iterator<CollisionResult> iterator() {
|
public Iterator<CollisionResult> iterator() {
|
||||||
|
if (results == null) {
|
||||||
|
List<CollisionResult> dumbCompiler = Collections.emptyList();
|
||||||
|
return dumbCompiler.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
if (!sorted){
|
if (!sorted){
|
||||||
Collections.sort(results);
|
Collections.sort(results);
|
||||||
sorted = true;
|
sorted = true;
|
||||||
@ -68,16 +76,22 @@ public class CollisionResults implements Iterable<CollisionResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addCollision(CollisionResult result){
|
public void addCollision(CollisionResult result){
|
||||||
|
if (results == null) {
|
||||||
|
results = new ArrayList<CollisionResult>();
|
||||||
|
}
|
||||||
results.add(result);
|
results.add(result);
|
||||||
sorted = false;
|
sorted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size(){
|
public int size(){
|
||||||
|
if (results == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return results.size();
|
return results.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CollisionResult getClosestCollision(){
|
public CollisionResult getClosestCollision(){
|
||||||
if (size() == 0)
|
if (results == null || size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!sorted){
|
if (!sorted){
|
||||||
@ -89,7 +103,7 @@ public class CollisionResults implements Iterable<CollisionResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CollisionResult getFarthestCollision(){
|
public CollisionResult getFarthestCollision(){
|
||||||
if (size() == 0)
|
if (results == null || size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!sorted){
|
if (!sorted){
|
||||||
@ -101,6 +115,10 @@ public class CollisionResults implements Iterable<CollisionResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CollisionResult getCollision(int index){
|
public CollisionResult getCollision(int index){
|
||||||
|
if (results == null) {
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: 0");
|
||||||
|
}
|
||||||
|
|
||||||
if (!sorted){
|
if (!sorted){
|
||||||
Collections.sort(results);
|
Collections.sort(results);
|
||||||
sorted = true;
|
sorted = true;
|
||||||
@ -115,6 +133,9 @@ public class CollisionResults implements Iterable<CollisionResult> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public CollisionResult getCollisionDirect(int index){
|
public CollisionResult getCollisionDirect(int index){
|
||||||
|
if (results == null) {
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: 0");
|
||||||
|
}
|
||||||
return results.get(index);
|
return results.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,11 +143,13 @@ public class CollisionResults implements Iterable<CollisionResult> {
|
|||||||
public String toString(){
|
public String toString(){
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("CollisionResults[");
|
sb.append("CollisionResults[");
|
||||||
|
if (results != null) {
|
||||||
for (CollisionResult result : results){
|
for (CollisionResult result : results){
|
||||||
sb.append(result).append(", ");
|
sb.append(result).append(", ");
|
||||||
}
|
}
|
||||||
if (results.size() > 0)
|
if (results.size() > 0)
|
||||||
sb.setLength(sb.length()-2);
|
sb.setLength(sb.length()-2);
|
||||||
|
}
|
||||||
|
|
||||||
sb.append("]");
|
sb.append("]");
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user