Add Javadoc to method arguments and format the code
This commit is contained in:
parent
41502a7507
commit
5698486178
|
@ -15,11 +15,12 @@ import java.util.stream.IntStream;
|
|||
public class FoldingHash {
|
||||
|
||||
/**
|
||||
* Calculate the hash value of a given string
|
||||
* @param str
|
||||
* @param groupSize
|
||||
* @param maxValue
|
||||
* @return
|
||||
* Calculate the hash value of a given string.
|
||||
*
|
||||
* @param str Assume it is not null
|
||||
* @param groupSize the group size in the folding technique
|
||||
* @param maxValue defines a max value that the hash may acquire (exclusive)
|
||||
* @return integer value from 0 (inclusive) to maxValue (exclusive)
|
||||
*/
|
||||
public int hash(String str, int groupSize, int maxValue) {
|
||||
final int[] codes = this.toAsciiCodes(str);
|
||||
|
@ -37,9 +38,9 @@ public class FoldingHash {
|
|||
* If the original array has not enough elements, the returning array will contain
|
||||
* element from the offset till the end of the original array.
|
||||
*
|
||||
* @param numbers
|
||||
* @param offset
|
||||
* @param length
|
||||
* @param numbers original array. Assume it is not null.
|
||||
* @param offset index of the element to start from. Assume it is less than the size of the array
|
||||
* @param length max size of the resulting array
|
||||
* @return
|
||||
*/
|
||||
public int[] extract(int[] numbers, int offset, int length) {
|
||||
|
@ -53,9 +54,9 @@ public class FoldingHash {
|
|||
}
|
||||
|
||||
/**
|
||||
* Concatenate the numbers into a single number.
|
||||
* Concatenate the numbers into a single number as if they were strings.
|
||||
* Assume that the procedure does not suffer from the overflow.
|
||||
* @param numbers
|
||||
* @param numbers integers to concatenate
|
||||
* @return
|
||||
*/
|
||||
public int concatenate(int[] numbers) {
|
||||
|
@ -66,8 +67,8 @@ public class FoldingHash {
|
|||
}
|
||||
|
||||
/**
|
||||
* Convert the string into its characters' ascii codes.
|
||||
* @param str
|
||||
* Convert the string into its characters' ASCII codes.
|
||||
* @param str input string
|
||||
* @return
|
||||
*/
|
||||
private int[] toAsciiCodes(String str) {
|
||||
|
|
|
@ -7,11 +7,11 @@ package com.baeldung.folding;
|
|||
*
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String... arg) {
|
||||
FoldingHash hasher = new FoldingHash();
|
||||
final String str = "Java language";
|
||||
System.out.println(hasher.hash(str, 2, 100_000));
|
||||
System.out.println(hasher.hash(str, 3, 1_000));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,44 +14,41 @@ public class FoldingHashUnitTest {
|
|||
final int value = hasher.hash("Java language", 2, 100_000);
|
||||
assertEquals(value, 48933);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenStringVaJaLanguage_whenSize2Capacity100000_thenSameAsJavaLanguage() throws Exception {
|
||||
final FoldingHash hasher = new FoldingHash();
|
||||
final int java = hasher.hash("Java language", 2, 100_000);
|
||||
final int vaja = hasher.hash("vaJa language", 2, 100_000);
|
||||
assertTrue(java == vaja);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleElementArray_whenOffset0Size2_thenSingleElement() throws Exception {
|
||||
final FoldingHash hasher = new FoldingHash();
|
||||
final int[] value = hasher.extract(new int[] {5}, 0, 2);
|
||||
assertArrayEquals(new int[] {5}, value);
|
||||
final int[] value = hasher.extract(new int[] { 5 }, 0, 2);
|
||||
assertArrayEquals(new int[] { 5 }, value);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenFiveElementArray_whenOffset0Size3_thenFirstThreeElements() throws Exception {
|
||||
final FoldingHash hasher = new FoldingHash();
|
||||
final int[] value = hasher.extract(new int[] {1, 2, 3, 4, 5}, 0, 3);
|
||||
assertArrayEquals(new int[] {1, 2, 3}, value);
|
||||
final int[] value = hasher.extract(new int[] { 1, 2, 3, 4, 5 }, 0, 3);
|
||||
assertArrayEquals(new int[] { 1, 2, 3 }, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFiveElementArray_whenOffset1Size2_thenTwoElements() throws Exception {
|
||||
final FoldingHash hasher = new FoldingHash();
|
||||
final int[] value = hasher.extract(new int[] {1, 2, 3, 4, 5}, 1, 2);
|
||||
assertArrayEquals(new int[] {2, 3}, value);
|
||||
final int[] value = hasher.extract(new int[] { 1, 2, 3, 4, 5 }, 1, 2);
|
||||
assertArrayEquals(new int[] { 2, 3 }, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFiveElementArray_whenOffset2SizeTooBig_thenElementsToTheEnd() throws Exception {
|
||||
final FoldingHash hasher = new FoldingHash();
|
||||
final int[] value = hasher.extract(new int[] {1, 2, 3, 4, 5}, 2, 2000);
|
||||
assertArrayEquals(new int[] {3, 4, 5}, value);
|
||||
final int[] value = hasher.extract(new int[] { 1, 2, 3, 4, 5 }, 2, 2000);
|
||||
assertArrayEquals(new int[] { 3, 4, 5 }, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue