Package org.organicdesign.fp.collections
Class RangeOfInt
- java.lang.Object
-
- org.organicdesign.fp.collections.RangeOfInt
-
- All Implemented Interfaces:
Serializable
,Iterable<Integer>
,Collection<Integer>
,List<Integer>
,Sized
,UnmodCollection<Integer>
,UnmodIterable<Integer>
,UnmodList<Integer>
,UnmodSortedCollection<Integer>
,UnmodSortedIterable<Integer>
,Transformable<Integer>
public class RangeOfInt extends Object implements UnmodList<Integer>, 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 class
RangeOfInt.Equat
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodIterable
UnmodIterable.UnIterable
-
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 boolean
contains(int i)
Returns true if the number is within the bounds of this range (low end incluive, high end exclusive).boolean
contains(Object o)
Though this overrides List.contains(Object o), it is effectively a convenience method for calling contains(int i).boolean
equals(Object other)
Integer
get(int idx)
int
hashCode()
int
indexOf(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.int
lastIndexOf(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.@NotNull UnmodListIterator<Integer>
listIterator(int startIdx)
Subclasses should override this when they can do so more efficiently.static @NotNull RangeOfInt
of(@NotNull Number e)
static @NotNull RangeOfInt
of(@NotNull Number s, @NotNull Number e)
int
size()
Returns the number of items in this collection or iterable.@NotNull RangeOfInt
subList(int fromIndex, int toIndex)
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Methods inherited from interface java.util.List
spliterator
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
any, toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutList, toMutMap, toMutRrbt, toMutSet, toMutSortedMap, toMutSortedSet
-
-
-
-
Method Detail
-
of
@NotNull public static @NotNull RangeOfInt of(@NotNull @NotNull Number s, @NotNull @NotNull Number e)
-
of
@NotNull public static @NotNull RangeOfInt of(@NotNull @NotNull 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(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:
contains
in interfaceCollection<Integer>
- Specified by:
contains
in interfaceList<Integer>
- Specified by:
contains
in interfaceUnmodList<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:
IllegalArgumentException
- if the argument is not an Integer, Long, BigInteger, or String.NumberFormatException
- if a String argument cannot be parsed using Integer.valueOf().
-
indexOf
public int indexOf(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(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:
lastIndexOf
in interfaceList<Integer>
- Specified by:
lastIndexOf
in interfaceUnmodList<Integer>
-
size
public int size()
Description copied from interface:Sized
Returns the number of items in this collection or iterable.
-
hashCode
public int hashCode()
-
equals
public boolean equals(Object other)
-
listIterator
@NotNull public @NotNull UnmodListIterator<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:
listIterator
in interfaceList<Integer>
- Specified by:
listIterator
in interfaceUnmodList<Integer>
-
-