diff --git a/src/main/java/com/reandroid/utils/NumbersUtil.java b/src/main/java/com/reandroid/utils/NumbersUtil.java new file mode 100644 index 000000000..0d6ef66a3 --- /dev/null +++ b/src/main/java/com/reandroid/utils/NumbersUtil.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2022 github.com/REAndroid + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.reandroid.utils; + +public class NumbersUtil { + + public static int abs(int i){ + if(i < 0){ + i = -i; + } + return i; + } + public static int min(int i1, int i2){ + if(i1 < i2){ + return i1; + } + return i2; + } +} diff --git a/src/main/java/com/reandroid/utils/StringsUtil.java b/src/main/java/com/reandroid/utils/StringsUtil.java index 0ded987ca..f0ebe5b19 100644 --- a/src/main/java/com/reandroid/utils/StringsUtil.java +++ b/src/main/java/com/reandroid/utils/StringsUtil.java @@ -15,6 +15,8 @@ */ package com.reandroid.utils; +import com.reandroid.utils.collection.ArrayIterator; + import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -540,5 +542,39 @@ public static int compareStrings(String s1, String s2) { return CompareUtil.compare(s1, s2); } + public static String join(Iterable iterable, Object separator){ + return join(iterable.iterator(), separator); + } + public static String join(Object[] items, Object separator){ + return join(ArrayIterator.of(items), separator); + } + public static String join(Iterator iterator, Object separator){ + StringBuilder builder = new StringBuilder(); + boolean appendOnce = false; + while (iterator.hasNext()){ + if(appendOnce){ + builder.append(separator); + } + builder.append(iterator.next()); + appendOnce = true; + } + if(appendOnce){ + return builder.toString(); + } + return EMPTY; + } + public static int diffStart(String str1, String str2){ + if(isEmpty(str1) || isEmpty(str2) || str1.equals(str2)){ + return -1; + } + int length = NumbersUtil.min(str1.length(), str2.length()); + for(int i = 0; i < length; i++){ + if(str1.charAt(i) != str2.charAt(i)){ + return i; + } + } + return -1; + } + private static final int MAX_STRING_APPEND = 5; }