@Test
public void gcd_of_array_containing_1_to_5_is_1() throws Exception {
//计算最大公约数
OptionalInt gcd = Snippets.gcd(new int[]{1, 2, 3, 4, 5});
assertThat(gcd).isNotEmpty();
assertThat(gcd).hasValue(1);
}
@Test
public void gcd_of_array_containing_4_8_and_12_is_4() throws Exception {
OptionalInt gcd = Snippets.gcd(new int[]{4, 8, 12});
assertThat(gcd).isNotEmpty();
assertThat(gcd).hasValue(4);
}
@Test
public void lcm_of_array_containing_1_to_5_is_60() throws Exception {
//计算最小公倍数
OptionalInt lcm = Snippets.lcm(new int[]{1, 2, 3, 4, 5});
assertThat(lcm).isNotEmpty();
assertThat(lcm).hasValue(60);
}
@Test
public void lcm_of_array_containing_4_8_and_12_is_24() throws Exception {
OptionalInt lcm = Snippets.lcm(new int[]{4, 8, 12});
assertThat(lcm).isNotEmpty();
assertThat(lcm).hasValue(24);
}
@Test
public void max_of_array_containing_10_1_and_5_is_10() throws Exception {
//计算数组当中的最大值
OptionalInt max = Snippets.arrayMax(new int[]{10, 1, 5});
assertThat(max).hasValue(10);
}
@Test
public void min_of_array_containing_10_1_and_5_is_10() throws Exception {
//计算数组当中的最小值
OptionalInt min = Snippets.arrayMin(new int[]{10, 1, 5});
assertThat(min).hasValue(1);
}
@Test
public void chunk_breaks_input_array__with_odd_length() throws Exception {
//将数组分割成特定大小的小数组。
int[][] chunks = Snippets.chunk(new int[]{1, 2, 3, 4, 5}, 2);
assertThat(chunks)
.containsExactly(
new int[]{1, 2},
new int[]{3, 4},
new int[]{5}
);
}
@Test
public void chunk_breaks_input_array__with_event_length() throws Exception {
int[][] chunks = Snippets.chunk(new int[]{1, 2, 3, 4, 5, 6}, 2);
assertThat(chunks)
.containsExactly(
new int[]{1, 2},
new int[]{3, 4},
new int[]{5, 6}
);
}
@Test
public void countOccurrences_counts_occurrences_of_a_value() throws Exception {
//计算数组中某个值出现的次数。
long count = Snippets.countOccurrences(new int[]{1, 1, 2, 1, 2, 3}, 1);
assertThat(count).isEqualTo(3);
}
@Test
public void deepFlatten_flatten_a_deeply_nested_array() throws Exception {
//数组扁平化。
int[] flatten = Snippets.deepFlatten(
new Object[]{1, new Object[]{2}, new Object[]{3, 4, 5}}
);
assertThat(flatten).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void difference_between_array_with_1_2_3_and_array_with_1_2_4_is_3() throws Exception {
//返回两个数组之间的差异。
int[] difference = Snippets.difference(new int[]{1, 2, 3}, new int[]{1, 2, 4});
assertThat(difference).isEqualTo(new int[]{3});
}
@Test
public void difference_between_array_with_1_2_3_and_array_with_1_2_3_is_empty_array() throws Exception {
int[] difference = Snippets.difference(new int[]{1, 2, 3}, new int[]{1, 2, 3});
assertThat(difference).isEmpty();
}
@Test
public void differenceWith_return_all_squares_that_do_not_exist_in_second() throws Exception {
//从比较器函数不返回true的数组中筛选出所有值。
int[] difference = Snippets.differenceWith(
new int[]{1, 4, 9, 16, 25},
new int[]{1, 2, 3, 6, 7},
(o1, o2) -> o1 - (o2 * o2)
);
assertThat(difference).isEqualTo(new int[]{16, 25});
}
@Test
public void differenceWith_returns_empty_array_when_two_arrays_are_equal_as_per_comparison_operation() throws Exception {
int[] difference = Snippets.differenceWith(
new int[]{1, 2, 3},
new int[]{1, 2, 3},
(o1, o2) -> o1 - o2
);
assertThat(difference).isEmpty();
}
@Test
public void differenceWith_returns_first_array_when_elements_in_second_array_are_not_comparable_as_per_comparison_operation() throws Exception {
int[] difference = Snippets.differenceWith(
new int[]{1, 2, 3},
new int[]{10, 11, 12},
(o1, o2) -> o1 - o2
);
assertThat(difference).isEqualTo(new int[]{1, 2, 3});
}
@Test
public void distinct_remove_all_duplicate_values_from_an_array() throws Exception {
//返回数组的所有不同值。
int[] distinct = Snippets.distinctValuesOfArray(new int[]{1, 2, 2, 3, 4, 4, 5});
assertThat(distinct).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void drop_elements_less_than_3() throws Exception {
//移除数组中的元素,直到传递的函数返回true为止。返回数组中的其余元素
int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i >= 3);
assertThat(elements).isEqualTo(new int[]{3, 4});
}
@Test
public void drop_elements_returns_empty_array_when_no_element_match_the_condition() throws Exception {
int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i < 1);
assertThat(elements).isEmpty();
}
@Test
public void drop_elements__return_all_elements_when_all_elements_match_the_condition() throws Exception {
int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i <= 4);
assertThat(elements).isEqualTo(new int[]{1, 2, 3, 4});
}
@Test
public void dropRight_remove_n_elements_from_right() throws Exception {
//返回一个新数组,从右边移除n个元素。
int[] elements = Snippets.dropRight(new int[]{1, 2, 3}, 1);
assertThat(elements).isEqualTo(new int[]{1, 2});
elements = Snippets.dropRight(new int[]{1, 2, 3}, 2);
assertThat(elements).isEqualTo(new int[]{1});
elements = Snippets.dropRight(new int[]{1, 2, 3}, 3);
assertThat(elements).isEmpty();
elements = Snippets.dropRight(new int[]{1, 2, 3}, 42);
assertThat(elements).isEmpty();
}
@Test
public void everyNth_return_every_2nd_element() throws Exception {
//返回数组中的每个第n个元素。
int[] elements = Snippets.everyNth(new int[]{1, 2, 3, 4, 5, 6}, 2);
assertThat(elements).isEqualTo(new int[]{2, 4, 6});
}
@Test
public void indexOf() throws Exception {
//查找数组中元素的索引,在不存在元素的情况下返回-1。
int i = Snippets.indexOf(new int[]{1, 7, 3, 7, 5, 6}, 7);
System.out.println(i);
}
@Test
public void lastIndexOf() throws Exception {
//查找数组中元素的最后索引,在不存在元素的情况下返回-1。
int i = Snippets.lastIndexOf(new int[]{1, 7, 3, 7, 5, 6}, 7);
System.out.println(i);
}
@Test
public void filterNonUnique_return_unique_elements() throws Exception {
//筛选出数组中的非唯一值。
int[] elements = Snippets.filterNonUnique(new int[]{1, 2, 2, 3, 4, 4, 5});
assertThat(elements).isEqualTo(new int[]{1, 3, 5});
}
@Test
public void filterNonUnique_return_same_array_when_all_unique() throws Exception {
int[] elements = Snippets.filterNonUnique(new int[]{1, 2, 3, 4, 5});
assertThat(elements).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void filterNonUnique_return_empty_array_when_all_duplicated() throws Exception {
int[] elements = Snippets.filterNonUnique(new int[]{1, 1, 2, 2, 3, 3, 4, 4, 5, 5});
assertThat(elements).isEmpty();
}
@Test
public void flatten_flat_one_level_array() throws Exception {
//使数组扁平。
int[] flatten = Snippets.flatten(new Object[]{1, new int[]{2}, 3, 4});
assertThat(flatten).isEqualTo(new int[]{1, 2, 3, 4});
}
@Test
public void flattenDepth_flatten_to_specified_depth() throws Exception {
//将数组压平到指定的深度。
Object[] input = {
1,
new Object[]{2},
new Object[]{
new Object[]{
new Object[]{
3
},
4
}, 5
}
};
Object[] flatten = Snippets.flattenDepth(input, 2);
assertThat(flatten).isEqualTo(new Object[]{1, 2, new Object[]{3}, 4, 5});
}
@Test
public void group_elements_by_length() throws Exception {
//根据给定函数对数组元素进行分组。
Map<Integer, List<String>> groups = Snippets.groupBy(new String[]{"one", "two", "three"}, String::length);
assertThat(groups)
.containsExactly(
new SimpleEntry<>(3, Arrays.asList("one", "two")),
new SimpleEntry<>(5, Collections.singletonList("three"))
);
}
@Test
public void initial_return_array_except_last_element() throws Exception {
//返回数组中除去最后一个的所有元素。
Integer[] initial = Snippets.initial(new Integer[]{1, 2, 3});
assertThat(initial).isEqualTo(new Integer[]{1, 2});
}
@Test
public void initializeArrayWithRange_from_1_to_5() throws Exception {
//初始化一个数组,该数组包含在指定范围内的数字,传入 start 和 end。
int[] numbers = Snippets.initializeArrayWithRange(5, 1);
assertThat(numbers).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void initializeArrayWithValues() throws Exception {
//使用指定的值初始化并填充数组。
int[] elements = Snippets.initializeArrayWithValues(5, 2);
assertThat(elements).isEqualTo(new int[]{2, 2, 2, 2, 2});
}
@Test
public void intersection_between_two_arrays() throws Exception {
//返回两个数组中存在的元素列表。
int[] elements = Snippets.intersection(new int[]{1, 2, 3}, new int[]{4, 3, 2});
assertThat(elements).isEqualTo(new int[]{2, 3});
}
@Test
public void isSorted_return_1_when_array_sorted_is_ascending_order() throws Exception {
//如果数组按升序排序,则返回 1,如果数组按降序排序,返回 -1,如果没有排序,则返回 0。
int sorted = Snippets.isSorted(new Integer[]{0, 1, 2, 3});
assertThat(sorted).isEqualTo(1);
sorted = Snippets.isSorted(new Integer[]{0, 1, 2, 2});
assertThat(sorted).isEqualTo(1);
}
@Test
public void isSorted_return_minus_1_when_array_sorted_in_descending_order() throws Exception {
int sorted = Snippets.isSorted(new Integer[]{3, 2, 1, 0});
assertThat(sorted).isEqualTo(-1);
sorted = Snippets.isSorted(new Integer[]{3, 3, 2, 1, 0});
assertThat(sorted).isEqualTo(-1);
}
@Test
public void isSorted_returns_0_when_array_is_not_sorted() throws Exception {
int sorted = Snippets.isSorted(new Integer[]{3, 4, 1, 0});
assertThat(sorted).isEqualTo(0);
}
@Test
public void join_should_create_string_from_an_array_with_different_sep_and_end() throws Exception {
//将数组的所有元素连接到字符串中,并返回此字符串。
String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"}, ",", "&");
assertThat(joined).isEqualTo("pen,pineapple,apple&pen");
}
@Test
public void join_should_create_string_from_an_array_with_sep_only() throws Exception {
String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"}, ",");
assertThat(joined).isEqualTo("pen,pineapple,apple,pen");
}
@Test
public void join_should_create_string_from_an_array_with_default_sep() throws Exception {
String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"});
assertThat(joined).isEqualTo("pen,pineapple,apple,pen");
}
@Test
public void join_should_create_empty_string_with_empty_array() throws Exception {
String joined = Snippets.join(new String[]{});
assertThat(joined).isEqualTo("");
}
@Test
public void nthElement_return_nth_element_from_start_when_n_is_greater_than_0() throws Exception {
//返回数组的第n个元素。
String nthElement = Snippets.nthElement(new String[]{"a", "b", "c"}, 1);
assertThat(nthElement).isEqualTo("b");
}
@Test
public void nthElement_return_nth_element_from_end_when_n_is_less_than_0() throws Exception {
String nthElement = Snippets.nthElement(new String[]{"a", "b", "c"}, -3);
assertThat(nthElement).isEqualTo("a");
}
@Test
public void pick_should_pick_key_pairs_corresponding_to_keys() throws Exception {
Map<String, Integer> obj = new HashMap<>();
obj.put("a", 1);
obj.put("b", 2);
obj.put("c", 3);
System.out.println(obj.entrySet().stream().filter(s -> !s.getKey().contains("a") || s.getKey().contains("c")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
//从对象中选择与给定键对应的键值对。
Map<String, Integer> picked = Snippets.pick(obj, new String[]{"a", "c"});
System.out.println(picked);
assertThat(picked).containsExactly(new SimpleEntry<>("a", 1), new SimpleEntry<>("c", 3));
}
@Test
public void reducedFilter_Test() throws Exception {
Map<String, Object> item1 = new HashMap<>();
item1.put("id", 1);
item1.put("name", "john");
item1.put("age", 24);
Map<String, Object> item2 = new HashMap<>();
item2.put("id", 2);
item2.put("name", "mike");
item2.put("age", 50);
Map<String, Object> item3 = new HashMap<>();
item3.put("id", 21);
item3.put("name", "mike1");
item3.put("age", 50);
//根据条件筛选对象数组,同时筛选出未指定的键。
Map<String, Object>[] filtered = Snippets.reducedFilter((Map<String, Object>[]) new Map[]{item1, item2,item3}, new String[]{"id", "name"}, item -> (Integer) item.get("age") > 24);
System.out.println(Arrays.toString(filtered));
List<Map<String, Object>> maps = Arrays.asList(filtered);
System.out.println(maps);
ArrayList<Map<String, Object>> objects = new ArrayList<>();
objects.add(item1);
objects.add(item2);
objects.add(item3);
System.out.println(objects.stream().filter(s -> Integer.parseInt(s.get("age").toString()) > 24).map(el -> Arrays.stream(new String[]{"id", "name"}).filter(el::containsKey)
.collect(Collectors.toMap(Function.identity(), el::get))).collect(Collectors.toList()));
/*assertThat(filtered).hasSize(1);
assertThat(filtered[0])
.containsOnly(
new SimpleEntry<String, Object>("id", 2),
new SimpleEntry<String, Object>("name", "mike"));*/
}
@Test
public void sample_should_return_random_element() throws Exception {
//从数组中返回一个随机元素。
Integer sample = Snippets.sample(new Integer[]{3, 7, 9, 11});
System.out.println(sample);
assertThat(sample).isIn(Arrays.asList(3, 7, 9, 11));
}
@Test
public void sampleSize_should_return_sample_of_size_array_length_when_sample_size_is_greater_than_array_size() throws Exception {
//从 array 到 array 大小的唯一键获取 n 个随机元素。
Integer[] sample = Snippets.sampleSize(new Integer[]{1, 2, 3}, 3);
System.out.println(Arrays.toString(sample));
assertThat(sample).hasSize(3);
}
@Test
public void similarity_test() throws Exception {
//返回出现在两个数组中的元素数组。
Integer[] arr = Snippets.similarity(new Integer[]{1, 2, 3}, new Integer[]{1, 2, 4});
assertThat(arr).isEqualTo(new Integer[]{1, 2});
}
@Test
public void empty_array() throws Exception {
//新建一个泛型空数组
String[] empty = Snippets.emptyArray(String.class);
assertThat(empty).isEmpty();
}
@Test
public void sortedIndex_descending() throws Exception {
//返回值应该插入到数组中的最低索引,以保持其排序顺序。
int index = Snippets.sortedIndex(new Integer[]{0, 3, 7, 1}, 7);
System.out.println(index);//2
int index1 = Snippets.sortedIndex(new Integer[]{0, 3, 7, 1}, 8);
System.out.println(index1);//4
//assertThat(index).isEqualTo(1);
}
@Test
public void sortedIndex_ascending() throws Exception {
int index = Snippets.sortedIndex(new Integer[]{30, 50}, 40);
assertThat(index).isEqualTo(1);
}
@Test
public void sortedIndex_ascending_at_end() throws Exception {
int index = Snippets.sortedIndex(new Integer[]{30, 50}, 60);
System.out.println(index);
// assertThat(index).isEqualTo(2);
}
@Test
public void symmetricDifference_test() throws Exception {
//返回两个数组之间的对称差异。
Integer[] diff = Snippets.symmetricDifference(
new Integer[]{1, 2, 3},
new Integer[]{1, 2, 4}
);
assertThat(diff).isEqualTo(new Integer[]{3, 4});
/* Integer[] diff = Snippets.symmetricDifference(
new Integer[]{1, 2, 3},
new Integer[]{1, 2, 4 , 6 }
);
3,4,6*/
}
@Test
public void takeRight() throws Exception {
Integer[] integers = {1, 22, 3,7,55,8};
//返回从末尾移除n个元素的数组。
Integer[] integers1 = Snippets.takeRight(integers, 4);
//返回一个从开头删除n个元素的数组。
Integer[] integers2 = Snippets.take(integers, 4);
//返回数组中除第一个元素外的所有元素。
Integer[] integers3 = Snippets.tail(integers);
System.out.println(Arrays.toString(integers1));//[3, 7, 55, 8]
System.out.println(Arrays.toString(integers2));//[1, 22, 3, 7]
System.out.println(Arrays.toString(integers3));//[22, 3, 7, 55, 8]
}
@Test
public void union_test() throws Exception {
//返回两个数组中任何一个中存在的每个元素一次。
Integer[] union = Snippets.union(
new Integer[]{1, 2, 3},
new Integer[]{1, 2, 4}
);
assertThat(union).isEqualTo(new Integer[]{1, 2, 3, 4});
}
@Test
public void without_test() throws Exception {
//筛选出具有指定值之一的数组的元素。
Integer[] without = Snippets.without(
new Integer[]{2, 1, 2, 3},
1, 5
);
System.out.println(Arrays.toString(without));//[2, 2, 3]
//assertThat(without).isEqualTo(new Integer[]{3});
}
@Test
public void zip_test() throws Exception {
//根据原始数组中的位置创建元素数组。
List<Object[]> zipped = Snippets.zip(
new String[]{"a", "b"},
new Integer[]{1, 2},
new Boolean[]{true, false}
);
ArrayList<List<Object>> objects1 = new ArrayList<>();
for (int i = 0; i < zipped.size(); i++) {
Object[] objects = zipped.get(i);
objects1.add(Arrays.asList(objects));
}
System.out.println(objects1);
assertThat(zipped).hasSize(2);
assertThat(zipped.get(0)).isEqualTo(new Object[]{"a", 1, true});
assertThat(zipped.get(1)).isEqualTo(new Object[]{"b", 2, false});
}
@Test
public void zip_test_2() throws Exception {
List<Object[]> zipped = Snippets.zip(
new String[]{"a"},
new Integer[]{1, 2},
new Boolean[]{true, false}
);
assertThat(zipped).hasSize(2);
assertThat(zipped.get(0)).isEqualTo(new Object[]{"a", 1, true});
assertThat(zipped.get(1)).isEqualTo(new Object[]{null, 2, false});
ArrayList<List<Object>> objects1 = new ArrayList<>();
}
@Test
public void zipObject_test_1() throws Exception {
//给定有效的属性标识符数组和值数组,返回将属性与值关联的对象。
Map<String, Object> map = Snippets.zipObject(
new String[]{"a", "b", "c"},
new Integer[]{1, 2}
);
assertThat(map).containsOnly(
new SimpleEntry<>("a", 1),
new SimpleEntry<>("b", 2),
new SimpleEntry<>("c", null)
);
}
@Test
public void zipObject_test_2() throws Exception {
Map<String, Object> map = Snippets.zipObject(
new String[]{"a", "b"},
new Integer[]{1, 2, 3}
);
assertThat(map).containsOnly(
new SimpleEntry<>("a", 1),
new SimpleEntry<>("b", 2)
);
}
@Test
public void average_of_1_to_10_is_5_dot_5() throws Exception {
//返回两个或两个以上数字的平均值。
double average = Snippets.average(IntStream.rangeClosed(1, 10).toArray());
assertThat(average).isEqualTo(5.5);
}
@Test
public void capitalize_test() throws Exception {
//将字符串首字母大写。
assertThat(Snippets.capitalize("fooBar", false)).isEqualTo("FooBar");
assertThat(Snippets.capitalize("fooBar", true)).isEqualTo("Foobar");
}
@Test
public void capitalizeEveryWord_test() throws Exception {
//将字符串中每个单词的首字母大写。
String actual = Snippets.capitalizeEveryWord("hello world!");
System.out.println(actual);
//assertThat(actual).isEqualTo("Hello World!");
}
@Test
public void anagrams_test() throws Exception {
//生成一个字符串的所有字符(包含重复)。
List<String> anagrams = Snippets.anagrams("abc");
assertThat(anagrams)
.containsOnly("abc", "acb", "bac", "bca", "cab", "cba");
}
@Test
public void byteSize_of_smiley_is_4() throws Exception {
//以字节为单位返回字符串的长度。
int length = Snippets.byteSize("\uD83D\uDE00");
assertThat(length).isEqualTo(4);
}
@Test
public void byteSize_of_hello_world_is_11() throws Exception {
assertThat(Snippets.byteSize("Hello World")).isEqualTo(11);
}
@Test
public void countVowels_test() throws Exception {
//在提供的字符串中返回元音的个数。
assertThat(Snippets.countVowels("foobar")).isEqualTo(3);
}
@Test
public void escapeRegex_test() throws Exception {
//转义要在正则表达式中使用的字符串。
String actual = Snippets.escapeRegExp("(test)");
System.out.println(actual);
assertThat(actual).isEqualTo("\\Q(test)\\E");
}
@Test
public void fromCamelCase_test() throws Exception {
//从驼峰式转换字符串。
assertThat(Snippets.fromCamelCase("someJavaProperty", "_"))
.isEqualTo("some_java_property");
assertThat(Snippets.fromCamelCase("someDatabaseFieldName", " "))
.isEqualTo("some database field name");
assertThat(Snippets.fromCamelCase("someLabelThatNeedsToBeCamelized", "-"))
.isEqualTo("some-label-that-needs-to-be-camelized");
}
@Test
public void isAbsoluteUrl_test() throws Exception {
//如果给定的字符串是绝对URL,则返回 true,否则返回 false。
assertThat(Snippets.isAbsoluteUrl("https://google.com")).isTrue();
assertThat(Snippets.isAbsoluteUrl("ftp://www.myserver.net")).isTrue();
assertThat(Snippets.isAbsoluteUrl("/foo/bar")).isFalse();
}
@Test
public void isLowerCase_test() throws Exception {
//检查字符串是否为小写。
assertThat(Snippets.isLowerCase("abc")).isTrue();
assertThat(Snippets.isLowerCase("a3@$")).isTrue();
assertThat(Snippets.isLowerCase("Ab4")).isFalse();
}
@Test
public void mask_test() throws Exception {
//用指定的掩码字符替换除最后 num 个字符以外的所有字符。
assertThat(Snippets.mask("1234567890", 4, "*")).isEqualTo("******7890");
assertThat(Snippets.mask("1234567890", 3, "*")).isEqualTo("*******890");
assertThat(Snippets.mask("1234567890", -4, "*")).isEqualTo("1234******");
}
@Test
public void palindrome_test() throws Exception {
//判断一个字符串是否回文。
assertThat(Snippets.isPalindrome("taco cat")).isTrue();
assertThat(Snippets.isPalindrome("abc")).isFalse();
}
@Test
public void reverseString_test() throws Exception {
//反转字符串。
assertThat(Snippets.reverseString("foobar")).isEqualTo("raboof");
}
@Test
public void sortCharactersInString_test() throws Exception {
//按字母顺序排列字符串中的字符。
assertThat(Snippets.sortCharactersInString("cabbage")).isEqualTo("aabbceg");
}
@Test
public void splitLines_test() throws Exception {
//将多行字符串拆分为行数组。
String[] actual = Snippets.splitLines("This\nis a\r\nmultiline\nstring.\n");
assertThat(actual)
.isEqualTo(new String[]{
"This",
"is a",
"multiline",
"string."
});
System.out.println("\\r?\\n");
}
@Test
public void toCamelCase_test() throws Exception {
//转换一个字符串为驼峰式。
assertThat(Snippets.toCamelCase("some_database_field_name")).isEqualTo("someDatabaseFieldName");
assertThat(Snippets.toCamelCase("Some label that needs to be camelized")).isEqualTo("someLabelThatNeedsToBeCamelized");
assertThat(Snippets.toCamelCase("some-java-property")).isEqualTo("someJavaProperty");
assertThat(Snippets.toCamelCase("some-mixed_string with spaces_underscores-and-hyphens")).isEqualTo("someMixedStringWithSpacesUnderscoresAndHyphens");
}
@Test
public void toKebabCase_test() throws Exception {
//将字符串转换为kebab大小写。
assertThat(Snippets.toKebabCase("camelCase")).isEqualTo("camel-case");
assertThat(Snippets.toKebabCase("some text")).isEqualTo("some-text");
assertThat(Snippets.toKebabCase("some-mixed_string With spaces_underscores-and-hyphens")).isEqualTo("some-mixed-string-with-spaces-underscores-and-hyphens");
assertThat(Snippets.toKebabCase("AllThe-small Things")).isEqualTo("all-the-small-things");
assertThat(Snippets.toKebabCase("IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingXMLAndHTML")).isEqualTo("i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html");
}
@Test
public void toKebabCase_test1() throws Exception {
//正则匹配。
List<String> camel21321Case = Snippets.match("camel21321Case", "\\d");
System.out.println(camel21321Case);//[2, 1, 3, 2, 1]
//集合转字符串并以','分割
String join = String.join(",", camel21321Case);
System.out.println(join);//2,1,3,2,1
//集合替换根据正则的条件
camel21321Case.replaceAll(s->s.replaceAll("\\d", "+"));
System.out.println(camel21321Case);//[+, +, +, +, +]
//字符串替换根据正则的条件
String s = "camel21321Case".replaceAll("\\d", "+");
System.out.println(s);//camel+++++Case
}
@Test
public void toSnakeCase_test() throws Exception {
//将字符串转换为蛇形小写,如 Im_Biezhi。
assertThat(Snippets.toSnakeCase("camelCase")).isEqualTo("camel_case");
assertThat(Snippets.toSnakeCase("some text")).isEqualTo("some_text");
assertThat(Snippets.toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens")).isEqualTo("some_mixed_string_with_spaces_underscores_and_hyphens");
assertThat(Snippets.toSnakeCase("AllThe-small Things")).isEqualTo("all_the_small_things");
assertThat(Snippets.toSnakeCase("IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingXMLAndHTML")).isEqualTo("i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_xml_and_html");
}
@Test
public void truncateString_test() throws Exception {
//将字符串截断到指定的长度。
assertThat(Snippets.truncateString("boomerang", 7)).isEqualTo("boom...");
}
@Test
public void words_test() throws Exception {
//将给定的字符串转换为单词数组。 按照"[^a-zA-Z-]+"正则的形式
assertThat(Snippets.words("I love java!!")).isEqualTo(new String[]{"I", "love", "java"});
assertThat(Snippets.words("Kotlin,Java & LemonTea")).isEqualTo(new String[]{"Kotlin", "Java", "LemonTea"});
}
@Test
public void randomInts_test() throws Exception {
//生成5个随机数 范围在[100-200)
int[] actual = Snippets.randomInts(5, 100, 200);
System.out.println(Arrays.toString(actual));
assertThat(actual).hasSize(5);
}
@Test
public void concat_test() throws Exception {
//数组之前的链接
String[] first = {"a", "b"};
String[] second = {"c", "d"};
assertThat(Snippets.concat(first, second))
.isEqualTo(new String[]{"a", "b", "c", "d"});
}
@Test
public void getCurrentWorkingDirectoryPath_test() throws Exception {
//获取当前工作目录。
assertThat(Snippets.getCurrentWorkingDirectoryPath()).isNotBlank();
//以小写字符串的形式获取操作系统的名称。
System.out.println(Snippets.osName());
//C:\Users\tzy\Desktop\hoot\javaDemo\30-seconds-of-java8-master\30-seconds-of-java8-master
System.out.println(Snippets.getCurrentWorkingDirectoryPath());
//返回 java.io.tmpdir 系统属性的值。如果末尾没有分隔符,则追加分隔符。
System.out.println(Snippets.tmpDirName());//C:\Users\tzy\AppData\Local\Temp\
try {
int i=1/0;
}catch (Exception e){
//将异常堆栈跟踪转换为字符串。
System.out.println(Snippets.stackTraceAsString(e));
}
}
@Test
public void isNumeric_test() throws Exception {
//检查字符串是否为数字。
assertThat(Snippets.isNumeric("123")).isTrue();
assertThat(Snippets.isNumeric("abc")).isFalse();
assertThat(Snippets.isNumeric("")).isFalse();
}
@Test
public void findNextPositivePowerOfTwo_test() throws Exception {
//查找大于或等于该值的下一个幂。
assertThat(Snippets.findNextPositivePowerOfTwo(-1)).isEqualTo(1);
assertThat(Snippets.findNextPositivePowerOfTwo(3)).isEqualTo(4);
assertThat(Snippets.findNextPositivePowerOfTwo(31)).isEqualTo(32);
assertThat(Snippets.findNextPositivePowerOfTwo(32)).isEqualTo(32);
}
@Test
public void isEven_test() throws Exception {
//检查数字是否是偶数。
assertThat(Snippets.isEven(1)).isFalse();
assertThat(Snippets.isEven(2)).isTrue();
assertThat(Snippets.isEven(3)).isFalse();
assertThat(Snippets.isEven(4)).isTrue();
assertThat(Snippets.isEven(-1)).isFalse();
}
@Test
public void stringToIntegers_test() throws Exception {
//将由空格分隔的数字字符串转换为 int 数组。
int[] intArray = Snippets.stringToIntegers("1 2 3 4 5");
assertThat(intArray).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
private static class Class1 implements I2, I1, I5 {
// empty
}
private static class Class2 extends Class1 implements I2, I3 {
// empty
}
private interface I1 {
// empty
}
private interface I2 {
// empty
}
private interface I3 extends I4, I5 {
// empty
}
private interface I4 {
// empty
}
private interface I5 extends I6 {
// empty
}
private interface I6 {
// empty
}
@Test
public void getAllInterfaces_shouldFindAllInterfacesImplementedByAClass() {
//此方法返回由给定类及其超类实现的所有接口。
final List<Class<?>> list = Snippets.getAllInterfaces(Class2.class);
assertThat(list).hasSize(6);
assertThat(list).containsExactly(I2.class, I3.class, I4.class, I5.class, I6.class, I1.class);
}
enum Priority {
High, Medium, Low
}
@Test
public void getEnumMap_convert_enum_to_map() throws Exception {
//将枚举转换为 Map,其中 key 是枚举名,value 是枚举本身。
Map<String, Priority> map = Snippets.getEnumMap(Priority.class);
assertThat(map).hasSize(3);
assertThat(map)
.containsOnly(
new SimpleEntry<>("High", Priority.High),
new SimpleEntry<>("Medium", Priority.Medium),
new SimpleEntry<>("Low", Priority.Low)
);
}
java8的一些小片段
于 2022-02-26 15:33:16 首次发布