Add Javadoc to method arguments and format the code

This commit is contained in:
Andrew Shcherbakov 2019-06-15 19:40:15 +02:00
parent 41502a7507
commit 5698486178
3 changed files with 26 additions and 28 deletions

View File

@ -15,11 +15,12 @@ import java.util.stream.IntStream;
public class FoldingHash { public class FoldingHash {
/** /**
* Calculate the hash value of a given string * Calculate the hash value of a given string.
* @param str *
* @param groupSize * @param str Assume it is not null
* @param maxValue * @param groupSize the group size in the folding technique
* @return * @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) { public int hash(String str, int groupSize, int maxValue) {
final int[] codes = this.toAsciiCodes(str); 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 * If the original array has not enough elements, the returning array will contain
* element from the offset till the end of the original array. * element from the offset till the end of the original array.
* *
* @param numbers * @param numbers original array. Assume it is not null.
* @param offset * @param offset index of the element to start from. Assume it is less than the size of the array
* @param length * @param length max size of the resulting array
* @return * @return
*/ */
public int[] extract(int[] numbers, int offset, int length) { 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. * Assume that the procedure does not suffer from the overflow.
* @param numbers * @param numbers integers to concatenate
* @return * @return
*/ */
public int concatenate(int[] numbers) { public int concatenate(int[] numbers) {
@ -66,8 +67,8 @@ public class FoldingHash {
} }
/** /**
* Convert the string into its characters' ascii codes. * Convert the string into its characters' ASCII codes.
* @param str * @param str input string
* @return * @return
*/ */
private int[] toAsciiCodes(String str) { private int[] toAsciiCodes(String str) {

View File

@ -7,11 +7,11 @@ package com.baeldung.folding;
* *
*/ */
public class Main { public class Main {
public static void main(String... arg) { public static void main(String... arg) {
FoldingHash hasher = new FoldingHash(); FoldingHash hasher = new FoldingHash();
final String str = "Java language"; final String str = "Java language";
System.out.println(hasher.hash(str, 2, 100_000)); System.out.println(hasher.hash(str, 2, 100_000));
System.out.println(hasher.hash(str, 3, 1_000)); System.out.println(hasher.hash(str, 3, 1_000));
} }
} }

View File

@ -15,7 +15,6 @@ public class FoldingHashUnitTest {
assertEquals(value, 48933); assertEquals(value, 48933);
} }
@Test @Test
public void givenStringVaJaLanguage_whenSize2Capacity100000_thenSameAsJavaLanguage() throws Exception { public void givenStringVaJaLanguage_whenSize2Capacity100000_thenSameAsJavaLanguage() throws Exception {
final FoldingHash hasher = new FoldingHash(); final FoldingHash hasher = new FoldingHash();
@ -24,7 +23,6 @@ public class FoldingHashUnitTest {
assertTrue(java == vaja); assertTrue(java == vaja);
} }
@Test @Test
public void givenSingleElementArray_whenOffset0Size2_thenSingleElement() throws Exception { public void givenSingleElementArray_whenOffset0Size2_thenSingleElement() throws Exception {
final FoldingHash hasher = new FoldingHash(); final FoldingHash hasher = new FoldingHash();
@ -53,5 +51,4 @@ public class FoldingHashUnitTest {
assertArrayEquals(new int[] { 3, 4, 5 }, value); assertArrayEquals(new int[] { 3, 4, 5 }, value);
} }
} }