From 07f946e7108dc7792e3a364f360d3fdee4cf63e0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 16 Nov 2017 15:47:10 +0300 Subject: [PATCH] Add some useful JDK classes --- .../java/util/TEmptyStackException.java | 23 ++++++ .../java/util/TServiceConfigurationError.java | 26 +++++++ .../org/teavm/classlib/java/util/TStack.java | 72 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/TEmptyStackException.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/TServiceConfigurationError.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/TStack.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TEmptyStackException.java b/classlib/src/main/java/org/teavm/classlib/java/util/TEmptyStackException.java new file mode 100644 index 000000000..165c0b402 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TEmptyStackException.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 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.util; + +public class TEmptyStackException extends RuntimeException { + public TEmptyStackException() { + super(); + } +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TServiceConfigurationError.java b/classlib/src/main/java/org/teavm/classlib/java/util/TServiceConfigurationError.java new file mode 100644 index 000000000..aaa68202e --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TServiceConfigurationError.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 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.util; + +public class TServiceConfigurationError extends Error { + public TServiceConfigurationError(String message) { + super(message); + } + + public TServiceConfigurationError(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TStack.java b/classlib/src/main/java/org/teavm/classlib/java/util/TStack.java new file mode 100644 index 000000000..0499c01c3 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TStack.java @@ -0,0 +1,72 @@ +/* + * Copyright 2017 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.util; + +public class TStack extends TVector { + public TStack() { + super(); + } + + public boolean empty() { + return isEmpty(); + } + + @SuppressWarnings("unchecked") + public synchronized E peek() { + try { + return (E) elementData[elementCount - 1]; + } catch (IndexOutOfBoundsException e) { + throw new TEmptyStackException(); + } + } + + @SuppressWarnings("unchecked") + public synchronized E pop() { + if (elementCount == 0) { + throw new TEmptyStackException(); + } + final int index = --elementCount; + final E obj = (E) elementData[index]; + elementData[index] = null; + modCount++; + return obj; + } + + public E push(E object) { + addElement(object); + return object; + } + + public synchronized int search(Object o) { + final Object[] dumpArray = elementData; + final int size = elementCount; + if (o != null) { + for (int i = size - 1; i >= 0; i--) { + if (o.equals(dumpArray[i])) { + return size - i; + } + } + } else { + for (int i = size - 1; i >= 0; i--) { + if (dumpArray[i] == null) { + return size - i; + } + } + } + return -1; + } +}