Skip to content

Commit

Permalink
2-Collection_Containers.adoc file updated for adding IntInterval Docu…
Browse files Browse the repository at this point in the history
…mentation.
  • Loading branch information
sandip1805 committed Oct 3, 2022
1 parent dc74b57 commit ea321c6
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions docs/2-Collection_Containers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -951,11 +951,64 @@ IntArrayList listFromIntIterable = IntArrayList.newListWith(IntHashSet.newSetWit
An *IntInterval* is a range of **int**s that may be iterated over using a step value.
(Similar to *Interval*, but uses primitive ints instead of the wrapper *Integers*.)
All arguments are inclusive in methods of IntInterval.
As *IntInterval* implements *ImmutableIntList*, you can pass it in any method or constructor argument which accept *ImmutableIntList*.
[source,java]
----
Assert.assertEquals(IntLists.mutable.with(1, 2, 3), IntInterval.oneTo(3));
Assert.assertEquals(IntLists.mutable.with(1, 3, 5), IntInterval.oneToBy(5, 2));
Assertions.assertInstanceOf(ImmutableIntList.class, IntInterval.oneTo(5));
----
.There are different ways you can generate interval of numbers using *IntInterval*, which are listed below.
- You can initialize IntInterval using constructor argument which accept from, to and steps.
- You can initialize IntInterval using static factory methods.
[source,java]
----
// Generate interval of numbers from 1 to 5, with the default step value of 1
ImmutableIntList expected = IntLists.immutable.with(1, 2, 3, 4, 5);
Assertions.assertEquals(expected, IntInterval.fromTo(1, 5));
// Generate interval of numbers from 1 to 10, with providing steps value as method argument
ImmutableIntList expected = IntLists.immutable.with(1, 4, 7, 10);
Assertions.assertEquals(expected, IntInterval.fromToBy(1, 10, 3));
// from method is used to generate interval with only one number which is provided as method argument
ImmutableIntList expected = IntLists.immutable.with(10);
Assertions.assertEquals(expected, IntInterval.from(10));
// Generate interval of even numbers, here steps value can be 2 or -2, based on following condition [to > from ? 2 : -2]
ImmutableIntList expected = IntLists.immutable.with(2, 4, 6);
Assertions.assertEquals(expected, IntInterval.evensFromTo(2, 6));
ImmutableIntList expected = IntLists.immutable.with(6, 4, 2);
Assertions.assertEquals(expected, IntInterval.evensFromTo(6, 2));
// Generate interval of odd numbers, here steps value can be 2 or -2, based on following condition [to > from ? 2 : -2]
ImmutableIntList expected = IntLists.immutable.with(1, 3, 5);
Assertions.assertEquals(expected, IntInterval.oddsFromTo(1, 5));
ImmutableIntList expected = IntLists.immutable.with(5, 3, 1);
Assertions.assertEquals(expected, IntInterval.oddsFromTo(5, 1));
// Generate interval of numbers from 1 to 5, with the default step value of 1
ImmutableIntList expected = IntLists.immutable.with(1, 2, 3, 4, 5);
Assertions.assertEquals(expected, IntInterval.oneTo(5));
// Generate interval of numbers from 1 to 9, with providing step value as a method argument
ImmutableIntList expected = IntLists.immutable.with(1, 3, 5, 7, 9);
Assertions.assertEquals(expected, IntInterval.oneToBy(10, 2));
// zero is used to create interval with only one number [0]
ImmutableIntList expected = IntLists.immutable.with(0);
Assertions.assertEquals(expected, IntInterval.zero());
// Generate interval of numbers from 0 to 4, with the default step value of 1
ImmutableIntList expected = IntLists.immutable.with(0, 1, 2, 3, 4);
Assertions.assertEquals(expected, IntInterval.zeroTo(4));
// Generate interval of numbers from 0 to 4, with providing step value as a method argument
ImmutableIntList expected = IntLists.immutable.with(0, 2, 4);
Assertions.assertEquals(expected, IntInterval.zeroToBy(4, 2));
----
=== Primitive sets
Expand Down

0 comments on commit ea321c6

Please sign in to comment.