├── .github ├── dependabot.yml └── workflows │ └── maven.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.adoc ├── RELEASE_PROCEDURE.adoc ├── SECURITY.md ├── build-release-17 ├── build-test-java17 ├── build-test-java21 ├── pom.xml └── src ├── main └── java │ ├── module-info.java │ └── org │ └── wildfly │ └── common │ ├── Assert.java │ ├── Branch.java │ ├── Factory.java │ ├── HostName.java │ ├── _private │ └── CommonMessages.java │ ├── annotation │ ├── NotNull.java │ └── Nullable.java │ ├── archive │ ├── Archive.java │ ├── ByteBufferInputStream.java │ ├── HugeIndex.java │ ├── Index.java │ ├── JDKSpecific.java │ ├── LargeIndex.java │ ├── TinyIndex.java │ └── package-info.java │ ├── array │ ├── ArrayIterator.java │ └── Arrays2.java │ ├── bytes │ ├── ByteStringBuilder.java │ └── package-info.java │ ├── codec │ ├── Alphabet.java │ ├── Base32Alphabet.java │ ├── Base64Alphabet.java │ ├── DecodeException.java │ └── package-info.java │ ├── context │ ├── ContextManager.java │ ├── ContextPermission.java │ ├── ContextPermissionCollection.java │ ├── Contextual.java │ └── package-info.java │ ├── cpu │ ├── CacheInfo.java │ ├── CacheLevelInfo.java │ ├── CacheType.java │ ├── ProcessorInfo.java │ └── package-info.java │ ├── expression │ ├── Expression.java │ ├── ResolveContext.java │ └── package-info.java │ ├── flags │ └── Flags.java │ ├── function │ ├── ExceptionBiConsumer.java │ ├── ExceptionBiFunction.java │ ├── ExceptionBiPredicate.java │ ├── ExceptionBinaryOperator.java │ ├── ExceptionConsumer.java │ ├── ExceptionFunction.java │ ├── ExceptionIntFunction.java │ ├── ExceptionLongFunction.java │ ├── ExceptionObjIntConsumer.java │ ├── ExceptionObjLongConsumer.java │ ├── ExceptionPredicate.java │ ├── ExceptionRunnable.java │ ├── ExceptionSupplier.java │ ├── ExceptionToIntBiFunction.java │ ├── ExceptionToIntFunction.java │ ├── ExceptionToLongBiFunction.java │ ├── ExceptionToLongFunction.java │ ├── ExceptionUnaryOperator.java │ ├── Functions.java │ ├── ThreadLocalStack.java │ └── package-info.java │ ├── iteration │ ├── Base16DecodingByteIterator.java │ ├── Base16EncodingCodePointIterator.java │ ├── Base32DecodingByteIterator.java │ ├── Base32EncodingCodePointIterator.java │ ├── Base64DecodingByteIterator.java │ ├── Base64EncodingIterator.java │ ├── BiDirIntIterator.java │ ├── BigEndianBase32DecodingByteIterator.java │ ├── BigEndianBase32EncodingIterator.java │ ├── BigEndianBase64DecodingByteIterator.java │ ├── BigEndianBase64EncodingIterator.java │ ├── ByteArrayIterator.java │ ├── ByteBufferIterator.java │ ├── ByteIterator.java │ ├── ByteIteratorInputStream.java │ ├── ByteTableTranslatingByteIterator.java │ ├── CharArrayIterator.java │ ├── CodePointIterator.java │ ├── CompositeIterable.java │ ├── CompositeIterator.java │ ├── ConcatByteIterator.java │ ├── DelimitedByteIterator.java │ ├── DelimitedCodePointIterator.java │ ├── EnumerationIterator.java │ ├── IndexIterator.java │ ├── IntIterator.java │ ├── IntTableTranslatingByteIterator.java │ ├── InterleavedByteArrayIterator.java │ ├── Latin1DecodingIterator.java │ ├── Latin1EncodingByteIterator.java │ ├── LimitedByteIterator.java │ ├── LimitedCodePointIterator.java │ ├── LittleEndianBase32DecodingByteIterator.java │ ├── LittleEndianBase32EncodingIterator.java │ ├── LittleEndianBase64DecodingByteIterator.java │ ├── LittleEndianBase64EncodingIterator.java │ ├── SkippingCodePointIterator.java │ ├── StringIterator.java │ ├── Utf8DecodingIterator.java │ ├── Utf8EncodingByteIterator.java │ └── package-info.java │ ├── lock │ ├── ExtendedLock.java │ ├── ExtendedReentrantLock.java │ ├── Locks.java │ ├── SpinLock.java │ └── package-info.java │ ├── math │ ├── HashMath.java │ └── package-info.java │ ├── net │ ├── CidrAddress.java │ ├── CidrAddressTable.java │ ├── HostName.java │ ├── Inet.java │ ├── URIs.java │ └── package-info.java │ ├── os │ ├── Process.java │ ├── ProcessRedirect.java │ └── package-info.java │ ├── package-info.java │ ├── ref │ ├── CleanerReference.java │ ├── Log.java │ ├── PhantomReference.java │ ├── Reapable.java │ ├── Reaper.java │ ├── Reference.java │ ├── References.java │ ├── SoftReference.java │ ├── StrongReference.java │ ├── WeakReference.java │ └── package-info.java │ ├── rpc │ ├── IdentityIntMap.java │ ├── RemoteExceptionCause.java │ └── package-info.java │ ├── selector │ ├── ContextClassLoaderSelector.java │ ├── DefaultSelector.java │ ├── GlobalSelector.java │ ├── Selector.java │ ├── SelectorPermission.java │ ├── ThreadLocalSelector.java │ └── package-info.java │ ├── string │ ├── CompositeCharSequence.java │ └── package-info.java │ └── xml │ ├── DocumentBuilderFactoryUtil.java │ ├── FactoryConstants.java │ ├── Log.java │ ├── SAXParserFactoryUtil.java │ ├── TransformerFactoryUtil.java │ ├── XMLInputFactoryUtil.java │ ├── XMLReaderFactoryUtil.java │ └── package-info.java └── test └── java └── org └── wildfly └── common ├── archive ├── ArchiveIndexTest.java └── ArchiveTest.java ├── codec ├── Base32Test.java ├── Base64Test.java └── HexTest.java ├── context └── ContextPermissionTestCase.java ├── iteration ├── CompositeIterableTestCase.java └── CompositeIteratorTestCase.java ├── lock └── SpinLockTest.java ├── math └── HashMathTestCase.java ├── net └── CidrAddressTableTest.java ├── rpc └── TestRemoteExceptionCause.java ├── string └── CompositeCharSequenceTestCase.java └── xml └── XmlFactoriesTest.java /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "maven" 4 | directory: "/" # Location of package manifests 5 | schedule: 6 | interval: "daily" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "daily" 11 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Build WildFly Common 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Check out project 15 | uses: actions/checkout@v4 16 | with: 17 | path: wildfly-common 18 | 19 | - name: Set up JDKs 20 | uses: actions/setup-java@v4 21 | with: 22 | distribution: 'temurin' 23 | cache: maven 24 | java-version: | 25 | 17 26 | 21 27 | 23 28 | 29 | - name: Build & Test with Maven 30 | run: | 31 | cd wildfly-common 32 | mvn -B -ntp -N install --file pom.xml -Djava17.home="${{env.JAVA_HOME_17_X64}}" -Djava21.home="${{env.JAVA_HOME_21_X64}}" 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore .svn metadata files 2 | .svn 3 | # ignore Maven generated target folders 4 | ~ 5 | target 6 | # ignore downloaded maven 7 | tools/maven 8 | # ignore eclipse files 9 | .project 10 | .classpath 11 | .settings 12 | .metadata 13 | # ignore m2e annotation processing files 14 | .factorypath 15 | # ignore IDEA files 16 | *.iml 17 | *.ipr 18 | *.iws 19 | .idea 20 | # ignore NetBeans files 21 | nbactions.xml 22 | nb-configuration.xml 23 | catalog.xml 24 | # 25 | maven-ant-tasks.jar 26 | test-output 27 | transaction.log 28 | # vim files 29 | *.swp 30 | /.gitk-tmp.* 31 | atlassian-ide-plugin.xml 32 | # temp files 33 | *~ 34 | # maven versions plugin 35 | pom.xml.versionsBackup 36 | # hprof dumps 37 | /*.hprof 38 | # ignore 'randomly' strewn around logs 39 | server.log 40 | # ignore java crashes 41 | hs_err_pid*.log 42 | # H2 databases produced by tests 43 | *.h2.db 44 | # JBoss transaction generated files 45 | PutObjectStoreDirHere 46 | # ignore mvn-rpmbuild repo 47 | /.m2 48 | # ignore eap repo 49 | local-repo-eap 50 | 51 | #These keep hanging around 52 | arquillian/*/server.log* 53 | client/shade/dependency-reduced-pom.xml 54 | 55 | #OS X stuff 56 | .DS_Store 57 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = WildFly Common 2 | 3 | Common utilities for WildFly projects. 4 | 5 | == Instructions 6 | 7 | Compile and test the project: 8 | 9 | [source,bash] 10 | ---- 11 | mvn verify 12 | ---- 13 | 14 | == Issue tracker 15 | 16 | https://github.com/wildfly/wildfly-common/issues 17 | 18 | (and https://issues.redhat.com/projects/WFCOM/summary for releases older than `2.0.0.Final`) -------------------------------------------------------------------------------- /RELEASE_PROCEDURE.adoc: -------------------------------------------------------------------------------- 1 | # Release Procedure 2 | 3 | WildFly Common is released in JBoss Nexus Repository 4 | 5 | To release a new version, follow the instructions: 6 | 7 | ``` 8 | git checkout --detach 9 | 10 | mvn versions:set -DnewVersion= 11 | git commit -am 'Prep ' 12 | git tag -a -m 13 | mvn clean install && mvn deploy -Pjboss-release -DaltDeploymentRepository=jboss-releases-repository::https://repository.jboss.org/nexus/service/local/staging/deploy/maven2 14 | ``` 15 | 16 | If the deployment to Nexus is successful, you can continue with: 17 | 18 | ``` 19 | git checkout main (or the release branch) 20 | mvn versions:set -DnewVersion=-SNAPSHOT 21 | git commit -am 'Next is ' 22 | git push upstream --dry-run 23 | ``` 24 | 25 | At this point, release the artifacts in Nexus. 26 | 27 | Finally, push the commits to GitHub: 28 | 29 | ``` 30 | git push upstream 31 | ``` 32 | 33 | Once the artifacts are in Nexus and the tag is pushed to GitHub, the last step is to create the Release Notes in https://github.com/wildfly/wildfly-common/releases[GitHub Releases] from the new tag. 34 | 35 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Security Contacts and Procedures 4 | 5 | The WildFly community takes security very seriously, and we aim to take immediate action to address serious security-related problems that involve our products or services. 6 | 7 | Please report any suspected security vulnerability in this project to Red Hat Product Security at secalert@redhat.com. You can use our GPG key to communicate with us securely. 8 | 9 | To report an issue in any Red Hat branded website or online service, please contact Red Hat Information Security at site-security@redhat.com. 10 | 11 | https://access.redhat.com/security/team/contact 12 | -------------------------------------------------------------------------------- /build-release-17: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wildfly/wildfly-common/301a540e81c28166dda5d22055ed90211b78ff3c/build-release-17 -------------------------------------------------------------------------------- /build-test-java17: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wildfly/wildfly-common/301a540e81c28166dda5d22055ed90211b78ff3c/build-test-java17 -------------------------------------------------------------------------------- /build-test-java21: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wildfly/wildfly-common/301a540e81c28166dda5d22055ed90211b78ff3c/build-test-java21 -------------------------------------------------------------------------------- /src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module org.wildfly.common { 2 | requires java.compiler; 3 | requires java.xml; 4 | requires java.transaction.xa; 5 | requires java.sql; 6 | 7 | requires io.smallrye.common.cpu; 8 | requires io.smallrye.common.expression; 9 | requires io.smallrye.common.net; 10 | requires io.smallrye.common.os; 11 | requires io.smallrye.common.ref; 12 | 13 | requires org.jboss.logging; 14 | 15 | requires static io.smallrye.common.function; 16 | 17 | requires static org.jboss.logging.annotations; 18 | } -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/Branch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common; 20 | 21 | /** 22 | * Branch probability directives. On JVMs which support these constructs, 23 | */ 24 | public final class Branch { 25 | private Branch() {} 26 | 27 | /** 28 | * Assert that the given expression is on the "fast path", which is to say, it will be {@code true} more than 99.9% 29 | * of the time. 30 | * 31 | * @param expr the expression value 32 | * @return the expression value 33 | */ 34 | public static boolean veryLikely(boolean expr) { 35 | return expr; 36 | } 37 | 38 | /** 39 | * Assert that the given expression is on the "slow path", which is to say, it will be {@code false} more than 99.9% 40 | * of the time. 41 | * 42 | * @param expr the expression value 43 | * @return the expression value 44 | */ 45 | public static boolean veryUnlikely(boolean expr) { 46 | return expr; 47 | } 48 | 49 | /** 50 | * Assert that the given expression is likely, which is to say, it will be {@code true} more than 75% 51 | * of the time. 52 | * 53 | * @param expr the expression value 54 | * @return the expression value 55 | */ 56 | public static boolean likely(boolean expr) { 57 | return expr; 58 | } 59 | 60 | /** 61 | * Assert that the given expression is unlikely, which is to say, it will be {@code false} more than 75% 62 | * of the time. 63 | * 64 | * @param expr the expression value 65 | * @return the expression value 66 | */ 67 | public static boolean unlikely(boolean expr) { 68 | return expr; 69 | } 70 | 71 | /** 72 | * Assert that the given expression has the given probability of being {@code true}. 73 | * 74 | * @param prob the probability where {@code 0.0f ≤ prob ≤ 1.0f} 75 | * @param expr the expression value 76 | * @return the expression value 77 | */ 78 | public static boolean probability(float prob, boolean expr) { 79 | assert 0.0f <= prob && prob <= 1.0f; 80 | return expr; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/Factory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common; 20 | 21 | import java.util.function.Supplier; 22 | 23 | /** 24 | * An object which produces another object. 25 | * 26 | * @author David M. Lloyd 27 | * @see Supplier 28 | */ 29 | public interface Factory { 30 | 31 | /** 32 | * Create the object. 33 | * 34 | * @return the object 35 | */ 36 | T create(); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/HostName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common; 20 | 21 | /** 22 | * @deprecated Please use {@link org.wildfly.common.net.HostName} instead. 23 | */ 24 | public final class HostName { 25 | 26 | /** 27 | * @deprecated Please use {@link org.wildfly.common.net.HostName#getHostName()} instead. 28 | */ 29 | public static String getHostName() { 30 | return org.wildfly.common.net.HostName.getHostName(); 31 | } 32 | 33 | /** 34 | * @deprecated Please use {@link org.wildfly.common.net.HostName#getQualifiedHostName()} instead. 35 | */ 36 | public static String getQualifiedHostName() { 37 | return org.wildfly.common.net.HostName.getQualifiedHostName(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/annotation/NotNull.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.annotation; 20 | 21 | import java.lang.annotation.Documented; 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * An advisory annotation indicating that the given variable, method, or parameter is not nullable. 29 | * 30 | * @author David M. Lloyd 31 | */ 32 | @Retention(RetentionPolicy.CLASS) 33 | @Target({ ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.PARAMETER }) 34 | @Documented 35 | public @interface NotNull { 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/annotation/Nullable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.annotation; 20 | 21 | import java.lang.annotation.Documented; 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * An advisory annotation indicating that the given variable, method, or parameter is nullable. 29 | * 30 | * @author David M. Lloyd 31 | */ 32 | @Retention(RetentionPolicy.CLASS) 33 | @Target({ ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.PARAMETER }) 34 | @Documented 35 | public @interface Nullable { 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/ByteBufferInputStream.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.archive; 2 | 3 | import java.io.InputStream; 4 | import java.nio.ByteBuffer; 5 | 6 | final class ByteBufferInputStream extends InputStream { 7 | private final ByteBuffer[] bufs; 8 | private final long offset; 9 | private final long size; 10 | long pos; 11 | long mark; 12 | 13 | ByteBufferInputStream(final ByteBuffer[] bufs, final long offset, final long size) { 14 | this.bufs = bufs; 15 | this.offset = offset; 16 | this.size = size; 17 | } 18 | 19 | public int read() { 20 | return pos < size ? Archive.getByte(bufs, offset + pos++) : -1; 21 | } 22 | 23 | public int read(final byte[] b) { 24 | return read(b, 0, b.length); 25 | } 26 | 27 | public int read(final byte[] b, final int off, final int len) { 28 | final long rem = size - pos; 29 | if (rem == 0) return -1; 30 | final int realLen = (int) Math.min(len, rem); 31 | if (realLen > 0) { 32 | Archive.readBytes(bufs, offset + pos, b, off, realLen); 33 | return realLen; 34 | } else { 35 | return 0; 36 | } 37 | } 38 | 39 | public long skip(final long n) { 40 | final long rem = size - pos; 41 | final long cnt = Math.min(rem, n); 42 | if (cnt > 0) { 43 | pos += cnt; 44 | return cnt; 45 | } else { 46 | return 0; 47 | } 48 | } 49 | 50 | public int available() { 51 | return (int) Math.min(Integer.MAX_VALUE, size - pos); 52 | } 53 | 54 | public void close() { 55 | } 56 | 57 | public void mark(final int readLimit) { 58 | mark = pos; 59 | } 60 | 61 | public void reset() { 62 | pos = mark; 63 | } 64 | 65 | public boolean markSupported() { 66 | return true; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/HugeIndex.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.archive; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * An index for archives greater than 4GB in size. 7 | */ 8 | final class HugeIndex extends Index { 9 | private final long[] table; 10 | 11 | HugeIndex(final int entries) { 12 | super(entries); 13 | final long[] array = new long[size()]; 14 | Arrays.fill(array, -1); 15 | this.table = array; 16 | } 17 | 18 | long get(final int index) { 19 | return table[index]; 20 | } 21 | 22 | void put(int index, final long offset) { 23 | final long[] table = this.table; 24 | long val = table[index]; 25 | while (val != -1L) { 26 | index = index + 1 & getMask(); 27 | val = table[index]; 28 | } 29 | table[index] = offset; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/Index.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.archive; 2 | 3 | /** 4 | */ 5 | abstract class Index { 6 | final int tableSize; 7 | 8 | Index(final int entries) { 9 | if (entries >= 1 << 30) { 10 | throw new IllegalStateException("Index is too large"); 11 | } 12 | this.tableSize = Integer.highestOneBit(entries << 2); 13 | } 14 | 15 | final int size() { 16 | return tableSize; 17 | } 18 | 19 | abstract long get(int index); 20 | 21 | abstract void put(int index, long offset); 22 | 23 | int getMask() { 24 | return tableSize - 1; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/JDKSpecific.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.archive; 2 | 3 | import static java.lang.Math.min; 4 | 5 | import java.io.IOException; 6 | import java.nio.ByteBuffer; 7 | import java.util.zip.DataFormatException; 8 | import java.util.zip.Inflater; 9 | 10 | /** 11 | */ 12 | final class JDKSpecific { 13 | private JDKSpecific() {} 14 | 15 | static ByteBuffer inflate(final Inflater inflater, final ByteBuffer[] bufs, long offset, final int compSize, final int uncompSize) throws DataFormatException, IOException { 16 | int cnt = 0; 17 | byte[] out = new byte[uncompSize]; 18 | int op = 0; 19 | while (cnt < compSize) { 20 | int rem = compSize - cnt; 21 | final ByteBuffer buf = bufs[Archive.bufIdx(offset + cnt)].duplicate(); 22 | buf.position(Archive.bufOffs(offset + cnt)); 23 | buf.limit(min(buf.capacity(), buf.position() + rem)); 24 | cnt += buf.remaining(); 25 | inflater.setInput(buf); 26 | do { 27 | op += inflater.inflate(out, op, uncompSize - op); 28 | } while (! inflater.needsInput()); 29 | } 30 | if (! inflater.finished()) { 31 | throw new IOException("Corrupted compression stream"); 32 | } 33 | return ByteBuffer.wrap(out); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/LargeIndex.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.archive; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * An index for archives between 64KB and 4GB in size. 7 | */ 8 | final class LargeIndex extends Index { 9 | private final int[] table; 10 | 11 | LargeIndex(final int entries) { 12 | super(entries); 13 | final int[] array = new int[size()]; 14 | Arrays.fill(array, -1); 15 | this.table = array; 16 | } 17 | 18 | long get(final int index) { 19 | final int val = table[index]; 20 | return val == -1 ? -1 : val & 0xffffffff; 21 | } 22 | 23 | void put(int index, final long offset) { 24 | final int[] table = this.table; 25 | int val = table[index]; 26 | while (val != -1L) { 27 | index = index + 1 & getMask(); 28 | val = table[index]; 29 | } 30 | table[index] = Math.toIntExact(offset); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/TinyIndex.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.archive; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * An index for archives less than 64KB in size. 7 | */ 8 | final class TinyIndex extends Index { 9 | private final short[] table; 10 | 11 | TinyIndex(final int entries) { 12 | super(entries); 13 | final short[] array = new short[size()]; 14 | Arrays.fill(array, (short) -1); 15 | this.table = array; 16 | } 17 | 18 | long get(final int index) { 19 | final int val = table[index]; 20 | return val == -1 ? -1 : val & 0xffff; 21 | } 22 | 23 | void put(int index, final long offset) { 24 | final short[] table = this.table; 25 | int val = table[index]; 26 | while (val != -1L) { 27 | index = index + 1 & getMask(); 28 | val = table[index]; 29 | } 30 | table[index] = (short) offset; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/archive/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes for dealing with Zip and JAR format archives. 3 | */ 4 | package org.wildfly.common.archive; 5 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/bytes/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes related to the manipulation of byte sequences. 21 | */ 22 | package org.wildfly.common.bytes; 23 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/codec/Alphabet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.codec; 20 | 21 | /** 22 | * A base-n encoder/decoder alphabet. Alphabets may be little-endian or big-endian. Each base has its own subclass. 23 | */ 24 | public abstract class Alphabet { 25 | private final boolean littleEndian; 26 | 27 | Alphabet(final boolean littleEndian) { 28 | this.littleEndian = littleEndian; 29 | } 30 | 31 | /** 32 | * Determine whether this is a little-endian or big-endian alphabet. 33 | * 34 | * @return {@code true} if the alphabet is little-endian, {@code false} if it is big-endian 35 | */ 36 | public boolean isLittleEndian() { 37 | return littleEndian; 38 | } 39 | 40 | /** 41 | * Encode the given byte value to a code point. 42 | * 43 | * @param val the value 44 | * @return the Unicode code point 45 | */ 46 | public abstract int encode(int val); 47 | 48 | /** 49 | * Decode the given code point (character). If the code point is not valid, -1 is returned. 50 | * 51 | * @param codePoint the Unicode code point 52 | * @return the decoded value or -1 if the code point is not valid 53 | */ 54 | public abstract int decode(int codePoint); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/codec/Base64Alphabet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.codec; 20 | 21 | import org.wildfly.common.iteration.ByteIterator; 22 | import org.wildfly.common.iteration.CodePointIterator; 23 | 24 | /** 25 | * A base-64 alphabet. 26 | * 27 | * @see ByteIterator#base64Encode(Base64Alphabet) 28 | * @see CodePointIterator#base64Decode(Base64Alphabet) 29 | */ 30 | public abstract class Base64Alphabet extends Alphabet { 31 | 32 | /** 33 | * Construct a new instance. 34 | * 35 | * @param littleEndian {@code true} if the alphabet is little-endian (LSB first), {@code false} otherwise 36 | */ 37 | protected Base64Alphabet(final boolean littleEndian) { 38 | super(littleEndian); 39 | } 40 | 41 | /** 42 | * Encode the given 6-bit value to a code point. 43 | * 44 | * @param val the 6-bit value 45 | * @return the Unicode code point 46 | */ 47 | public abstract int encode(int val); 48 | 49 | /** 50 | * Decode the given code point. If the code point is not valid, -1 is returned. 51 | * 52 | * @param codePoint the code point 53 | * @return the decoded 6-bit value or -1 if the code point is not valid 54 | */ 55 | public abstract int decode(int codePoint); 56 | 57 | /** 58 | * The standard RFC 4648 base-64 alphabet. 59 | */ 60 | public static final Base64Alphabet STANDARD = new Base64Alphabet(false) { 61 | public int encode(final int val) { 62 | if (val <= 25) { 63 | return 'A' + val; 64 | } else if (val <= 51) { 65 | return 'a' + val - 26; 66 | } else if (val <= 61) { 67 | return '0' + val - 52; 68 | } else if (val == 62) { 69 | return '+'; 70 | } else { 71 | assert val == 63; 72 | return '/'; 73 | } 74 | } 75 | 76 | public int decode(final int codePoint) throws IllegalArgumentException { 77 | if ('A' <= codePoint && codePoint <= 'Z') { 78 | return codePoint - 'A'; 79 | } else if ('a' <= codePoint && codePoint <= 'z') { 80 | return codePoint - 'a' + 26; 81 | } else if ('0' <= codePoint && codePoint <= '9') { 82 | return codePoint - '0' + 52; 83 | } else if (codePoint == '+') { 84 | return 62; 85 | } else if (codePoint == '/') { 86 | return 63; 87 | } else { 88 | return -1; 89 | } 90 | } 91 | }; 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/codec/DecodeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.codec; 20 | 21 | /** 22 | * An exception which indicates that decoding has failed due to invalid or truncated input. 23 | */ 24 | public class DecodeException extends IllegalArgumentException { 25 | 26 | private static final long serialVersionUID = 5823281980783313991L; 27 | 28 | /** 29 | * Constructs a new {@code DecodeException} instance. The message is left blank ({@code null}), and no cause is 30 | * specified. 31 | */ 32 | public DecodeException() { 33 | } 34 | 35 | /** 36 | * Constructs a new {@code DecodeException} instance with an initial message. No cause is specified. 37 | * 38 | * @param msg the message 39 | */ 40 | public DecodeException(final String msg) { 41 | super(msg); 42 | } 43 | 44 | /** 45 | * Constructs a new {@code DecodeException} instance with an initial cause. If a non-{@code null} cause is 46 | * specified, its message is used to initialize the message of this {@code DecodeException}; otherwise the message 47 | * is left blank ({@code null}). 48 | * 49 | * @param cause the cause 50 | */ 51 | public DecodeException(final Throwable cause) { 52 | super(cause); 53 | } 54 | 55 | /** 56 | * Constructs a new {@code DecodeException} instance with an initial message and cause. 57 | * 58 | * @param msg the message 59 | * @param cause the cause 60 | */ 61 | public DecodeException(final String msg, final Throwable cause) { 62 | super(msg, cause); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/codec/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes relating to encoding and decoding. 21 | */ 22 | package org.wildfly.common.codec; 23 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/context/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Types related to management of contextual objects. Such objects may implement {@link org.wildfly.common.context.Contextual Contextual} 21 | * to gain thread-contextual functionality. 22 | * 23 | * @author David M. Lloyd 24 | */ 25 | package org.wildfly.common.context; 26 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/cpu/CacheLevelInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.cpu; 20 | 21 | /** 22 | * @author David M. Lloyd 23 | * 24 | * @deprecated Use {@link io.smallrye.common.cpu.CacheLevelInfo} instead. 25 | */ 26 | @Deprecated(forRemoval = true) 27 | public final class CacheLevelInfo { 28 | private final io.smallrye.common.cpu.CacheLevelInfo info; 29 | 30 | CacheLevelInfo(final io.smallrye.common.cpu.CacheLevelInfo info) { 31 | this.info = info; 32 | } 33 | 34 | /** 35 | * Get the level index. For example, the level of L1 cache will be "1", L2 will be "2", etc. If the level is 36 | * not known, 0 is returned. 37 | * 38 | * @return the level index, or 0 if unknown 39 | */ 40 | public int getCacheLevel() { 41 | return info.getCacheLevel(); 42 | } 43 | 44 | /** 45 | * Get the type of cache. If the type is unknown, {@link CacheType#UNKNOWN} is returned. 46 | * 47 | * @return the type of cache (not {@code null}) 48 | */ 49 | public CacheType getCacheType() { 50 | return CacheType.of(info.getCacheType()); 51 | } 52 | 53 | /** 54 | * Get the size of this cache level in kilobytes. If the size is unknown, 0 is returned. 55 | * 56 | * @return the size of this cache level in kilobytes, or 0 if unknown 57 | */ 58 | public int getCacheLevelSizeKB() { 59 | return info.getCacheLevelSizeKB(); 60 | } 61 | 62 | /** 63 | * Get the cache line size in bytes. If the size is unknown, 0 is returned. 64 | * 65 | * @return the cache line size in bytes, or 0 if unknown 66 | */ 67 | public int getCacheLineSize() { 68 | return info.getCacheLineSize(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/cpu/ProcessorInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.cpu; 20 | 21 | /** 22 | * Provides general information about the processors on this host (Java 9 version). 23 | * 24 | * @deprecated Use {@link io.smallrye.common.cpu.ProcessorInfo} instead. 25 | */ 26 | @Deprecated(forRemoval = true) 27 | public class ProcessorInfo { 28 | private ProcessorInfo() { 29 | } 30 | 31 | /** 32 | * {@return the number of available processors} 33 | * @deprecated Use {@link io.smallrye.common.cpu.ProcessorInfo#availableProcessors()} instead. 34 | */ 35 | @Deprecated(forRemoval = true) 36 | public static int availableProcessors() { 37 | return io.smallrye.common.cpu.ProcessorInfo.availableProcessors(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/cpu/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * A set of utility classes to determine information about the runtime environment's CPU configuration. 21 | * 22 | * @author David M. Lloyd 23 | * @deprecated Use {@link io.smallrye.common.cpu} instead. 24 | */ 25 | @Deprecated(forRemoval = true) 26 | package org.wildfly.common.cpu; 27 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/expression/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Expression string parsing and expansion classes. 21 | * 22 | * @author David M. Lloyd 23 | * 24 | * @deprecated Use {@link io.smallrye.common.expression} instead. 25 | */ 26 | @Deprecated(forRemoval = true) 27 | package org.wildfly.common.expression; 28 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionBiConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument consumer which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionBiConsumer { 30 | /** 31 | * Performs this operation on the given arguments. 32 | * 33 | * @param t the first argument 34 | * @param u the second argument 35 | * @throws E if an exception occurs 36 | */ 37 | void accept(T t, U u) throws E; 38 | 39 | default ExceptionBiConsumer andThen(ExceptionBiConsumer after) { 40 | Assert.checkNotNullParam("after", after); 41 | return (t, u) -> { 42 | accept(t, u); 43 | after.accept(t, u); 44 | }; 45 | } 46 | 47 | default ExceptionRunnable compose(ExceptionSupplier before1, ExceptionSupplier before2) { 48 | Assert.checkNotNullParam("before1", before1); 49 | Assert.checkNotNullParam("before2", before2); 50 | return () -> accept(before1.get(), before2.get()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionBiFunction { 30 | /** 31 | * Applies this function to the given arguments. 32 | * 33 | * @param t the first argument 34 | * @param u the second argument 35 | * @return the function result 36 | * @throws E if an exception occurs 37 | */ 38 | R apply(T t, U u) throws E; 39 | 40 | default ExceptionBiFunction andThen(ExceptionFunction after) { 41 | Assert.checkNotNullParam("after", after); 42 | return (t, u) -> after.apply(apply(t, u)); 43 | } 44 | 45 | default ExceptionBiConsumer andThen(ExceptionConsumer after) { 46 | Assert.checkNotNullParam("after", after); 47 | return (t, u) -> after.accept(apply(t, u)); 48 | } 49 | 50 | default ExceptionSupplier compose(ExceptionSupplier before1, ExceptionSupplier before2) { 51 | Assert.checkNotNullParam("before1", before1); 52 | Assert.checkNotNullParam("before2", before2); 53 | return () -> apply(before1.get(), before2.get()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionBiPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | /** 22 | * A two-argument predicate which can throw an exception. 23 | * 24 | * @author David M. Lloyd 25 | */ 26 | @FunctionalInterface 27 | public interface ExceptionBiPredicate { 28 | /** 29 | * Evaluate this predicate on the given arguments. 30 | * 31 | * @param t the first argument 32 | * @param u the second argument 33 | * @return {@code true} if the predicate passes, {@code false} otherwise 34 | * @throws E if an exception occurs 35 | */ 36 | boolean test(T t, U u) throws E; 37 | 38 | default ExceptionBiPredicate and(ExceptionBiPredicate other) { 39 | return (t, u) -> test(t, u) && other.test(t, u); 40 | } 41 | 42 | default ExceptionBiPredicate or(ExceptionBiPredicate other) { 43 | return (t, u) -> test(t, u) || other.test(t, u); 44 | } 45 | 46 | default ExceptionBiPredicate xor(ExceptionBiPredicate other) { 47 | return (t, u) -> test(t, u) != other.test(t, u); 48 | } 49 | 50 | default ExceptionBiPredicate not() { 51 | return (t, u) -> !test(t, u); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionBinaryOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A binary operator which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionBinaryOperator extends ExceptionBiFunction { 30 | 31 | default ExceptionBinaryOperator andThen(ExceptionUnaryOperator after) { 32 | Assert.checkNotNullParam("after", after); 33 | return (t, u) -> after.apply(apply(t, u)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A one-argument consumer which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionConsumer { 30 | /** 31 | * Performs this operation on the given argument. 32 | * 33 | * @param t the argument 34 | * @throws E if an exception occurs 35 | */ 36 | void accept(T t) throws E; 37 | 38 | default ExceptionConsumer andThen(ExceptionConsumer after) { 39 | Assert.checkNotNullParam("after", after); 40 | return t -> { 41 | accept(t); 42 | after.accept(t); 43 | }; 44 | } 45 | 46 | default ExceptionConsumer compose(ExceptionConsumer before) { 47 | Assert.checkNotNullParam("before", before); 48 | return t -> { 49 | accept(t); 50 | before.accept(t); 51 | }; 52 | } 53 | 54 | default ExceptionRunnable compose(ExceptionSupplier before) { 55 | Assert.checkNotNullParam("before", before); 56 | return () -> accept(before.get()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A one-argument function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionFunction { 30 | /** 31 | * Applies this function to the given arguments. 32 | * 33 | * @param t the argument 34 | * @return the function result 35 | * @throws E if an exception occurs 36 | */ 37 | R apply(T t) throws E; 38 | 39 | default ExceptionFunction andThen(ExceptionFunction after) { 40 | Assert.checkNotNullParam("after", after); 41 | return t -> after.apply(apply(t)); 42 | } 43 | 44 | default ExceptionFunction andThen(ExceptionBiFunction after) { 45 | Assert.checkNotNullParam("after", after); 46 | return t -> after.apply(t, apply(t)); 47 | } 48 | 49 | default ExceptionFunction compose(ExceptionFunction before) { 50 | Assert.checkNotNullParam("before", before); 51 | return t -> apply(before.apply(t)); 52 | } 53 | 54 | default ExceptionConsumer andThen(ExceptionConsumer after) { 55 | Assert.checkNotNullParam("after", after); 56 | return t -> after.accept(apply(t)); 57 | } 58 | 59 | default ExceptionConsumer andThen(ExceptionBiConsumer after) { 60 | Assert.checkNotNullParam("after", after); 61 | return t -> after.accept(t, apply(t)); 62 | } 63 | 64 | default ExceptionPredicate andThen(ExceptionPredicate after) { 65 | Assert.checkNotNullParam("after", after); 66 | return t -> after.test(apply(t)); 67 | } 68 | 69 | default ExceptionPredicate andThen(ExceptionBiPredicate after) { 70 | Assert.checkNotNullParam("after", after); 71 | return t -> after.test(t, apply(t)); 72 | } 73 | 74 | default ExceptionSupplier compose(ExceptionSupplier before) { 75 | Assert.checkNotNullParam("before", before); 76 | return () -> apply(before.get()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionIntFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A one-argument integer function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionIntFunction { 30 | /** 31 | * Applies this function to the given arguments. 32 | * 33 | * @param value the argument 34 | * @return the function result 35 | * @throws E if an exception occurs 36 | */ 37 | R apply(int value) throws E; 38 | 39 | default ExceptionIntFunction andThen(ExceptionFunction after) { 40 | Assert.checkNotNullParam("after", after); 41 | return t -> after.apply(apply(t)); 42 | } 43 | 44 | default ExceptionFunction compose(ExceptionToIntFunction before) { 45 | Assert.checkNotNullParam("before", before); 46 | return t -> apply(before.apply(t)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionLongFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A one-argument long integer function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionLongFunction { 30 | /** 31 | * Applies this function to the given arguments. 32 | * 33 | * @param value the argument 34 | * @return the function result 35 | * @throws E if an exception occurs 36 | */ 37 | R apply(long value) throws E; 38 | 39 | default ExceptionLongFunction andThen(ExceptionFunction after) { 40 | Assert.checkNotNullParam("after", after); 41 | return t -> after.apply(apply(t)); 42 | } 43 | 44 | default ExceptionFunction compose(ExceptionToLongFunction before) { 45 | Assert.checkNotNullParam("before", before); 46 | return t -> apply(before.apply(t)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionObjIntConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument object and integer consumer which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionObjIntConsumer { 30 | /** 31 | * Performs this operation on the given arguments. 32 | * 33 | * @param t the first argument 34 | * @param value the second argument 35 | * @throws E if an exception occurs 36 | */ 37 | void accept(T t, int value) throws E; 38 | 39 | default ExceptionObjIntConsumer andThen(ExceptionObjIntConsumer after) { 40 | Assert.checkNotNullParam("after", after); 41 | return (t, v) -> { 42 | accept(t, v); 43 | after.accept(t, v); 44 | }; 45 | } 46 | 47 | default ExceptionObjIntConsumer compose(ExceptionObjIntConsumer before) { 48 | Assert.checkNotNullParam("before", before); 49 | return (t, v) -> { 50 | before.accept(t, v); 51 | accept(t, v); 52 | }; 53 | } 54 | 55 | default ExceptionObjIntConsumer andThen(ExceptionObjLongConsumer after) { 56 | Assert.checkNotNullParam("after", after); 57 | return (t, v) -> { 58 | accept(t, v); 59 | after.accept(t, v); 60 | }; 61 | } 62 | 63 | default ExceptionObjIntConsumer compose(ExceptionObjLongConsumer before) { 64 | Assert.checkNotNullParam("before", before); 65 | return (t, v) -> { 66 | before.accept(t, v); 67 | accept(t, v); 68 | }; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionObjLongConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument object and long integer consumer which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionObjLongConsumer { 30 | /** 31 | * Performs this operation on the given arguments. 32 | * 33 | * @param t the first argument 34 | * @param value the second argument 35 | * @throws E if an exception occurs 36 | */ 37 | void accept(T t, long value) throws E; 38 | 39 | default ExceptionObjLongConsumer andThen(ExceptionObjLongConsumer after) { 40 | Assert.checkNotNullParam("after", after); 41 | return (t, v) -> { 42 | accept(t, v); 43 | after.accept(t, v); 44 | }; 45 | } 46 | 47 | default ExceptionObjLongConsumer compose(ExceptionObjLongConsumer before) { 48 | Assert.checkNotNullParam("before", before); 49 | return (t, v) -> { 50 | before.accept(t, v); 51 | accept(t, v); 52 | }; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | /** 22 | * A one-argument predicate which can throw an exception. 23 | * 24 | * @author David M. Lloyd 25 | */ 26 | @FunctionalInterface 27 | public interface ExceptionPredicate { 28 | /** 29 | * Evaluate this predicate on the given arguments. 30 | * 31 | * @param t the first argument 32 | * @return {@code true} if the predicate passes, {@code false} otherwise 33 | * @throws E if an exception occurs 34 | */ 35 | boolean test(T t) throws E; 36 | 37 | default ExceptionPredicate and(ExceptionPredicate other) { 38 | return t -> test(t) && other.test(t); 39 | } 40 | 41 | default ExceptionPredicate or(ExceptionPredicate other) { 42 | return t -> test(t) || other.test(t); 43 | } 44 | 45 | default ExceptionPredicate xor(ExceptionPredicate other) { 46 | return t -> test(t) != other.test(t); 47 | } 48 | 49 | default ExceptionPredicate not() { 50 | return t -> !test(t); 51 | } 52 | 53 | default ExceptionBiPredicate with(ExceptionPredicate other) { 54 | return (t, u) -> test(t) && other.test(u); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * An operation that can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | public interface ExceptionRunnable { 29 | /** 30 | * Run the operation. 31 | * 32 | * @throws E if an exception occurs 33 | */ 34 | void run() throws E; 35 | 36 | default ExceptionRunnable andThen(ExceptionRunnable after) { 37 | Assert.checkNotNullParam("after", after); 38 | return () -> { 39 | run(); 40 | after.run(); 41 | }; 42 | } 43 | 44 | default ExceptionRunnable compose(ExceptionRunnable before) { 45 | Assert.checkNotNullParam("before", before); 46 | return () -> { 47 | before.run(); 48 | run(); 49 | }; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A supplier which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionSupplier { 30 | /** 31 | * Gets a result. 32 | * 33 | * @return the result 34 | * @throws E if an exception occurs 35 | */ 36 | T get() throws E; 37 | 38 | default ExceptionRunnable andThen(ExceptionConsumer after) { 39 | Assert.checkNotNullParam("after", after); 40 | return () -> after.accept(get()); 41 | } 42 | 43 | default ExceptionSupplier andThen(ExceptionFunction after) { 44 | Assert.checkNotNullParam("after", after); 45 | return () -> after.apply(get()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionToIntBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionToIntBiFunction { 30 | /** 31 | * Apply this function to the given arguments. 32 | * 33 | * @param t the first argument 34 | * @param u the second argument 35 | * @return the function result 36 | * @throws E if an exception occurs 37 | */ 38 | int apply(T t, U u) throws E; 39 | 40 | default ExceptionBiFunction andThen(ExceptionIntFunction after) { 41 | Assert.checkNotNullParam("after", after); 42 | return (t, u) -> after.apply(apply(t, u)); 43 | } 44 | 45 | default ExceptionBiFunction andThen(ExceptionLongFunction after) { 46 | Assert.checkNotNullParam("after", after); 47 | return (t, u) -> after.apply(apply(t, u)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionToIntFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionToIntFunction { 30 | /** 31 | * Apply this function to the given arguments. 32 | * 33 | * @param t the first argument 34 | * @return the function result 35 | * @throws E if an exception occurs 36 | */ 37 | int apply(T t) throws E; 38 | 39 | default ExceptionFunction andThen(ExceptionIntFunction after) { 40 | Assert.checkNotNullParam("after", after); 41 | return t -> after.apply(apply(t)); 42 | } 43 | 44 | default ExceptionFunction andThen(ExceptionLongFunction after) { 45 | Assert.checkNotNullParam("after", after); 46 | return t -> after.apply(apply(t)); 47 | } 48 | 49 | default ExceptionToIntFunction compose(ExceptionFunction before) { 50 | Assert.checkNotNullParam("before", before); 51 | return t -> apply(before.apply(t)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionToLongBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionToLongBiFunction { 30 | /** 31 | * Apply this function to the given arguments. 32 | * 33 | * @param t the first argument 34 | * @param u the second argument 35 | * @return the function result 36 | * @throws E if an exception occurs 37 | */ 38 | long apply(T t, U u) throws E; 39 | 40 | default ExceptionBiFunction andThen(ExceptionLongFunction after) { 41 | Assert.checkNotNullParam("after", after); 42 | return (t, u) -> after.apply(apply(t, u)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionToLongFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A two-argument function which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionToLongFunction { 30 | /** 31 | * Apply this function to the given arguments. 32 | * 33 | * @param t the first argument 34 | * @return the function result 35 | * @throws E if an exception occurs 36 | */ 37 | long apply(T t) throws E; 38 | 39 | default ExceptionFunction andThen(ExceptionLongFunction after) { 40 | Assert.checkNotNullParam("after", after); 41 | return t -> after.apply(apply(t)); 42 | } 43 | 44 | default ExceptionToLongFunction compose(ExceptionFunction before) { 45 | Assert.checkNotNullParam("before", before); 46 | return t -> apply(before.apply(t)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ExceptionUnaryOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.function; 20 | 21 | import org.wildfly.common.Assert; 22 | 23 | /** 24 | * A unary operator which can throw an exception. 25 | * 26 | * @author David M. Lloyd 27 | */ 28 | @FunctionalInterface 29 | public interface ExceptionUnaryOperator extends ExceptionFunction { 30 | default ExceptionUnaryOperator andThen(ExceptionUnaryOperator after) { 31 | Assert.checkNotNullParam("after", after); 32 | return t -> after.apply(apply(t)); 33 | } 34 | 35 | default ExceptionUnaryOperator compose(ExceptionUnaryOperator before) { 36 | Assert.checkNotNullParam("before", before); 37 | return t -> apply(before.apply(t)); 38 | } 39 | 40 | static ExceptionUnaryOperator of(ExceptionFunction func) { 41 | return func instanceof ExceptionUnaryOperator ? (ExceptionUnaryOperator) func : func::apply; 42 | } 43 | 44 | static ExceptionUnaryOperator identity() { 45 | return t -> t; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/ThreadLocalStack.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.function; 2 | 3 | import java.util.ArrayDeque; 4 | import java.util.Deque; 5 | 6 | /** 7 | * A thread local stack data structure. In order to avoid memory churn the underlying 8 | * ArrayDeque is never freed. If we remove the deque when it is empty then this results 9 | * in excessive deque allocations. 10 | */ 11 | public class ThreadLocalStack { 12 | 13 | private static final Object NULL_VALUE = new Object(); 14 | 15 | private final ThreadLocal> deque = ThreadLocal.withInitial(ArrayDeque::new); 16 | 17 | public void push(E item) { 18 | Deque st = deque.get(); 19 | if(item == null) { 20 | st.push(NULL_VALUE); 21 | } else { 22 | st.push(item); 23 | } 24 | } 25 | 26 | public E peek() { 27 | Deque st = deque.get(); 28 | Object o = st.peek(); 29 | if(o == NULL_VALUE) { 30 | return null; 31 | } else { 32 | return (E) o; 33 | } 34 | } 35 | 36 | public E pop() { 37 | Deque st = deque.get(); 38 | Object o = st.pop(); 39 | if(o == NULL_VALUE) { 40 | return null; 41 | } else { 42 | return (E) o; 43 | } 44 | } 45 | 46 | public boolean isEmpty() { 47 | return deque.get().isEmpty(); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/function/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2016 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes which implement useful functions which are missing from {@code java.util.function}. 21 | */ 22 | package org.wildfly.common.function; 23 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/Base16DecodingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import static org.wildfly.common._private.CommonMessages.msg; 22 | 23 | import java.util.NoSuchElementException; 24 | 25 | /** 26 | */ 27 | final class Base16DecodingByteIterator extends ByteIterator { 28 | private final CodePointIterator iter; 29 | private int b; 30 | private long offset; 31 | private boolean havePair; 32 | 33 | Base16DecodingByteIterator(final CodePointIterator iter) { 34 | this.iter = iter; 35 | } 36 | 37 | private int calc(final int b0, final int b1) { 38 | int d0 = Character.digit(b0, 16); 39 | int d1 = Character.digit(b1, 16); 40 | if (d0 == - 1 || d1 == - 1) throw msg.invalidHexCharacter(); 41 | return ((d0 << 4) | d1) & 0xff; 42 | } 43 | 44 | public boolean hasNext() { 45 | if (havePair) { 46 | return true; 47 | } 48 | if (! iter.hasNext()) { 49 | return false; 50 | } 51 | int b0 = iter.next(); 52 | if (! iter.hasNext()) { 53 | throw msg.expectedEvenNumberOfHexCharacters(); 54 | } 55 | int b1 = iter.next(); 56 | b = calc(b0, b1); 57 | havePair = true; 58 | return true; 59 | } 60 | 61 | public boolean hasPrevious() { 62 | return offset > 0; 63 | } 64 | 65 | public int next() throws NoSuchElementException { 66 | if (! hasNext()) { 67 | throw new NoSuchElementException(); 68 | } 69 | offset++; 70 | havePair = false; 71 | return b; 72 | } 73 | 74 | public int peekNext() throws NoSuchElementException { 75 | if (! hasNext()) { 76 | throw new NoSuchElementException(); 77 | } 78 | return b; 79 | } 80 | 81 | public int previous() throws NoSuchElementException { 82 | if (! hasPrevious()) { 83 | throw new NoSuchElementException(); 84 | } 85 | int b1 = iter.previous(); 86 | int b0 = iter.previous(); 87 | b = calc(b0, b1); 88 | offset--; 89 | havePair = true; 90 | return b; 91 | } 92 | 93 | public int peekPrevious() throws NoSuchElementException { 94 | if (! hasPrevious()) { 95 | throw new NoSuchElementException(); 96 | } 97 | int b1 = iter.previous(); 98 | int b0 = iter.peekPrevious(); 99 | iter.next(); 100 | return calc(b0, b1); 101 | } 102 | 103 | public long getIndex() { 104 | return offset; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/Base16EncodingCodePointIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class Base16EncodingCodePointIterator extends CodePointIterator { 26 | private ByteIterator iter; 27 | private final boolean toUpperCase; 28 | int b; 29 | boolean lo; 30 | 31 | Base16EncodingCodePointIterator(final ByteIterator iter, final boolean toUpperCase) { 32 | this.iter = iter; 33 | this.toUpperCase = toUpperCase; 34 | } 35 | 36 | public boolean hasNext() { 37 | return lo || iter.hasNext(); 38 | } 39 | 40 | public boolean hasPrevious() { 41 | return lo || iter.hasPrevious(); 42 | } 43 | 44 | private int hex(final int i) { 45 | if (i < 10) { 46 | return '0' + i; 47 | } else { 48 | assert i < 16; 49 | return (toUpperCase ? 'A' : 'a') + i - 10; 50 | } 51 | } 52 | 53 | public int next() throws NoSuchElementException { 54 | if (! hasNext()) throw new NoSuchElementException(); 55 | if (lo) { 56 | lo = false; 57 | return hex(b & 0xf); 58 | } else { 59 | b = iter.next(); 60 | lo = true; 61 | return hex(b >> 4); 62 | } 63 | } 64 | 65 | public int peekNext() throws NoSuchElementException { 66 | if (! hasNext()) throw new NoSuchElementException(); 67 | if (lo) { 68 | return hex(b & 0xf); 69 | } else { 70 | return hex(iter.peekNext() >> 4); 71 | } 72 | } 73 | 74 | public int previous() throws NoSuchElementException { 75 | if (! hasPrevious()) throw new NoSuchElementException(); 76 | if (lo) { 77 | lo = false; 78 | iter.previous(); 79 | return hex(b >> 4); 80 | } else { 81 | b = iter.peekPrevious(); 82 | lo = true; 83 | return hex(b & 0xf); 84 | } 85 | } 86 | 87 | public int peekPrevious() throws NoSuchElementException { 88 | if (! hasPrevious()) throw new NoSuchElementException(); 89 | if (lo) { 90 | return hex(b >> 4); 91 | } else { 92 | return hex(iter.peekPrevious() & 0xf); 93 | } 94 | } 95 | 96 | public long getIndex() { 97 | return iter.getIndex() * 2 + (lo ? 1 : 0); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/BiDirIntIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | * A bi-directional primitive iterator. 25 | */ 26 | public interface BiDirIntIterator extends IntIterator { 27 | boolean hasNext(); 28 | 29 | int next() throws NoSuchElementException; 30 | 31 | int peekNext() throws NoSuchElementException; 32 | 33 | /** 34 | * Determine if there is a previous element in this sequence. 35 | * 36 | * @return {@code true} if there is a previous element, {@code false} otherwise 37 | */ 38 | boolean hasPrevious(); 39 | 40 | /** 41 | * Get the previous element in the sequence. 42 | * 43 | * @return the previous element 44 | * @throws NoSuchElementException if there are no more elements 45 | */ 46 | int previous() throws NoSuchElementException; 47 | 48 | /** 49 | * Observe the previous element in the sequence without moving the iterator. 50 | * 51 | * @return the previous element 52 | * @throws NoSuchElementException if there are no more elements 53 | */ 54 | int peekPrevious() throws NoSuchElementException; 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/BigEndianBase32DecodingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import static org.wildfly.common._private.CommonMessages.msg; 22 | 23 | import org.wildfly.common.codec.Base32Alphabet; 24 | 25 | /** 26 | */ 27 | final class BigEndianBase32DecodingByteIterator extends Base32DecodingByteIterator { 28 | private final Base32Alphabet alphabet; 29 | 30 | BigEndianBase32DecodingByteIterator(final CodePointIterator iter, final boolean requirePadding, final Base32Alphabet alphabet) { 31 | super(iter, requirePadding); 32 | this.alphabet = alphabet; 33 | } 34 | 35 | int calc0(final int b0, final int b1) { 36 | final int d0 = alphabet.decode(b0); 37 | final int d1 = alphabet.decode(b1); 38 | // d0 = r0[7..3] 39 | // d1 = r0[2..0] + r1[7..6] 40 | if (d0 == - 1 || d1 == - 1) throw msg.invalidBase32Character(); 41 | return (d0 << 3 | d1 >> 2) & 0xff; 42 | } 43 | 44 | int calc1(final int b1, final int b2, final int b3) { 45 | final int d1 = alphabet.decode(b1); 46 | final int d2 = alphabet.decode(b2); 47 | final int d3 = alphabet.decode(b3); 48 | // d1 = r0[2..0] + r1[7..6] 49 | // d2 = r1[5..1] 50 | // d3 = r1[0] + r2[7..4] 51 | if (d1 == - 1 || d2 == - 1 || d3 == - 1) throw msg.invalidBase32Character(); 52 | return (d1 << 6 | d2 << 1 | d3 >> 4) & 0xff; 53 | } 54 | 55 | int calc2(final int b3, final int b4) { 56 | final int d3 = alphabet.decode(b3); 57 | final int d4 = alphabet.decode(b4); 58 | // d3 = r1[0] + r2[7..4] 59 | // d4 = r2[3..0] + r3[7] 60 | if (d3 == - 1 || d4 == - 1) throw msg.invalidBase32Character(); 61 | return (d3 << 4 | d4 >> 1) & 0xff; 62 | } 63 | 64 | int calc3(final int b4, final int b5, final int b6) { 65 | final int d4 = alphabet.decode(b4); 66 | final int d5 = alphabet.decode(b5); 67 | final int d6 = alphabet.decode(b6); 68 | // d4 = r2[3..0] + r3[7] 69 | // d5 = r3[6..2] 70 | // d6 = r3[1..0] + r4[7..5] 71 | if (d4 == - 1 || d5 == - 1 || d6 == - 1) throw msg.invalidBase32Character(); 72 | return (d4 << 7 | d5 << 2 | d6 >> 3) & 0xff; 73 | } 74 | 75 | int calc4(final int b6, final int b7) { 76 | final int d6 = alphabet.decode(b6); 77 | final int d7 = alphabet.decode(b7); 78 | // d6 = r3[1..0] + r4[7..5] 79 | // d7 = r4[4..0] 80 | if (d6 == - 1 || d7 == - 1) throw msg.invalidBase32Character(); 81 | return (d6 << 5 | d7) & 0xff; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/BigEndianBase32EncodingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import org.wildfly.common.codec.Base32Alphabet; 22 | 23 | /** 24 | */ 25 | final class BigEndianBase32EncodingIterator extends Base32EncodingCodePointIterator { 26 | private final Base32Alphabet alphabet; 27 | 28 | BigEndianBase32EncodingIterator(final ByteIterator iter, final boolean addPadding, final Base32Alphabet alphabet) { 29 | super(iter, addPadding); 30 | this.alphabet = alphabet; 31 | } 32 | 33 | int calc0(final int b0) { 34 | // d0 = r0[7..3] 35 | return alphabet.encode((b0 >> 3) & 0x1f); 36 | } 37 | 38 | int calc1(final int b0, final int b1) { 39 | // d1 = r0[2..0] + r1[7..6] 40 | return alphabet.encode((b0 << 2 | b1 >> 6) & 0x1f); 41 | } 42 | 43 | int calc2(final int b1) { 44 | // d2 = r1[5..1] 45 | return alphabet.encode((b1 >> 1) & 0x1f); 46 | } 47 | 48 | int calc3(final int b1, final int b2) { 49 | // d3 = r1[0] + r2[7..4] 50 | return alphabet.encode((b1 << 4 | b2 >> 4) & 0x1f); 51 | } 52 | 53 | int calc4(final int b2, final int b3) { 54 | // d4 = r2[3..0] + r3[7] 55 | return alphabet.encode((b2 << 1 | b3 >> 7) & 0x1f); 56 | } 57 | 58 | int calc5(final int b3) { 59 | // d5 = r3[6..2] 60 | return alphabet.encode((b3 >> 2) & 0x1f); 61 | } 62 | 63 | int calc6(final int b3, final int b4) { 64 | // d6 = r3[1..0] + r4[7..5] 65 | return alphabet.encode((b3 << 3 | b4 >> 5) & 0x1f); 66 | } 67 | 68 | int calc7(final int b4) { 69 | // d7 = r4[4..0] 70 | return alphabet.encode(b4 & 0x1f); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/BigEndianBase64DecodingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import static org.wildfly.common._private.CommonMessages.msg; 22 | 23 | import org.wildfly.common.codec.Base64Alphabet; 24 | 25 | /** 26 | */ 27 | final class BigEndianBase64DecodingByteIterator extends Base64DecodingByteIterator { 28 | private final Base64Alphabet alphabet; 29 | 30 | BigEndianBase64DecodingByteIterator(final CodePointIterator iter, final boolean requirePadding, final Base64Alphabet alphabet) { 31 | super(iter, requirePadding); 32 | this.alphabet = alphabet; 33 | } 34 | 35 | int calc0(final int b0, final int b1) { 36 | final int d0 = alphabet.decode(b0); 37 | final int d1 = alphabet.decode(b1); 38 | // d0 = r0[7..2] 39 | // d1 = r0[1..0] + r1[7..4] 40 | if (d0 == - 1 || d1 == - 1) throw msg.invalidBase64Character(); 41 | return (d0 << 2 | d1 >> 4) & 0xff; 42 | } 43 | 44 | int calc1(final int b1, final int b2) { 45 | final int d1 = alphabet.decode(b1); 46 | final int d2 = alphabet.decode(b2); 47 | // d1 = r0[1..0] + r1[7..4] 48 | // d2 = r1[3..0] + r2[7..6] 49 | if (d1 == - 1 || d2 == - 1) throw msg.invalidBase64Character(); 50 | return (d1 << 4 | d2 >> 2) & 0xff; 51 | } 52 | 53 | int calc2(final int b2, final int b3) { 54 | final int d2 = alphabet.decode(b2); 55 | final int d3 = alphabet.decode(b3); 56 | // d2 = r1[3..0] + r2[7..6] 57 | // d3 = r2[5..0] 58 | if (d2 == - 1 || d3 == - 1) throw msg.invalidBase64Character(); 59 | return (d2 << 6 | d3) & 0xff; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/BigEndianBase64EncodingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import org.wildfly.common.codec.Base64Alphabet; 22 | 23 | /** 24 | */ 25 | final class BigEndianBase64EncodingIterator extends Base64EncodingIterator { 26 | private final Base64Alphabet alphabet; 27 | 28 | BigEndianBase64EncodingIterator(final ByteIterator iter, final boolean addPadding, final Base64Alphabet alphabet) { 29 | super(iter, addPadding); 30 | this.alphabet = alphabet; 31 | } 32 | 33 | int calc0(final int b0) { 34 | // d0 = r0[7..2] 35 | return alphabet.encode((b0 >> 2) & 0x3f); 36 | } 37 | 38 | int calc1(final int b0, final int b1) { 39 | // d1 = r0[1..0] + r1[7..4] 40 | return alphabet.encode((b0 << 4 | b1 >> 4) & 0x3f); 41 | } 42 | 43 | int calc2(final int b1, final int b2) { 44 | // d2 = r1[3..0] + r2[7..6] 45 | return alphabet.encode((b1 << 2 | b2 >> 6) & 0x3f); 46 | } 47 | 48 | int calc3(final int b2) { 49 | // d3 = r2[5..0] 50 | return alphabet.encode(b2 & 0x3f); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/ByteBufferIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.nio.ByteBuffer; 22 | import java.util.NoSuchElementException; 23 | 24 | /** 25 | */ 26 | final class ByteBufferIterator extends ByteIterator { 27 | private final ByteBuffer buffer; 28 | private final int initialPosition; 29 | 30 | ByteBufferIterator(final ByteBuffer buffer) { 31 | this.buffer = buffer; 32 | initialPosition = buffer.position(); 33 | } 34 | 35 | public boolean hasNext() { 36 | return buffer.hasRemaining(); 37 | } 38 | 39 | public boolean hasPrevious() { 40 | return buffer.position() > initialPosition; 41 | } 42 | 43 | public int next() throws NoSuchElementException { 44 | if (! hasNext()) throw new NoSuchElementException(); 45 | return buffer.get() & 0xff; 46 | } 47 | 48 | public int peekNext() throws NoSuchElementException { 49 | if (! hasNext()) throw new NoSuchElementException(); 50 | return buffer.get(buffer.position()) & 0xff; 51 | } 52 | 53 | public int previous() throws NoSuchElementException { 54 | if (! hasPrevious()) throw new NoSuchElementException(); 55 | buffer.position(buffer.position() - 1); 56 | return peekNext(); 57 | } 58 | 59 | public int peekPrevious() throws NoSuchElementException { 60 | if (! hasPrevious()) throw new NoSuchElementException(); 61 | return buffer.get(buffer.position() - 1) & 0xff; 62 | } 63 | 64 | public long getIndex() { 65 | return buffer.position() - initialPosition; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/ByteIteratorInputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | /** 25 | */ 26 | final class ByteIteratorInputStream extends InputStream { 27 | private final ByteIterator iter; 28 | 29 | ByteIteratorInputStream(final ByteIterator iter) { 30 | this.iter = iter; 31 | } 32 | 33 | public int read() throws IOException { 34 | return iter.hasNext() ? iter.next() : - 1; 35 | } 36 | 37 | public int read(final byte[] b, final int off, final int len) throws IOException { 38 | if (len == 0) return 0; 39 | if (! iter.hasNext()) return - 1; 40 | return iter.drain(b, off, len); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/ByteTableTranslatingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class ByteTableTranslatingByteIterator extends ByteIterator { 26 | private final ByteIterator iter; 27 | private final byte[] table; 28 | 29 | ByteTableTranslatingByteIterator(final ByteIterator iter, final byte[] table) { 30 | this.iter = iter; 31 | this.table = table; 32 | } 33 | 34 | public boolean hasNext() { 35 | return iter.hasNext(); 36 | } 37 | 38 | public boolean hasPrevious() { 39 | return iter.hasPrevious(); 40 | } 41 | 42 | public int next() throws NoSuchElementException { 43 | return table[iter.next()] & 0xff; 44 | } 45 | 46 | public int peekNext() throws NoSuchElementException { 47 | return table[iter.peekNext()] & 0xff; 48 | } 49 | 50 | public int previous() throws NoSuchElementException { 51 | return table[iter.previous()] & 0xff; 52 | } 53 | 54 | public int peekPrevious() throws NoSuchElementException { 55 | return table[iter.peekPrevious()] & 0xff; 56 | } 57 | 58 | public long getIndex() { 59 | return iter.getIndex(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/CharArrayIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class CharArrayIterator extends CodePointIterator { 26 | private final int len; 27 | private final char[] chars; 28 | private final int offs; 29 | private int idx; 30 | private int offset; 31 | 32 | CharArrayIterator(final int len, final char[] chars, final int offs) { 33 | this.len = len; 34 | this.chars = chars; 35 | this.offs = offs; 36 | idx = 0; 37 | offset = 0; 38 | } 39 | 40 | public boolean hasNext() { 41 | return idx < len; 42 | } 43 | 44 | public boolean hasPrevious() { 45 | return idx > 0; 46 | } 47 | 48 | public int next() { 49 | if (! hasNext()) throw new NoSuchElementException(); 50 | try { 51 | offset++; 52 | return Character.codePointAt(chars, offs + idx); 53 | } finally { 54 | idx = Character.offsetByCodePoints(chars, offs, len, offs + idx, 1) - offs; 55 | } 56 | } 57 | 58 | public int peekNext() throws NoSuchElementException { 59 | if (! hasNext()) throw new NoSuchElementException(); 60 | return Character.codePointAt(chars, offs + idx); 61 | } 62 | 63 | public int previous() { 64 | if (! hasPrevious()) throw new NoSuchElementException(); 65 | idx = Character.offsetByCodePoints(chars, offs, len, offs + idx, - 1) - offs; 66 | offset--; 67 | return Character.codePointAt(chars, offs + idx); 68 | } 69 | 70 | public int peekPrevious() throws NoSuchElementException { 71 | if (! hasPrevious()) throw new NoSuchElementException(); 72 | return Character.codePointBefore(chars, offs + idx); 73 | } 74 | 75 | public long getIndex() { 76 | return offset; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/CompositeIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2019 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.Arrays; 22 | import java.util.Iterator; 23 | import java.util.NoSuchElementException; 24 | import java.util.function.Consumer; 25 | 26 | /** 27 | * Iterator that iterates over a series of iterators. 28 | * @author Paul Ferraro 29 | */ 30 | public class CompositeIterator implements Iterator { 31 | 32 | private final Iterable> iterators; 33 | private Iterator lastIterator; 34 | 35 | /** 36 | * Constructs a new composite iterator. 37 | * @param iterators a series of iterators 38 | */ 39 | @SafeVarargs 40 | public CompositeIterator(Iterator... iterators) { 41 | this(Arrays.asList(iterators)); 42 | } 43 | 44 | /** 45 | * Constructs a new composite iterator. 46 | * @param iterators a series of iterators 47 | */ 48 | public CompositeIterator(Iterable> iterators) { 49 | this.iterators = iterators; 50 | } 51 | 52 | @Override 53 | public boolean hasNext() { 54 | for (Iterator iterator : this.iterators) { 55 | if (iterator.hasNext()) return true; 56 | } 57 | return false; 58 | } 59 | 60 | @Override 61 | public E next() { 62 | for (Iterator iterator : this.iterators) { 63 | if (iterator.hasNext()) { 64 | this.lastIterator = iterator; 65 | return iterator.next(); 66 | } 67 | } 68 | throw new NoSuchElementException(); 69 | } 70 | 71 | @Override 72 | public void remove() { 73 | Iterator iterator = this.lastIterator; 74 | if (iterator == null) { 75 | throw new IllegalStateException(); 76 | } 77 | iterator.remove(); 78 | } 79 | 80 | @Override 81 | public void forEachRemaining(Consumer action) { 82 | for (Iterator iterator : this.iterators) { 83 | while (iterator.hasNext()) { 84 | action.accept(iterator.next()); 85 | } 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/ConcatByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class ConcatByteIterator extends ByteIterator { 26 | private final ByteIterator[] iterators; 27 | private long index = 0; 28 | 29 | ConcatByteIterator(final ByteIterator[] iterators) { 30 | this.iterators = iterators; 31 | } 32 | 33 | private int seekNext() { 34 | for (int i = 0; i < iterators.length; i ++) { 35 | if (iterators[i].hasNext()) { 36 | return i; 37 | } 38 | } 39 | return -1; 40 | } 41 | 42 | private int seekPrevious() { 43 | for (int i = iterators.length - 1; i >= 0; i --) { 44 | if (iterators[i].hasPrevious()) { 45 | return i; 46 | } 47 | } 48 | return -1; 49 | } 50 | 51 | public boolean hasNext() { 52 | return seekNext() != -1; 53 | } 54 | 55 | public boolean hasPrevious() { 56 | return seekPrevious() != -1; 57 | } 58 | 59 | public int next() throws NoSuchElementException { 60 | final int seek = seekNext(); 61 | if (seek == -1) throw new NoSuchElementException(); 62 | final int next = iterators[seek].next(); 63 | index++; 64 | return next; 65 | } 66 | 67 | public int peekNext() throws NoSuchElementException { 68 | final int seek = seekNext(); 69 | if (seek == -1) throw new NoSuchElementException(); 70 | return iterators[seek].peekNext(); 71 | } 72 | 73 | public int previous() throws NoSuchElementException { 74 | final int seek = seekPrevious(); 75 | if (seek == -1) throw new NoSuchElementException(); 76 | final int previous = iterators[seek].previous(); 77 | index--; 78 | return previous; 79 | } 80 | 81 | public int peekPrevious() throws NoSuchElementException { 82 | final int seek = seekPrevious(); 83 | if (seek == -1) throw new NoSuchElementException(); 84 | return iterators[seek].peekPrevious(); 85 | } 86 | 87 | public long getIndex() { 88 | return index; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/DelimitedByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class DelimitedByteIterator extends ByteIterator { 26 | private final ByteIterator iter; 27 | private final int[] delims; 28 | long offset; 29 | 30 | DelimitedByteIterator(final ByteIterator iter, final int... delims) { 31 | this.iter = iter; 32 | this.delims = delims; 33 | offset = 0; 34 | } 35 | 36 | public boolean hasNext() { 37 | return iter.hasNext() && ! isDelim(iter.peekNext()); 38 | } 39 | 40 | public boolean hasPrevious() { 41 | return offset > 0; 42 | } 43 | 44 | public int next() { 45 | if (! hasNext()) throw new NoSuchElementException(); 46 | offset++; 47 | return iter.next(); 48 | } 49 | 50 | public int peekNext() throws NoSuchElementException { 51 | if (! hasNext()) throw new NoSuchElementException(); 52 | return iter.peekNext(); 53 | } 54 | 55 | public int previous() { 56 | if (! hasPrevious()) throw new NoSuchElementException(); 57 | offset--; 58 | return iter.previous(); 59 | } 60 | 61 | public int peekPrevious() throws NoSuchElementException { 62 | if (! hasPrevious()) throw new NoSuchElementException(); 63 | return iter.peekPrevious(); 64 | } 65 | 66 | public long getIndex() { 67 | return offset; 68 | } 69 | 70 | private boolean isDelim(int b) { 71 | for (int delim : delims) { 72 | if (delim == b) { 73 | return true; 74 | } 75 | } 76 | return false; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/DelimitedCodePointIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class DelimitedCodePointIterator extends CodePointIterator { 26 | private final CodePointIterator iter; 27 | private final int[] delims; 28 | long offset; 29 | 30 | DelimitedCodePointIterator(final CodePointIterator iter, final int... delims) { 31 | this.iter = iter; 32 | this.delims = delims; 33 | offset = 0; 34 | } 35 | 36 | public boolean hasNext() { 37 | return iter.hasNext() && ! isDelim(iter.peekNext()); 38 | } 39 | 40 | public boolean hasPrevious() { 41 | return offset > 0; 42 | } 43 | 44 | public int next() { 45 | if (! hasNext()) throw new NoSuchElementException(); 46 | offset++; 47 | return iter.next(); 48 | } 49 | 50 | public int peekNext() throws NoSuchElementException { 51 | if (! hasNext()) throw new NoSuchElementException(); 52 | return iter.peekNext(); 53 | } 54 | 55 | public int previous() { 56 | if (! hasPrevious()) throw new NoSuchElementException(); 57 | offset--; 58 | return iter.previous(); 59 | } 60 | 61 | public int peekPrevious() throws NoSuchElementException { 62 | if (! hasPrevious()) throw new NoSuchElementException(); 63 | return iter.peekPrevious(); 64 | } 65 | 66 | public long getIndex() { 67 | return offset; 68 | } 69 | 70 | private boolean isDelim(int b) { 71 | for (int delim : delims) { 72 | if (delim == b) { 73 | return true; 74 | } 75 | } 76 | return false; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/EnumerationIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.Enumeration; 22 | import java.util.Iterator; 23 | import java.util.NoSuchElementException; 24 | 25 | /** 26 | * An enumeration which is also an iterator. 27 | * 28 | * @author David M. Lloyd 29 | */ 30 | public interface EnumerationIterator extends Enumeration, Iterator { 31 | /** 32 | * Determine if there are more elements to iterate over in the direction of this iterator. 33 | * 34 | * @return {@code true} if there are more elements, {@code false} otherwise 35 | */ 36 | default boolean hasMoreElements() { 37 | return hasNext(); 38 | } 39 | 40 | /** 41 | * Get the next element in the direction of this iterator. 42 | * 43 | * @return the next element 44 | */ 45 | default E nextElement() { 46 | return next(); 47 | } 48 | 49 | /** 50 | * Get an enumeration iterator over one element. 51 | * 52 | * @param item the element 53 | * @param the element type 54 | * @return the enumeration iterator 55 | */ 56 | static EnumerationIterator over(E item) { 57 | return new EnumerationIterator() { 58 | boolean done; 59 | public boolean hasNext() { 60 | return ! done; 61 | } 62 | 63 | public E next() { 64 | if (! hasNext()) throw new NoSuchElementException(); 65 | done = true; 66 | return item; 67 | } 68 | }; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/IndexIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | /** 22 | * An iterator which can report the current iterator index. 23 | */ 24 | public interface IndexIterator { 25 | /** 26 | * Get the current iterator index. 27 | * 28 | * @return the index 29 | */ 30 | long getIndex(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/IntIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | * A primitive iterator, which can be used as the basis for string parsing, tokenizing, and other purposes. 25 | */ 26 | public interface IntIterator { 27 | /** 28 | * Determine if there is another element in this sequence. 29 | * 30 | * @return {@code true} if there is another element, {@code false} otherwise 31 | */ 32 | boolean hasNext(); 33 | 34 | /** 35 | * Get the next element in the sequence. 36 | * 37 | * @return the next element 38 | * @throws NoSuchElementException if there are no more elements 39 | */ 40 | int next() throws NoSuchElementException; 41 | 42 | /** 43 | * Observe the next element in the sequence without moving the iterator. 44 | * 45 | * @return the next element 46 | * @throws NoSuchElementException if there are no more elements 47 | */ 48 | int peekNext() throws NoSuchElementException; 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/IntTableTranslatingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class IntTableTranslatingByteIterator extends ByteIterator { 26 | private final ByteIterator iter; 27 | private final int[] table; 28 | 29 | IntTableTranslatingByteIterator(final ByteIterator iter, final int[] table) { 30 | this.iter = iter; 31 | this.table = table; 32 | } 33 | 34 | public boolean hasNext() { 35 | return iter.hasNext(); 36 | } 37 | 38 | public boolean hasPrevious() { 39 | return iter.hasPrevious(); 40 | } 41 | 42 | public int next() throws NoSuchElementException { 43 | return table[iter.next()] & 0xff; 44 | } 45 | 46 | public int peekNext() throws NoSuchElementException { 47 | return table[iter.peekNext()] & 0xff; 48 | } 49 | 50 | public int previous() throws NoSuchElementException { 51 | return table[iter.previous()] & 0xff; 52 | } 53 | 54 | public int peekPrevious() throws NoSuchElementException { 55 | return table[iter.peekPrevious()] & 0xff; 56 | } 57 | 58 | public long getIndex() { 59 | return iter.getIndex(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/InterleavedByteArrayIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class InterleavedByteArrayIterator extends ByteIterator { 26 | private final int len; 27 | private final byte[] bytes; 28 | private final int offs; 29 | private final int[] interleave; 30 | private int idx; 31 | 32 | InterleavedByteArrayIterator(final int len, final byte[] bytes, final int offs, final int[] interleave) { 33 | this.len = len; 34 | this.bytes = bytes; 35 | this.offs = offs; 36 | this.interleave = interleave; 37 | idx = 0; 38 | } 39 | 40 | public boolean hasNext() { 41 | return idx < len; 42 | } 43 | 44 | public boolean hasPrevious() { 45 | return idx > 0; 46 | } 47 | 48 | public int next() { 49 | if (! hasNext()) throw new NoSuchElementException(); 50 | return bytes[offs + interleave[idx++]] & 0xff; 51 | } 52 | 53 | public int previous() { 54 | if (! hasPrevious()) throw new NoSuchElementException(); 55 | return bytes[offs + interleave[-- idx]] & 0xff; 56 | } 57 | 58 | public int peekNext() throws NoSuchElementException { 59 | if (! hasNext()) throw new NoSuchElementException(); 60 | return bytes[offs + interleave[idx]] & 0xff; 61 | } 62 | 63 | public int peekPrevious() throws NoSuchElementException { 64 | if (! hasPrevious()) throw new NoSuchElementException(); 65 | return bytes[offs + interleave[idx - 1]] & 0xff; 66 | } 67 | 68 | public long getIndex() { 69 | return idx; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/Latin1DecodingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class Latin1DecodingIterator extends CodePointIterator { 26 | private final ByteIterator iter; 27 | private final long start; 28 | 29 | Latin1DecodingIterator(final ByteIterator iter, final long start) { 30 | this.iter = iter; 31 | this.start = start; 32 | } 33 | 34 | public boolean hasNext() { 35 | return iter.hasNext(); 36 | } 37 | 38 | public boolean hasPrevious() { 39 | return start > 0 && iter.hasPrevious(); 40 | } 41 | 42 | public int next() { 43 | return iter.next(); 44 | } 45 | 46 | public int peekNext() throws NoSuchElementException { 47 | return iter.peekNext(); 48 | } 49 | 50 | public int previous() { 51 | if (start == 0) throw new NoSuchElementException(); 52 | return iter.previous(); 53 | } 54 | 55 | public int peekPrevious() throws NoSuchElementException { 56 | return iter.peekPrevious(); 57 | } 58 | 59 | public long getIndex() { 60 | return iter.getIndex() - start; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/Latin1EncodingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class Latin1EncodingByteIterator extends ByteIterator { 26 | private final CodePointIterator iter; 27 | 28 | Latin1EncodingByteIterator(final CodePointIterator iter) { 29 | this.iter = iter; 30 | } 31 | 32 | public boolean hasNext() { 33 | return iter.hasNext(); 34 | } 35 | 36 | public boolean hasPrevious() { 37 | return iter.hasPrevious(); 38 | } 39 | 40 | public int next() throws NoSuchElementException { 41 | final int v = iter.next(); 42 | return v > 255 ? '?' : v; 43 | } 44 | 45 | public int peekNext() throws NoSuchElementException { 46 | final int v = iter.peekNext(); 47 | return v > 255 ? '?' : v; 48 | } 49 | 50 | public int previous() throws NoSuchElementException { 51 | final int v = iter.previous(); 52 | return v > 255 ? '?' : v; 53 | } 54 | 55 | public int peekPrevious() throws NoSuchElementException { 56 | final int v = iter.peekPrevious(); 57 | return v > 255 ? '?' : v; 58 | } 59 | 60 | public long getIndex() { 61 | return iter.getIndex(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/LimitedByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class LimitedByteIterator extends ByteIterator { 26 | private final ByteIterator iter; 27 | private final long size; 28 | long offset; 29 | 30 | LimitedByteIterator(final ByteIterator iter, final long size) { 31 | this.iter = iter; 32 | this.size = size; 33 | offset = 0; 34 | } 35 | 36 | public boolean hasNext() { 37 | return offset < size && iter.hasNext(); 38 | } 39 | 40 | public boolean hasPrevious() { 41 | return offset > 0; 42 | } 43 | 44 | public int next() { 45 | if (offset == size) { 46 | throw new NoSuchElementException(); 47 | } 48 | offset++; 49 | return iter.next(); 50 | } 51 | 52 | public int peekNext() throws NoSuchElementException { 53 | if (offset == size) { 54 | throw new NoSuchElementException(); 55 | } 56 | return iter.peekNext(); 57 | } 58 | 59 | public int previous() { 60 | if (offset == 0) { 61 | throw new NoSuchElementException(); 62 | } 63 | offset--; 64 | return iter.previous(); 65 | } 66 | 67 | public int peekPrevious() throws NoSuchElementException { 68 | if (offset == 0) { 69 | throw new NoSuchElementException(); 70 | } 71 | return iter.peekPrevious(); 72 | } 73 | 74 | public int drain(final byte[] dst, final int offs, final int len) { 75 | return super.drain(dst, offs, (int) Math.min(len, size - offset)); 76 | } 77 | 78 | public long getIndex() { 79 | return offset; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/LimitedCodePointIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class LimitedCodePointIterator extends CodePointIterator { 26 | private final CodePointIterator iter; 27 | private final long size; 28 | private long offset; 29 | 30 | LimitedCodePointIterator(final CodePointIterator iter, final long size) { 31 | this.iter = iter; 32 | this.size = size; 33 | offset = 0; 34 | } 35 | 36 | public boolean hasNext() { 37 | return offset < size && iter.hasNext(); 38 | } 39 | 40 | public boolean hasPrevious() { 41 | return offset > 0; 42 | } 43 | 44 | public int next() { 45 | if (! hasNext()) { 46 | throw new NoSuchElementException(); 47 | } 48 | offset++; 49 | return iter.next(); 50 | } 51 | 52 | public int peekNext() throws NoSuchElementException { 53 | if (! hasNext()) { 54 | throw new NoSuchElementException(); 55 | } 56 | return iter.peekNext(); 57 | } 58 | 59 | public int previous() { 60 | if (! hasPrevious()) { 61 | throw new NoSuchElementException(); 62 | } 63 | offset--; 64 | return iter.previous(); 65 | } 66 | 67 | public int peekPrevious() throws NoSuchElementException { 68 | if (! hasPrevious()) { 69 | throw new NoSuchElementException(); 70 | } 71 | return iter.peekPrevious(); 72 | } 73 | 74 | public long getIndex() { 75 | return offset; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/LittleEndianBase32DecodingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import static org.wildfly.common._private.CommonMessages.msg; 22 | 23 | import org.wildfly.common.codec.Base32Alphabet; 24 | 25 | /** 26 | */ 27 | final class LittleEndianBase32DecodingByteIterator extends Base32DecodingByteIterator { 28 | private final Base32Alphabet alphabet; 29 | 30 | LittleEndianBase32DecodingByteIterator(final CodePointIterator iter, final boolean requirePadding, final Base32Alphabet alphabet) { 31 | super(iter, requirePadding); 32 | this.alphabet = alphabet; 33 | } 34 | 35 | int calc0(final int b0, final int b1) { 36 | final int d0 = alphabet.decode(b0); 37 | final int d1 = alphabet.decode(b1); 38 | // d0 = r0[4..0] 39 | // d1 = r1[1..0] + r0[7..5] 40 | if (d0 == - 1 || d1 == - 1) throw msg.invalidBase32Character(); 41 | return (d0 | d1 << 5) & 0xff; 42 | } 43 | 44 | int calc1(final int b1, final int b2, final int b3) { 45 | final int d1 = alphabet.decode(b1); 46 | final int d2 = alphabet.decode(b2); 47 | final int d3 = alphabet.decode(b3); 48 | // d1 = r1[1..0] + r0[7..5] 49 | // d2 = r1[6..2] 50 | // d3 = r2[3..0] + r1[7] 51 | if (d1 == - 1 || d2 == - 1 || d3 == - 1) throw msg.invalidBase32Character(); 52 | return (d1 >> 3 | d2 << 2 | d3 << 7) & 0xff; 53 | } 54 | 55 | int calc2(final int b3, final int b4) { 56 | final int d3 = alphabet.decode(b3); 57 | final int d4 = alphabet.decode(b4); 58 | // d3 = r2[3..0] + r1[7] 59 | // d4 = r3[0] + r2[7..4] 60 | if (d3 == - 1 || d4 == - 1) throw msg.invalidBase32Character(); 61 | return (d3 >> 1 | d4 << 4) & 0xff; 62 | } 63 | 64 | int calc3(final int b4, final int b5, final int b6) { 65 | final int d4 = alphabet.decode(b4); 66 | final int d5 = alphabet.decode(b5); 67 | final int d6 = alphabet.decode(b6); 68 | // d4 = r3[0] + r2[7..4] 69 | // d5 = r3[5..1] 70 | // d6 = r4[2..0] + r3[7..6] 71 | if (d4 == - 1 || d5 == - 1 || d6 == - 1) throw msg.invalidBase32Character(); 72 | return (d4 >> 4 | d5 << 1 | d6 << 6) & 0xff; 73 | } 74 | 75 | int calc4(final int b6, final int b7) { 76 | final int d6 = alphabet.decode(b6); 77 | final int d7 = alphabet.decode(b7); 78 | // d6 = r4[2..0] + r3[7..6] 79 | // d7 = r4[7..3] 80 | if (d6 == - 1 || d7 == - 1) throw msg.invalidBase32Character(); 81 | return (d6 >> 2 | d7 << 3) & 0xff; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/LittleEndianBase32EncodingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import org.wildfly.common.codec.Base32Alphabet; 22 | 23 | /** 24 | */ 25 | final class LittleEndianBase32EncodingIterator extends Base32EncodingCodePointIterator { 26 | private final Base32Alphabet alphabet; 27 | 28 | LittleEndianBase32EncodingIterator(final ByteIterator iter, final boolean addPadding, final Base32Alphabet alphabet) { 29 | super(iter, addPadding); 30 | this.alphabet = alphabet; 31 | } 32 | 33 | int calc0(final int b0) { 34 | // d0 = r0[4..0] 35 | return alphabet.encode(b0 & 0x1f); 36 | } 37 | 38 | int calc1(final int b0, final int b1) { 39 | // d1 = r1[1..0] + r0[7..5] 40 | return alphabet.encode((b1 << 3 | b0 >> 5) & 0x1f); 41 | } 42 | 43 | int calc2(final int b1) { 44 | // d2 = r1[6..2] 45 | return alphabet.encode((b1 >> 2) & 0x1f); 46 | } 47 | 48 | int calc3(final int b1, final int b2) { 49 | // d3 = r2[3..0] + r1[7] 50 | return alphabet.encode((b2 << 1 | b1 >> 7) & 0x1f); 51 | } 52 | 53 | int calc4(final int b2, final int b3) { 54 | // d4 = r3[0] + r2[7..4] 55 | return alphabet.encode((b3 << 4 | b2 >> 4) & 0x1f); 56 | } 57 | 58 | int calc5(final int b3) { 59 | // d5 = r3[5..1] 60 | return alphabet.encode((b3 >> 1) & 0x1f); 61 | } 62 | 63 | int calc6(final int b3, final int b4) { 64 | // d6 = r4[2..0] + r3[7..6] 65 | return alphabet.encode((b4 << 2 | b3 >> 6) & 0x1f); 66 | } 67 | 68 | int calc7(final int b4) { 69 | // d7 = r4[7..3] 70 | return alphabet.encode((b4 >> 3) & 0x1f); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/LittleEndianBase64DecodingByteIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import static org.wildfly.common._private.CommonMessages.msg; 22 | 23 | import org.wildfly.common.codec.Base64Alphabet; 24 | 25 | /** 26 | */ 27 | final class LittleEndianBase64DecodingByteIterator extends Base64DecodingByteIterator { 28 | private final Base64Alphabet alphabet; 29 | 30 | LittleEndianBase64DecodingByteIterator(final CodePointIterator iter, final boolean requirePadding, final Base64Alphabet alphabet) { 31 | super(iter, requirePadding); 32 | this.alphabet = alphabet; 33 | } 34 | 35 | int calc0(final int b0, final int b1) { 36 | final int d0 = alphabet.decode(b0); 37 | final int d1 = alphabet.decode(b1); 38 | // d0 = r0[5..0] 39 | // d1 = r1[3..0] + r0[7..6] 40 | if (d0 == - 1 || d1 == - 1) throw msg.invalidBase64Character(); 41 | return (d0 | d1 << 6) & 0xff; 42 | } 43 | 44 | int calc1(final int b1, final int b2) { 45 | final int d1 = alphabet.decode(b1); 46 | final int d2 = alphabet.decode(b2); 47 | // d1 = r1[3..0] + r0[7..6] 48 | // d2 = r2[1..0] + r1[7..4] 49 | if (d1 == - 1 || d2 == - 1) throw msg.invalidBase64Character(); 50 | return (d1 >> 2 | d2 << 4) & 0xff; 51 | } 52 | 53 | int calc2(final int b2, final int b3) { 54 | final int d2 = alphabet.decode(b2); 55 | final int d3 = alphabet.decode(b3); 56 | // d2 = r2[1..0] + r1[7..4] 57 | // d3 = r2[7..2] 58 | if (d2 == - 1 || d3 == - 1) throw msg.invalidBase64Character(); 59 | return (d2 >> 4 | d3 << 2) & 0xff; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/LittleEndianBase64EncodingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import org.wildfly.common.codec.Base64Alphabet; 22 | 23 | /** 24 | */ 25 | final class LittleEndianBase64EncodingIterator extends Base64EncodingIterator { 26 | private final Base64Alphabet alphabet; 27 | 28 | LittleEndianBase64EncodingIterator(final ByteIterator iter, final boolean addPadding, final Base64Alphabet alphabet) { 29 | super(iter, addPadding); 30 | this.alphabet = alphabet; 31 | } 32 | 33 | int calc0(final int b0) { 34 | // d0 = r0[5..0] 35 | return alphabet.encode(b0 & 0x3f); 36 | } 37 | 38 | int calc1(final int b0, final int b1) { 39 | // d1 = r1[3..0] + r0[7..6] 40 | return alphabet.encode((b1 << 2 | b0 >> 6) & 0x3f); 41 | } 42 | 43 | int calc2(final int b1, final int b2) { 44 | // d2 = r2[1..0] + r1[7..4] 45 | return alphabet.encode((b2 << 4 | b1 >> 4) & 0x3f); 46 | } 47 | 48 | int calc3(final int b2) { 49 | // d3 = r2[7..2] 50 | return alphabet.encode((b2 >> 2) & 0x3f); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/StringIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.NoSuchElementException; 22 | 23 | /** 24 | */ 25 | final class StringIterator extends CodePointIterator { 26 | private final int len; 27 | private final String string; 28 | private final int offs; 29 | private int idx; 30 | private long offset; 31 | 32 | StringIterator(final int len, final String string, final int offs) { 33 | this.len = len; 34 | this.string = string; 35 | this.offs = offs; 36 | idx = 0; 37 | offset = 0; 38 | } 39 | 40 | public boolean hasNext() { 41 | return idx < len; 42 | } 43 | 44 | public boolean hasPrevious() { 45 | return offset > 0; 46 | } 47 | 48 | public int next() { 49 | if (! hasNext()) throw new NoSuchElementException(); 50 | try { 51 | offset++; 52 | return string.codePointAt(idx + offs); 53 | } finally { 54 | idx = string.offsetByCodePoints(idx + offs, 1) - offs; 55 | } 56 | } 57 | 58 | public int peekNext() throws NoSuchElementException { 59 | if (! hasNext()) throw new NoSuchElementException(); 60 | return string.codePointAt(idx + offs); 61 | } 62 | 63 | public int previous() { 64 | if (! hasPrevious()) throw new NoSuchElementException(); 65 | idx = string.offsetByCodePoints(idx + offs, - 1) - offs; 66 | offset--; 67 | return string.codePointAt(idx + offs); 68 | } 69 | 70 | public int peekPrevious() throws NoSuchElementException { 71 | if (! hasPrevious()) throw new NoSuchElementException(); 72 | return string.codePointBefore(idx + offs); 73 | } 74 | 75 | public long getIndex() { 76 | return offset; 77 | } 78 | 79 | public StringBuilder drainTo(final StringBuilder b) { 80 | try { 81 | return b.append(string, idx + offs, offs + len); 82 | } finally { 83 | offset += string.codePointCount(idx + offs, offs + len); 84 | idx = len; 85 | } 86 | } 87 | 88 | public String drainToString() { 89 | try { 90 | return string.substring(idx + offs, offs + len); 91 | } finally { 92 | offset += string.codePointCount(idx + offs, offs + len); 93 | idx = len; 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/iteration/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes related to iteration. 21 | */ 22 | package org.wildfly.common.iteration; 23 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/lock/ExtendedLock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.lock; 20 | 21 | import java.util.concurrent.locks.Lock; 22 | 23 | /** 24 | * A thread-owned lock which exposes additional informational methods. 25 | */ 26 | public interface ExtendedLock extends Lock { 27 | 28 | /** 29 | * Determine if this lock is held. 30 | * 31 | * @return {@code true} if this lock is held, {@code false} otherwise 32 | */ 33 | boolean isLocked(); 34 | 35 | /** 36 | * Determine if this lock is held by the current thread. 37 | * 38 | * @return {@code true} if this lock is held by the current thread, {@code false} otherwise 39 | */ 40 | boolean isHeldByCurrentThread(); 41 | 42 | /** 43 | * Query if this lock instance tends to be "fair". 44 | * 45 | * @return {@code true} if the lock instance tends towards fairness, {@code false} otherwise 46 | */ 47 | boolean isFair(); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/lock/ExtendedReentrantLock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.lock; 20 | 21 | import java.util.concurrent.locks.ReentrantLock; 22 | 23 | /** 24 | */ 25 | @SuppressWarnings("serial") 26 | class ExtendedReentrantLock extends ReentrantLock implements ExtendedLock { 27 | ExtendedReentrantLock(final boolean fair) { 28 | super(fair); 29 | } 30 | 31 | ExtendedReentrantLock() { 32 | super(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/lock/Locks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.lock; 20 | 21 | import org.wildfly.common.annotation.NotNull; 22 | 23 | /** 24 | * A utility class to create {@code ExtendedLock} objects. 25 | */ 26 | public final class Locks { 27 | private Locks() {} 28 | 29 | /** 30 | * Create a standard reentrant {@code ExtendedLock} with the default fairness policy. 31 | * 32 | * @return a reentrant {@code ExtendedLock} 33 | */ 34 | public static @NotNull ExtendedLock reentrantLock() { 35 | return new ExtendedReentrantLock(); 36 | } 37 | 38 | /** 39 | * Create a standard reentrant {@code ExtendedLock} with the given fairness policy. 40 | * 41 | * @param fair the fairness policy 42 | * @return a reentrant {@code ExtendedLock} 43 | */ 44 | public static @NotNull ExtendedLock reentrantLock(boolean fair) { 45 | return new ExtendedReentrantLock(fair); 46 | } 47 | 48 | /** 49 | * Create a spin lock. 50 | * 51 | * @return the spin lock 52 | * @see SpinLock 53 | */ 54 | public static @NotNull ExtendedLock spinLock() { 55 | return new SpinLock(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/lock/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes which provide basic lock types. 3 | */ 4 | package org.wildfly.common.lock; -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/math/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes which perform various common mathematical functions. 21 | * 22 | * @author David M. Lloyd 23 | */ 24 | 25 | package org.wildfly.common.math; 26 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/net/HostName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.net; 20 | 21 | /** 22 | * Methods for getting the system host name. The host name is detected from the environment, but may be overridden by 23 | * use of the {@code jboss.host.name} and/or {@code jboss.qualified.host.name} system properties. 24 | * 25 | * @author David M. Lloyd 26 | * @deprecated Use {@link io.smallrye.common.net.HostName} instead. 27 | */ 28 | @Deprecated(forRemoval = true) 29 | public final class HostName { 30 | 31 | /** 32 | * Get the detected host name. 33 | * 34 | * @return the detected host name 35 | * @deprecated Use {@link io.smallrye.common.net.HostName#getHostName()} instead. 36 | */ 37 | @Deprecated(forRemoval = true) 38 | public static String getHostName() { 39 | return io.smallrye.common.net.HostName.getHostName(); 40 | } 41 | 42 | /** 43 | * Get the detected qualified host name. 44 | * 45 | * @return the detected qualified host name 46 | * @deprecated Use {@link io.smallrye.common.net.HostName#getQualifiedHostName()} instead. 47 | */ 48 | @Deprecated(forRemoval = true) 49 | public static String getQualifiedHostName() { 50 | return io.smallrye.common.net.HostName.getQualifiedHostName(); 51 | } 52 | 53 | /** 54 | * Get the node name. 55 | * 56 | * @return the node name 57 | * @deprecated Use {@link io.smallrye.common.net.HostName#getNodeName()} instead. 58 | */ 59 | @Deprecated(forRemoval = true) 60 | public static String getNodeName() { 61 | return io.smallrye.common.net.HostName.getNodeName(); 62 | } 63 | 64 | /** 65 | * Set the host name. The qualified host name is set directly from the given value; the unqualified host name 66 | * is then re-derived from that value. The node name is not changed by this method. 67 | * 68 | * @param qualifiedHostName the host name 69 | * @deprecated Use {@link io.smallrye.common.net.HostName#setQualifiedHostName(String)} instead. 70 | */ 71 | @Deprecated(forRemoval = true) 72 | public static void setQualifiedHostName(final String qualifiedHostName) { 73 | io.smallrye.common.net.HostName.setQualifiedHostName(qualifiedHostName); 74 | } 75 | 76 | /** 77 | * Set the node name. 78 | * 79 | * @param nodeName the node name 80 | * @deprecated Use {@link io.smallrye.common.net.HostName#setNodeName(String)} instead. 81 | */ 82 | @Deprecated(forRemoval = true) 83 | public static void setNodeName(final String nodeName) { 84 | io.smallrye.common.net.HostName.setNodeName(nodeName); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/net/URIs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.net; 20 | 21 | import java.net.URI; 22 | 23 | /** 24 | * URI-specific utilities. 25 | * 26 | * @deprecated Use {@link io.smallrye.common.net.URIs} instead. 27 | */ 28 | @Deprecated(forRemoval = true) 29 | public final class URIs { 30 | private URIs() {} 31 | 32 | /** 33 | * Get the user name information from a URI, if any. 34 | * 35 | * @param uri the URI 36 | * @return the user name, or {@code null} if the URI did not contain a recoverable user name 37 | * @deprecated Use {@link io.smallrye.common.net.URIs#getUserFromURI(URI)} instead. 38 | */ 39 | @Deprecated(forRemoval = true) 40 | public static String getUserFromURI(URI uri) { 41 | return io.smallrye.common.net.URIs.getUserFromURI(uri); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/net/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Utilities relating abstractly to network-related activities. 21 | * 22 | * @author David M. Lloyd 23 | * 24 | * @deprecated Use {@link io.smallrye.common.net} instead. 25 | */ 26 | @Deprecated(forRemoval = true) 27 | package org.wildfly.common.net; 28 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/os/Process.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.os; 20 | 21 | /** 22 | * Utilities for getting information about the current process. 23 | * 24 | * @author David M. Lloyd 25 | * @deprecated Use {@link io.smallrye.common.os.Process} instead. 26 | */ 27 | @Deprecated(forRemoval = true) 28 | public final class Process { 29 | private Process() { 30 | } 31 | 32 | /** 33 | * Get the name of this process. If the process name is not known, then "<unknown>" is returned. 34 | * 35 | * @return the process name (not {@code null}) 36 | * @deprecated Use {@link io.smallrye.common.os.Process#getProcessName} instead. 37 | */ 38 | @Deprecated(forRemoval = true) 39 | public static String getProcessName() { 40 | return io.smallrye.common.os.Process.getProcessName(); 41 | } 42 | 43 | /** 44 | * Get the ID of this process. This is the operating system specific PID. If the PID cannot be determined, 45 | * -1 is returned. 46 | * 47 | * @return the ID of this process, or -1 if it cannot be determined 48 | * @deprecated Use {@link io.smallrye.common.os.Process#getProcessId} instead. 49 | */ 50 | @Deprecated(forRemoval = true) 51 | public static long getProcessId() { 52 | return io.smallrye.common.os.Process.getProcessId(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/os/ProcessRedirect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2018 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.os; 20 | 21 | /** 22 | * @deprecated Use {@link ProcessBuilder.Redirect#DISCARD} directly. 23 | */ 24 | @Deprecated(forRemoval = true) 25 | public final class ProcessRedirect { 26 | private ProcessRedirect() {} 27 | 28 | /** 29 | * @deprecated Use {@link ProcessBuilder.Redirect#DISCARD} directly. 30 | */ 31 | @Deprecated(forRemoval = true) 32 | public static ProcessBuilder.Redirect discard() { 33 | return ProcessBuilder.Redirect.DISCARD; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/os/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Utilities related to the operating system and current process. 21 | * 22 | * @author David M. Lloyd 23 | * @deprecated Use {@link io.smallrye.common.os} instead. 24 | */ 25 | @Deprecated(forRemoval = true) 26 | package org.wildfly.common.os; 27 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Common classes for a variety of purposes. Use {@link org.wildfly.common.Assert Assert} for common checks and 21 | * assertions. Use {@link org.wildfly.common.Factory Factory} as a generalized factory type. 22 | * 23 | * @author David M. Lloyd 24 | */ 25 | package org.wildfly.common; -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/CleanerReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | import java.lang.ref.Cleaner; 22 | import java.util.Collections; 23 | import java.util.Set; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | 26 | /** 27 | * A special version of {@link PhantomReference} that is strongly retained until it is reaped by the collection thread. 28 | * 29 | * @author David M. Lloyd 30 | * 31 | * @deprecated Use {@link Cleaner} or {@link io.smallrye.common.ref.CleanerReference} instead. 32 | */ 33 | @Deprecated(forRemoval = true) 34 | public class CleanerReference extends PhantomReference { 35 | private static final Set> set = Collections.newSetFromMap(new ConcurrentHashMap, Boolean>()); 36 | 37 | /** 38 | * Construct a new instance with a reaper. 39 | * 40 | * @param referent the referent 41 | * @param attachment the attachment 42 | * @param reaper the reaper to use 43 | */ 44 | public CleanerReference(final T referent, final A attachment, final Reaper reaper) { 45 | super(referent, attachment, reaper); 46 | set.add(this); 47 | } 48 | 49 | void clean() { 50 | set.remove(this); 51 | } 52 | 53 | public final int hashCode() { 54 | return super.hashCode(); 55 | } 56 | 57 | public final boolean equals(final Object obj) { 58 | return super.equals(obj); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | import static java.lang.invoke.MethodHandles.lookup; 22 | 23 | import org.jboss.logging.Logger; 24 | import org.jboss.logging.annotations.Cause; 25 | import org.jboss.logging.annotations.LogMessage; 26 | import org.jboss.logging.annotations.Message; 27 | import org.jboss.logging.annotations.MessageLogger; 28 | 29 | /** 30 | * @author David M. Lloyd 31 | */ 32 | @MessageLogger(projectCode = "COM", length = 5) 33 | interface Log { 34 | Log log = Logger.getMessageLogger(lookup(), Log.class, "org.wildfly.common.ref"); 35 | 36 | // 3000-3099 reserved for reference queue logging 37 | 38 | @LogMessage(level = Logger.Level.DEBUG) 39 | @Message(id = 3000, value = "Reaping a reference failed") 40 | void reapFailed(@Cause Throwable cause); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/PhantomReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | import java.lang.ref.ReferenceQueue; 22 | 23 | /** 24 | * A reapable phantom reference with an attachment. If a {@link Reaper} is given, then it will be used to asynchronously 25 | * clean up the referent. 26 | * 27 | * @param the reference value type 28 | * @param the attachment type 29 | * 30 | * @see java.lang.ref.PhantomReference 31 | * 32 | * @author David M. Lloyd 33 | * 34 | * @deprecated Use {@link io.smallrye.common.ref.PhantomReference} instead. 35 | */ 36 | @Deprecated(forRemoval = true) 37 | public class PhantomReference extends java.lang.ref.PhantomReference implements Reference, Reapable { 38 | private final A attachment; 39 | private final Reaper reaper; 40 | 41 | /** 42 | * Construct a new instance with an explicit reference queue. 43 | * 44 | * @param referent the referent 45 | * @param attachment the attachment 46 | * @param q the reference queue to use 47 | */ 48 | public PhantomReference(final T referent, final A attachment, final ReferenceQueue q) { 49 | super(referent, q); 50 | this.attachment = attachment; 51 | reaper = null; 52 | } 53 | 54 | /** 55 | * Construct a new instance with a reaper. 56 | * 57 | * @param referent the referent 58 | * @param attachment the attachment 59 | * @param reaper the reaper to use 60 | */ 61 | public PhantomReference(final T referent, final A attachment, final Reaper reaper) { 62 | super(referent, References.ReaperThread.REAPER_QUEUE); 63 | this.reaper = reaper; 64 | this.attachment = attachment; 65 | } 66 | 67 | public A getAttachment() { 68 | return attachment; 69 | } 70 | 71 | public Type getType() { 72 | return Type.PHANTOM; 73 | } 74 | 75 | public Reaper getReaper() { 76 | return reaper; 77 | } 78 | 79 | public String toString() { 80 | return "phantom reference"; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/Reapable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | /** 22 | * A reference which is reapable (can be automatically collected). 23 | * 24 | * @param the reference type 25 | * @param the reference attachment type 26 | * 27 | * @apiviz.exclude 28 | * 29 | * @author David M. Lloyd 30 | */ 31 | interface Reapable { 32 | 33 | /** 34 | * Get the associated reaper. 35 | * 36 | * @return the reaper 37 | */ 38 | Reaper getReaper(); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/Reaper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | /** 22 | * A cleaner for a dead object. 23 | * 24 | * @param the reference type 25 | * @param the reference attachment type 26 | * 27 | * @author David M. Lloyd 28 | * 29 | * @deprecated Use {@link io.smallrye.common.ref.Reaper} instead. 30 | */ 31 | @Deprecated(forRemoval = true) 32 | public interface Reaper { 33 | 34 | /** 35 | * Perform the cleanup action for a reference. 36 | * 37 | * @param reference the reference 38 | */ 39 | void reap(Reference reference); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/SoftReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | import java.lang.ref.ReferenceQueue; 22 | 23 | /** 24 | * A reapable soft reference with an attachment. If a {@link Reaper} is given, then it will be used to asynchronously 25 | * clean up the referent. 26 | * 27 | * @param the reference value type 28 | * @param the attachment type 29 | * 30 | * @see java.lang.ref.SoftReference 31 | * 32 | * @author David M. Lloyd 33 | * 34 | * @deprecated Use {@link io.smallrye.common.ref.SoftReference} instead. 35 | */ 36 | public class SoftReference extends java.lang.ref.SoftReference implements Reference, Reapable { 37 | private final A attachment; 38 | private final Reaper reaper; 39 | 40 | /** 41 | * Construct a new instance. 42 | * 43 | * @param referent the referent 44 | */ 45 | public SoftReference(final T referent) { 46 | this(referent, null, (ReferenceQueue) null); 47 | } 48 | 49 | /** 50 | * Construct a new instance. 51 | * 52 | * @param referent the referent 53 | * @param attachment the attachment 54 | */ 55 | public SoftReference(final T referent, final A attachment) { 56 | this(referent, attachment, (ReferenceQueue) null); 57 | } 58 | 59 | /** 60 | * Construct a new instance with an explicit reference queue. 61 | * 62 | * @param referent the referent 63 | * @param attachment the attachment 64 | * @param q the reference queue to use 65 | */ 66 | public SoftReference(final T referent, final A attachment, final ReferenceQueue q) { 67 | super(referent, q); 68 | reaper = null; 69 | this.attachment = attachment; 70 | } 71 | 72 | /** 73 | * Construct a new instance with a reaper. 74 | * 75 | * @param referent the referent 76 | * @param attachment the attachment 77 | * @param reaper the reaper to use 78 | */ 79 | public SoftReference(final T referent, final A attachment, final Reaper reaper) { 80 | super(referent, References.ReaperThread.REAPER_QUEUE); 81 | this.reaper = reaper; 82 | this.attachment = attachment; 83 | } 84 | 85 | public Reaper getReaper() { 86 | return reaper; 87 | } 88 | 89 | public A getAttachment() { 90 | return attachment; 91 | } 92 | 93 | public Type getType() { 94 | return Type.SOFT; 95 | } 96 | 97 | public String toString() { 98 | return "soft reference to " + String.valueOf(get()); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/StrongReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | /** 22 | * A strong reference with an attachment. Since strong references are always reachable, a reaper may not be used. 23 | * 24 | * @param the reference value type 25 | * @param the attachment type 26 | * 27 | * @author David M. Lloyd 28 | * 29 | * @deprecated Use {@link io.smallrye.common.ref.StrongReference} instead. 30 | */ 31 | @Deprecated(forRemoval = true) 32 | public class StrongReference implements Reference { 33 | 34 | private volatile T referent; 35 | private final A attachment; 36 | 37 | /** 38 | * Construct a new instance. 39 | * 40 | * @param referent the referent 41 | * @param attachment the attachment 42 | */ 43 | public StrongReference(final T referent, final A attachment) { 44 | this.referent = referent; 45 | this.attachment = attachment; 46 | } 47 | 48 | /** 49 | * Construct a new instance. 50 | * 51 | * @param referent the referent 52 | */ 53 | public StrongReference(final T referent) { 54 | this(referent, null); 55 | } 56 | 57 | public T get() { 58 | return referent; 59 | } 60 | 61 | public void clear() { 62 | referent = null; 63 | } 64 | 65 | public A getAttachment() { 66 | return attachment; 67 | } 68 | 69 | public Type getType() { 70 | return Type.STRONG; 71 | } 72 | 73 | public String toString() { 74 | return "strong reference to " + String.valueOf(get()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/WeakReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.ref; 20 | 21 | import java.lang.ref.ReferenceQueue; 22 | 23 | /** 24 | * A reapable weak reference with an attachment. If a {@link Reaper} is given, then it will be used to asynchronously 25 | * clean up the referent. 26 | * 27 | * @param the reference value type 28 | * @param the attachment type 29 | * 30 | * @see java.lang.ref.WeakReference 31 | * 32 | * @author David M. Lloyd 33 | * 34 | * @deprecated Use {@link io.smallrye.common.ref.WeakReference} instead. 35 | */ 36 | @Deprecated(forRemoval = true) 37 | public class WeakReference extends java.lang.ref.WeakReference implements Reference, Reapable { 38 | private final A attachment; 39 | private final Reaper reaper; 40 | 41 | /** 42 | * Construct a new instance. 43 | * 44 | * @param referent the referent 45 | */ 46 | public WeakReference(final T referent) { 47 | this(referent, null, (Reaper) null); 48 | } 49 | 50 | /** 51 | * Construct a new instance. 52 | * 53 | * @param referent the referent 54 | * @param attachment the attachment 55 | */ 56 | public WeakReference(final T referent, final A attachment) { 57 | this(referent, attachment, (Reaper) null); 58 | } 59 | 60 | /** 61 | * Construct a new instance with an explicit reference queue. 62 | * 63 | * @param referent the referent 64 | * @param attachment the attachment 65 | * @param q the reference queue to use 66 | */ 67 | public WeakReference(final T referent, final A attachment, final ReferenceQueue q) { 68 | super(referent, q); 69 | this.attachment = attachment; 70 | reaper = null; 71 | } 72 | 73 | /** 74 | * Construct a new instance with a reaper. 75 | * 76 | * @param referent the referent 77 | * @param attachment the attachment 78 | * @param reaper the reaper to use 79 | */ 80 | public WeakReference(final T referent, final A attachment, final Reaper reaper) { 81 | super(referent, References.ReaperThread.REAPER_QUEUE); 82 | this.attachment = attachment; 83 | this.reaper = reaper; 84 | } 85 | 86 | public A getAttachment() { 87 | return attachment; 88 | } 89 | 90 | public Type getType() { 91 | return Type.WEAK; 92 | } 93 | 94 | public Reaper getReaper() { 95 | return reaper; 96 | } 97 | 98 | public String toString() { 99 | return "weak reference to " + String.valueOf(get()); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/ref/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes which implement reference types which can be cleaned up automatically by a background thread. See 21 | * {@link org.wildfly.common.ref.Reference Reference} and its subtypes, and {@link org.wildfly.common.ref.Reaper Reaper} for more information. 22 | * 23 | * @deprecated Use {@link io.smallrye.common.ref} instead. 24 | */ 25 | @Deprecated(forRemoval = true) 26 | package org.wildfly.common.ref; -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/rpc/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2017 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes which are commonly useful for simple RPC applications. 21 | * 22 | * @author David M. Lloyd 23 | */ 24 | package org.wildfly.common.rpc; 25 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/selector/ContextClassLoaderSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.selector; 20 | 21 | /** 22 | * @author David M. Lloyd 23 | */ 24 | @Deprecated 25 | class ContextClassLoaderSelector extends Selector { 26 | 27 | public ClassLoader get() { 28 | return Thread.currentThread().getContextClassLoader(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/selector/DefaultSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.selector; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | import org.wildfly.common.context.Contextual; 27 | 28 | /** 29 | * An annotation indicating the default selector implementation class to use for a class. 30 | * 31 | * @author David M. Lloyd 32 | * 33 | * @deprecated Use {@link Contextual} instead. 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.TYPE) 37 | @Deprecated 38 | public @interface DefaultSelector { 39 | 40 | /** 41 | * The selector implementation class to use. 42 | * 43 | * @return the selector implementation class 44 | */ 45 | Class> value(); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/selector/GlobalSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.selector; 20 | 21 | import org.wildfly.common.Assert; 22 | import org.wildfly.common.context.Contextual; 23 | 24 | /** 25 | * A selector which always returns one global instance. 26 | * 27 | * @author David M. Lloyd 28 | * 29 | * @deprecated Use {@link Contextual} instead. 30 | */ 31 | @Deprecated 32 | public final class GlobalSelector extends Selector { 33 | private final T instance; 34 | 35 | /** 36 | * Construct a new instance. 37 | * 38 | * @param instance the constant instance to always return from this selector 39 | */ 40 | public GlobalSelector(final T instance) { 41 | Assert.checkNotNullParam("instance", instance); 42 | this.instance = instance; 43 | } 44 | 45 | public T get() { 46 | return instance; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/selector/ThreadLocalSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2014 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.selector; 20 | 21 | import org.wildfly.common.Assert; 22 | import org.wildfly.common.context.Contextual; 23 | 24 | /** 25 | * A thread local selector implementation. 26 | * 27 | * @author David M. Lloyd 28 | * 29 | * @deprecated Use {@link Contextual} instead. 30 | */ 31 | @Deprecated 32 | public final class ThreadLocalSelector extends Selector { 33 | private final ThreadLocal threadLocal; 34 | 35 | /** 36 | * Construct a new instance. 37 | * 38 | * @param threadLocal the thread-local to use to store the selector's value 39 | */ 40 | public ThreadLocalSelector(final ThreadLocal threadLocal) { 41 | Assert.checkNotNullParam("threadLocal", threadLocal); 42 | this.threadLocal = threadLocal; 43 | } 44 | 45 | public T get() { 46 | return threadLocal.get(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/selector/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2015 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * @author David M. Lloyd 21 | */ 22 | package org.wildfly.common.selector; 23 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/string/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2019 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes which perform various common string functions. 21 | * 22 | * @author Paul Ferraro 23 | */ 24 | 25 | package org.wildfly.common.string; 26 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/xml/FactoryConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Red Hat, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.wildfly.common.xml; 17 | 18 | /** 19 | * Constants to configure the factories. 20 | * OWASP XML 21 | * External Entity Prevention Cheatsheet 22 | * @author Boris Unckel 23 | * @since 1.6.0.Final 24 | */ 25 | final class FactoryConstants { 26 | 27 | /** 28 | * This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented 29 | * Xerces 2 only: 30 | * @see{https://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl} 31 | */ 32 | public static final String APACHE_DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"; 33 | 34 | /** 35 | * If you can't completely disable DTDs, then at least do the following. This feature has to be used together with the 36 | * following one (external-parameter-entities), otherwise it will not protect you from XXE for sure Do not include external 37 | * general entities. 38 | * Xerces 1: 39 | * @see{https://xerces.apache.org/xerces-j/features.html#external-general-entities} 40 | * 41 | * Xerces 2: 42 | * @see{https://xerces.apache.org/xerces2-j/features.html#external-general-entities} 43 | */ 44 | public static final String XML_EXTERNAL_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities"; 45 | 46 | /** 47 | * This feature has to be used together with the previous one(external-general-entities), otherwise it will not protect you from XXE for sure 48 | * Do not include external parameter entities or the external DTD subset. 49 | * Xerces: 50 | * @see{https://xerces.apache.org/xerces-j/features.html#external-parameter-entities} 51 | * 52 | * Xerces 2: 53 | * @see{https://xerces.apache.org/xerces2-j/features.html#external-parameter-entities} 54 | */ 55 | public static final String XML_EXTERNAL_PARAMETER_ENTITIES = "http://xml.org/sax/features/external-parameter-entities"; 56 | 57 | /** 58 | * Disable external DTDs as well. 59 | */ 60 | public static final String APACHE_LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; 61 | 62 | /** 63 | * No instance. 64 | */ 65 | private FactoryConstants() { 66 | throw new IllegalStateException("No instance"); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/xml/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2022 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.xml; 20 | 21 | import static java.lang.invoke.MethodHandles.lookup; 22 | import static org.jboss.logging.Logger.Level.WARN; 23 | 24 | import org.jboss.logging.BasicLogger; 25 | import org.jboss.logging.Logger; 26 | import org.jboss.logging.annotations.Cause; 27 | import org.jboss.logging.annotations.LogMessage; 28 | import org.jboss.logging.annotations.Message; 29 | import org.jboss.logging.annotations.MessageLogger; 30 | 31 | @MessageLogger(projectCode = "COM", length = 5) 32 | interface Log extends BasicLogger { 33 | 34 | Log XML_FACTORY_LOGGER = Logger.getMessageLogger(lookup(), Log.class, "org.wildfly.common.xml"); 35 | 36 | // 3100-3199 reserved for xml factory logging 37 | @LogMessage(level = WARN) 38 | @Message(id = 3100, value = "Property or feature %s not supported by %s") 39 | void xmlFactoryPropertyNotSupported(@Cause Throwable cause, String property, String factoryName); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/xml/XMLInputFactoryUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Red Hat, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.wildfly.common.xml; 17 | 18 | import static org.wildfly.common.xml.Log.XML_FACTORY_LOGGER; 19 | 20 | import javax.xml.stream.XMLInputFactory; 21 | 22 | import org.wildfly.common.annotation.NotNull; 23 | 24 | import java.util.concurrent.atomic.AtomicBoolean; 25 | 26 | /** 27 | * Factory provides {@link XMLInputFactory} with secure defaults set. Properties not supported generate a warning, but the 28 | * factory process creation will continue and return a result. 29 | * Settings based on recommendations of 30 | * Sonarcloud RSPEC-2755 and 31 | * OWASP XML 32 | * External Entity Prevention Cheatsheet. 33 | *

