Package org.organicdesign.fp.collections
Class RangeOfInt
- java.lang.Object
-
- org.organicdesign.fp.collections.RangeOfInt
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<java.lang.Integer>,java.util.Collection<java.lang.Integer>,java.util.List<java.lang.Integer>,Sized,UnmodCollection<java.lang.Integer>,UnmodIterable<java.lang.Integer>,UnmodList<java.lang.Integer>,UnmodSortedCollection<java.lang.Integer>,UnmodSortedIterable<java.lang.Integer>,Transformable<java.lang.Integer>
public class RangeOfInt extends java.lang.Object implements UnmodList<java.lang.Integer>, java.io.Serializable
An efficient (in both time and memory) implementation of List. If you want to compare a RangeOfInt to generic List<Integer>, use RangeOfInt.LIST_EQUATOR so that the hashCodes will be compatible. A RangeOfInt is an indexed sequence of integers. It currently assumes a step of 1. Like everything in Java and similar classes in Clojure, Python, and Scala, it is inclusive of the start value but exclusive of the end. In theory, a class like this could be made for anything that can provide it's next() and previous() item and a size. To do that, Integer would need to implement something that defined what the next() and previous() values. Currently limited to Integer.MIN_VALUE to Integer.MAX_VALUE.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRangeOfInt.Equat-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodList
UnmodList.AbstractUnmodList<E>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(int i)Returns true if the number is within the bounds of this range (low end incluive, high end exclusive).booleancontains(java.lang.Object o)Though this overrides List.contains(Object o), it is effectively a convenience method for calling contains(int i).booleanequals(java.lang.Object other)java.lang.Integerget(int idx)inthashCode()intindexOf(java.lang.Object o)Unlike most implementations of List, this method has excellent O(1) performance! The default implementation of this method has O(this.size()) performance.intlastIndexOf(java.lang.Object o)Unlike most implementations of List, this method has excellent O(1) performance! The default implementation of this method has O(this.size()) performance.UnmodListIterator<java.lang.Integer>listIterator(int startIdx)Subclasses should override this when they can do so more efficiently.static RangeOfIntof(java.lang.Number e)static RangeOfIntof(java.lang.Number s, java.lang.Number e)intsize()Returns the number of items in this collection or iterable.RangeOfIntsubList(int fromIndex, int toIndex)-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutableList, toMutableMap, toMutableRrbt, toMutableSet, toMutableSortedMap, toMutableSortedSet
-
-
-
-
Method Detail
-
of
public static RangeOfInt of(java.lang.Number s, java.lang.Number e)
-
of
public static RangeOfInt of(java.lang.Number e)
-
contains
public boolean contains(int i)
Returns true if the number is within the bounds of this range (low end incluive, high end exclusive). In math terms, returns true if the argument is [low, high). False otherwise. This is an efficient method when the compiler can see that it's being passed a primitive int, but contains(Object o) delegates to this method when it's passed a reasonable and unambiguous argument.
-
contains
public boolean contains(java.lang.Object o)
Though this overrides List.contains(Object o), it is effectively a convenience method for calling contains(int i). Therefore, it only accepts Integers, Longs, BigIntegers, and Strings that parse as signed decimal Integers. It does not accept Numbers since they can't easily be checked for truncation and floating-point might not round properly with respect to bounds, or might not make sense if you are using your range to define a set of integers. Handles truncation (returns false) for the types it accepts. Throws exceptions for types it does not accept. Thanks for all the help from codereview.stackexchange: http://codereview.stackexchange.com/questions/100846/rounding-and-truncation-in-intrange-containsobject-o- Specified by:
containsin interfacejava.util.Collection<java.lang.Integer>- Specified by:
containsin interfacejava.util.List<java.lang.Integer>- Specified by:
containsin interfaceUnmodList<java.lang.Integer>- Parameters:
o- an Integer, Long, BigInteger, or String that parses as a signed decimal Integer.- Returns:
- true if the number is within the bounds of this range (high end is exclusive). False otherwise.
- Throws:
java.lang.IllegalArgumentException- if the argument is not an Integer, Long, BigInteger, or String.java.lang.NumberFormatException- if a String argument cannot be parsed using Integer.valueOf().
-
get
public java.lang.Integer get(int idx)
- Specified by:
getin interfacejava.util.List<java.lang.Integer>
-
indexOf
public int indexOf(java.lang.Object o)
Unlike most implementations of List, this method has excellent O(1) performance! The default implementation of this method has O(this.size()) performance. If you call this much, you probably want to use a Map<Integer,T> instead for O(1) performance.
-
lastIndexOf
public int lastIndexOf(java.lang.Object o)
Unlike most implementations of List, this method has excellent O(1) performance! The default implementation of this method has O(this.size()) performance.- Specified by:
lastIndexOfin interfacejava.util.List<java.lang.Integer>- Specified by:
lastIndexOfin interfaceUnmodList<java.lang.Integer>
-
size
public int size()
Description copied from interface:SizedReturns the number of items in this collection or iterable.
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Collection<java.lang.Integer>- Specified by:
hashCodein interfacejava.util.List<java.lang.Integer>- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Specified by:
equalsin interfacejava.util.Collection<java.lang.Integer>- Specified by:
equalsin interfacejava.util.List<java.lang.Integer>- Overrides:
equalsin classjava.lang.Object
-
listIterator
public UnmodListIterator<java.lang.Integer> listIterator(int startIdx)
Subclasses should override this when they can do so more efficiently. Iterates from start of range (inclusive) up-to, but excluding, the end of the range. I'm not sure this is a good idea, but Python, Clojure, Scala, and just about everything in Java expects similar behavior.- Specified by:
listIteratorin interfacejava.util.List<java.lang.Integer>- Specified by:
listIteratorin interfaceUnmodList<java.lang.Integer>
-
subList
public RangeOfInt subList(int fromIndex, int toIndex)
-
-