Add some missing stdlib methods

This commit is contained in:
Alexey Andreev 2018-06-28 19:07:04 +03:00
parent fc13c10d98
commit d674b0b7de
3 changed files with 79 additions and 0 deletions

View File

@ -15,6 +15,7 @@
*/
package org.teavm.classlib.java.lang;
import java.util.Iterator;
import java.util.Locale;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.io.TUnsupportedEncodingException;
@ -666,4 +667,48 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
public static String format(Locale l, String format, Object... args) {
return new TFormatter(l).format(format, args).toString();
}
public static String join(CharSequence delimiter, CharSequence... elements) {
if (elements.length == 0) {
return "";
}
int resultLength = 0;
for (CharSequence element : elements) {
resultLength += element.length();
}
resultLength += elements.length * delimiter.length();
char[] chars = new char[resultLength];
int index = 0;
CharSequence firstElement = elements[0];
for (int i = 0; i < firstElement.length(); ++i) {
chars[index++] = firstElement.charAt(i);
}
for (int i = 1; i < elements.length; ++i) {
for (int j = 0; j < delimiter.length(); ++j) {
chars[index++] = delimiter.charAt(j);
}
CharSequence element = elements[i];
for (int j = 0; j < element.length(); ++j) {
chars[index++] = element.charAt(j);
}
}
return new String(chars);
}
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
Iterator<? extends CharSequence> iter = elements.iterator();
if (!iter.hasNext()) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(iter.next());
while (iter.hasNext()) {
sb.append(delimiter);
sb.append(iter.next());
}
return sb.toString();
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2018 Alexey Andreev.
*
* 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 org.teavm.classlib.java.nio.charset;
import org.teavm.classlib.java.nio.charset.impl.TUTF8Charset;
public final class TStandardCharsets {
private TStandardCharsets() {
}
public static final TCharset UTF_8 = new TUTF8Charset();
}

View File

@ -74,6 +74,15 @@ public interface TMap<K, V> {
}
}
default V putIfAbsent(K key, V value) {
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
V v = get(key);
if (v == null) {