Skip to content

Commit

Permalink
Number and string utils
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Mar 2, 2024
1 parent 12ebf27 commit b7c16b4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/reandroid/utils/NumbersUtil.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/reandroid/utils/StringsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

0 comments on commit b7c16b4

Please sign in to comment.