34 | * 35 | *

    36 | *
  • {@link javax.xml.stream.XMLInputFactory#SUPPORT_DTD} is set to false.
  • 37 | *
  • {@link javax.xml.stream.XMLInputFactory#IS_SUPPORTING_EXTERNAL_ENTITIES} is set to false.
  • 38 | *
39 | * 40 | * @author Boris Unckel 41 | * @since 1.6.0.Final 42 | */ 43 | public final class XMLInputFactoryUtil { 44 | 45 | /* 46 | * Prevent recurring log messages (per classloader). 47 | */ 48 | private static final AtomicBoolean TO_BE_LOGGED = new AtomicBoolean(true); 49 | 50 | /** 51 | * Factory generated with secure defaults. 52 | * @return an instance of the XMLInputFactory. 53 | */ 54 | @NotNull 55 | public static XMLInputFactory create() { 56 | final XMLInputFactory instance = XMLInputFactory.newInstance(); 57 | final boolean toBeLogged = TO_BE_LOGGED.compareAndSet(true, false); 58 | 59 | try { 60 | instance.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); 61 | } catch (IllegalArgumentException e) { 62 | if (toBeLogged) { 63 | XML_FACTORY_LOGGER.xmlFactoryPropertyNotSupported(e, XMLInputFactory.SUPPORT_DTD, 64 | instance.getClass().getCanonicalName()); 65 | } 66 | } 67 | 68 | try { 69 | instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); 70 | } catch (IllegalArgumentException e) { 71 | if (toBeLogged) { 72 | XML_FACTORY_LOGGER.xmlFactoryPropertyNotSupported(e, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, 73 | instance.getClass().getCanonicalName()); 74 | } 75 | } 76 | 77 | return instance; 78 | } 79 | 80 | /** 81 | * No instance. 82 | */ 83 | private XMLInputFactoryUtil() { 84 | throw new IllegalStateException("No instance"); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/wildfly/common/xml/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2022 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Classes which create XML related factories with safe defaults. 21 | * 22 | * @author Boris Unckel 23 | */ 24 | 25 | package org.wildfly.common.xml; 26 | -------------------------------------------------------------------------------- /src/test/java/org/wildfly/common/archive/ArchiveIndexTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2019 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.archive; 20 | 21 | import static org.junit.Assert.*; 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * 27 | */ 28 | public class ArchiveIndexTest { 29 | 30 | private void doIndexTest(Index index) { 31 | // simple get/put 32 | index.put(4, 1234); 33 | assertEquals(1234, index.get(4)); 34 | index.put(0, 5555); 35 | assertEquals(5555, index.get(0)); 36 | assertEquals(-1, index.get(3)); 37 | // check linear probing 38 | index.put(4, 2345); 39 | assertEquals(1234, index.get(4)); 40 | assertEquals(2345, index.get(5)); 41 | assertEquals(-1, index.get(6)); 42 | } 43 | 44 | @Test 45 | public void testTinyIndex() { 46 | doIndexTest(new TinyIndex(130)); 47 | } 48 | 49 | @Test 50 | public void testLargeIndex() { 51 | doIndexTest(new LargeIndex(112)); 52 | } 53 | 54 | @Test 55 | public void testHugeIndex() { 56 | doIndexTest(new HugeIndex(112)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/org/wildfly/common/iteration/CompositeIterableTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JBoss, Home of Professional Open Source. 3 | * Copyright 2019 Red Hat, Inc., and individual contributors 4 | * as indicated by the @author tags. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.wildfly.common.iteration; 20 | 21 | import java.util.ArrayList; 22 | import java.util.Arrays; 23 | import java.util.Collections; 24 | import java.util.Iterator; 25 | import java.util.List; 26 | import java.util.stream.Collectors; 27 | import java.util.stream.IntStream; 28 | 29 | import org.junit.Assert; 30 | import org.junit.Test; 31 | 32 | /** 33 | * Unit test for {@link CompositeIterable}. 34 | * @author Paul Ferraro 35 | */ 36 | public class CompositeIterableTestCase { 37 | 38 | @Test 39 | public void test() { 40 | List expected = IntStream.range(0, 10).mapToObj(Integer::valueOf).collect(Collectors.toList()); 41 | 42 | test(expected, new CompositeIterable<>(new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4)), Arrays.asList(5, 6, 7, 8, 9))); 43 | test(expected, new CompositeIterable<>(new ArrayList<>(Arrays.asList(0, 1)), Arrays.asList(2, 3), Arrays.asList(4, 5), Arrays.asList(6, 7), Arrays.asList(8, 9))); 44 | test(expected, new CompositeIterable<>(Collections.emptyList(), new ArrayList<>(expected), Collections.emptyList())); 45 | } 46 | 47 | @Test 48 | public void testRemove() { 49 | 50 | } 51 | 52 | static void test(Iterable expected, Iterable subject) { 53 | Assert.assertEquals(expected.hashCode(), subject.hashCode()); 54 | Assert.assertEquals(expected.toString(), subject.toString()); 55 | 56 | Iterator subjectIterator = subject.iterator(); 57 | Iterator expectedIterator = expected.iterator(); 58 | while (expectedIterator.hasNext()) { 59 | Assert.assertTrue(subjectIterator.hasNext()); 60 | Assert.assertEquals(expectedIterator.next(), subjectIterator.next()); 61 | } 62 | Assert.assertFalse(subjectIterator.hasNext()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/org/wildfly/common/lock/SpinLockTest.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.lock; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | import java.util.concurrent.LinkedBlockingQueue; 5 | import java.util.concurrent.ThreadPoolExecutor; 6 | import java.util.concurrent.TimeUnit; 7 | import java.util.concurrent.locks.LockSupport; 8 | 9 | import org.junit.AfterClass; 10 | import org.junit.Assert; 11 | import org.junit.BeforeClass; 12 | import org.junit.Test; 13 | 14 | /** 15 | * 16 | */ 17 | public class SpinLockTest { 18 | 19 | static final int POOL_SIZE = 16; 20 | static final int SPIN_COUNT = 20480; 21 | 22 | static ThreadPoolExecutor executor; 23 | 24 | @BeforeClass 25 | public static void initialize() { 26 | executor = new ThreadPoolExecutor(POOL_SIZE, POOL_SIZE, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue<>()); 27 | executor.prestartAllCoreThreads(); 28 | } 29 | 30 | @Test 31 | public void ensureLockWorks() { 32 | SpinLock lock = new SpinLock(); 33 | int[] holder = new int[1]; 34 | CountDownLatch latch = new CountDownLatch(POOL_SIZE); 35 | for (int i = 0; i < POOL_SIZE; i ++) { 36 | executor.execute(() -> { 37 | LockSupport.parkNanos(100_000L); 38 | for (int j = 0; j < SPIN_COUNT; j ++) { 39 | lock.lock(); 40 | try { 41 | holder[0] ++; 42 | } finally { 43 | lock.unlock(); 44 | } 45 | } 46 | latch.countDown(); 47 | }); 48 | } 49 | for (;;) try { 50 | latch.await(); 51 | break; 52 | } catch (InterruptedException ignored) { 53 | } 54 | Assert.assertEquals("Wrong final count", POOL_SIZE * SPIN_COUNT, holder[0]); 55 | } 56 | 57 | 58 | @AfterClass 59 | public static void shutdown() { 60 | ThreadPoolExecutor executor = SpinLockTest.executor; 61 | if (executor != null) { 62 | executor.shutdownNow(); 63 | for (;;) try { 64 | executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 65 | break; 66 | } catch (InterruptedException ignored) { 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/org/wildfly/common/string/CompositeCharSequenceTestCase.java: -------------------------------------------------------------------------------- 1 | package org.wildfly.common.string; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | import static org.junit.Assert.fail; 6 | 7 | import java.util.stream.IntStream; 8 | 9 | import org.junit.Test; 10 | import org.wildfly.common.string.CompositeCharSequence; 11 | 12 | /** 13 | * Unit test for {@link CompositeCharSequence}. 14 | * @author Paul Ferraro 15 | */ 16 | public class CompositeCharSequenceTestCase { 17 | private final CharSequence sequence = new CompositeCharSequence("01", "23", "45"); 18 | 19 | @Test 20 | public void length() { 21 | assertEquals(6, this.sequence.length()); 22 | } 23 | 24 | @Test 25 | public void string() { 26 | assertEquals("012345", this.sequence.toString()); 27 | } 28 | 29 | @Test 30 | public void charAt() { 31 | for (int i = -1; i < 7; ++i) { 32 | try { 33 | char result = this.sequence.charAt(i); 34 | if ((i < 0) || (i >= 6)) { 35 | fail(String.format("charAt(%d) returned '%s', but IndexOutOfBoundsException was expected", i, result)); 36 | } 37 | assertEquals('0' + i, result); 38 | } catch (IndexOutOfBoundsException e) { 39 | if ((i >= 0) && (i < 6)) { 40 | fail(String.format("Unexpected IndexOutOfBoundsException during charAt(%d)", i)); 41 | } 42 | } 43 | } 44 | } 45 | 46 | @Test 47 | public void subSequence() { 48 | for (int i = -1; i < 7; ++i) { 49 | for (int j = -1; j <= 7; ++j) { 50 | try { 51 | CharSequence result = this.sequence.subSequence(i, j); 52 | if ((i < 0) || (i > j) || (j > 6)) { 53 | fail(String.format("subSequence(%d, %d) returned '%s', but IndexOutOfBoundsException was expected", i, j, result.toString())); 54 | } 55 | StringBuilder expected = new StringBuilder(j - i); 56 | IntStream.range(i, j).forEach(value -> expected.append((char) ('0' + value))); 57 | assertEquals(expected.toString(), result.toString()); 58 | } catch (IndexOutOfBoundsException e) { 59 | if ((i >= 0) && (j <= 6) && (i <= j)) { 60 | fail(String.format("Unexpected IndexOutOfBoundsException during subSequence(%d, %d)", i, j)); 61 | } 62 | } 63 | } 64 | } 65 | } 66 | 67 | @Test 68 | public void equals() { 69 | assertTrue(this.sequence.equals("012345")); 70 | assertTrue(this.sequence.equals(new CompositeCharSequence("012", "345"))); 71 | assertTrue(this.sequence.equals(new CompositeCharSequence("012345"))); 72 | } 73 | 74 | @Test 75 | public void hash() { 76 | int hashCode = this.sequence.hashCode(); 77 | assertEquals("012345".hashCode(), hashCode); 78 | assertEquals(new CompositeCharSequence("012", "345").hashCode(), hashCode); 79 | assertEquals(new CompositeCharSequence("012345").hashCode(), hashCode); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/org/wildfly/common/xml/XmlFactoriesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Red Hat, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.wildfly.common.xml; 17 | 18 | import static org.junit.Assert.assertNotNull; 19 | 20 | import javax.xml.parsers.DocumentBuilder; 21 | import javax.xml.parsers.DocumentBuilderFactory; 22 | import javax.xml.parsers.ParserConfigurationException; 23 | import javax.xml.parsers.SAXParser; 24 | import javax.xml.parsers.SAXParserFactory; 25 | import javax.xml.stream.XMLInputFactory; 26 | import javax.xml.transform.Transformer; 27 | import javax.xml.transform.TransformerConfigurationException; 28 | import javax.xml.transform.TransformerFactory; 29 | 30 | import org.junit.Test; 31 | import org.xml.sax.SAXException; 32 | import org.xml.sax.XMLReader; 33 | 34 | 35 | /** 36 | * @author Boris Unckel 37 | * 38 | */ 39 | public class XmlFactoriesTest { 40 | 41 | @Test 42 | public void testDocumentBuilderFactoryUtil() throws ParserConfigurationException { 43 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtil.create(); 44 | assertNotNull(documentBuilderFactory); 45 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 46 | assertNotNull(documentBuilder); 47 | } 48 | 49 | @Test 50 | public void testSAXParserFactoryUtil() throws ParserConfigurationException, SAXException { 51 | SAXParserFactory saxParserFactory = SAXParserFactoryUtil.create(); 52 | assertNotNull(saxParserFactory); 53 | SAXParser saxParser = saxParserFactory.newSAXParser(); 54 | assertNotNull(saxParser); 55 | } 56 | 57 | @Test 58 | public void testTransformerFactoryUtil() throws TransformerConfigurationException { 59 | TransformerFactory transformerFactory = TransformerFactoryUtil.create(); 60 | assertNotNull(transformerFactory); 61 | Transformer transformer = transformerFactory.newTransformer(); 62 | assertNotNull(transformer); 63 | } 64 | 65 | @Test 66 | public void testXMLInputFactoryUtil() { 67 | XMLInputFactory xmlInputFactory = XMLInputFactoryUtil.create(); 68 | assertNotNull(xmlInputFactory); 69 | } 70 | 71 | @Test 72 | public void testXMLReaderFactoryUtil() throws SAXException { 73 | XMLReader xmlReader = XMLReaderFactoryUtil.create(); 74 | assertNotNull(xmlReader); 75 | } 76 | 77 | } 78 | --------------------------------------------------------------------------------