From ea321c60a59989e6b44212d05f77657041255630 Mon Sep 17 00:00:00 2001 From: Sandip Date: Mon, 26 Sep 2022 22:05:51 +0530 Subject: [PATCH] 2-Collection_Containers.adoc file updated for adding IntInterval Documentation. --- docs/2-Collection_Containers.adoc | 57 +++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/2-Collection_Containers.adoc b/docs/2-Collection_Containers.adoc index 0b1c7246ec..5f39cdab28 100644 --- a/docs/2-Collection_Containers.adoc +++ b/docs/2-Collection_Containers.adoc @@ -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