Repository: commons-lang Updated Branches: refs/heads/master b06f13ff0 -> 30d4070af
LANG-1313: Add ArchUtils - an utility class for the "os.arch" system property (closes #231) Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/90f0a680 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/90f0a680 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/90f0a680 Branch: refs/heads/master Commit: 90f0a680adf3eaa95e46e96d28ecd6848c74bfdd Parents: 1674c95 Author: Tomschi <toms...@users.noreply.github.com> Authored: Tue Mar 7 21:11:09 2017 +0100 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Mon Mar 27 20:40:28 2017 +0200 ---------------------------------------------------------------------- .../org/apache/commons/lang3/ArchUtils.java | 134 ++++++++++++++++ .../apache/commons/lang3/arch/Processor.java | 136 ++++++++++++++++ .../apache/commons/lang3/arch/package-info.java | 20 +++ .../org/apache/commons/lang3/ArchUtilsTest.java | 160 +++++++++++++++++++ 4 files changed, 450 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/90f0a680/src/main/java/org/apache/commons/lang3/ArchUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/ArchUtils.java b/src/main/java/org/apache/commons/lang3/ArchUtils.java new file mode 100644 index 0000000..41b770b --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/ArchUtils.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3; + +import org.apache.commons.lang3.arch.Processor; + +import java.util.HashMap; +import java.util.Map; + +/** + * An utility class for the os.arch System Property. The class defines methods for + * identifying the architecture of the current JVM. + * <p> + * Important: The os.arch System Property returns the architecture used by the JVM + * not of the operating system. + * </p> + */ +public class ArchUtils { + + private static final Map<String, Processor> ARCH_TO_PROCESSOR; + + static { + ARCH_TO_PROCESSOR = new HashMap<>(); + init(); + } + + private static final void init() { + init_X86_32Bit(); + init_X86_64Bit(); + init_IA64_32Bit(); + init_IA64_64Bit(); + init_PPC_32Bit(); + init_PPC_64Bit(); + } + + private static final void init_X86_32Bit() { + Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.X86); + addProcessors(processor, "x86", "i386", "i486", "i586", "i686", "pentium"); + } + + private static final void init_X86_64Bit() { + Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.X86); + addProcessors(processor, "x86_64", "amd64", "em64t", "universal"); + } + + private static final void init_IA64_32Bit() { + Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.IA_64); + addProcessors(processor, "ia64_32", "ia64n"); + } + + private static final void init_IA64_64Bit() { + Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.IA_64); + addProcessors(processor, "ia64", "ia64w"); + } + + private static final void init_PPC_32Bit() { + Processor processor = new Processor(Processor.Arch.BIT_32, Processor.Type.PPC); + addProcessors(processor, "ppc", "power", "powerpc", "power_pc", "power_rs"); + } + + private static final void init_PPC_64Bit() { + Processor processor = new Processor(Processor.Arch.BIT_64, Processor.Type.PPC); + addProcessors(processor, "ppc64", "power64", "powerpc64", "power_pc64", "power_rs64"); + } + + /** + * Adds the given {@link Processor} with the given key {@link String} to the map. + * + * @param key The key as {@link String}. + * @param processor The {@link Processor} to add. + * @throws IllegalStateException If the key already exists. + */ + private static final void addProcessor(String key, Processor processor) throws IllegalStateException { + if (!ARCH_TO_PROCESSOR.containsKey(key)) { + ARCH_TO_PROCESSOR.put(key, processor); + } else { + String msg = "Key " + key + " already exists in processor map"; + throw new IllegalStateException(msg); + } + } + + /** + * Adds the given {@link Processor} with the given keys to the map. + * + * @param keys The keys. + * @param processor The {@link Processor} to add. + * @throws IllegalStateException If the key already exists. + */ + private static final void addProcessors(Processor processor, String... keys) throws IllegalStateException { + for (String key : keys) { + addProcessor(key, processor); + } + } + + /** + * Returns a {@link Processor} object of the current JVM. + * + * <p> + * Important: The os.arch System Property returns the architecture used by the JVM + * not of the operating system. + * </p> + * + * @return A {@link Processor} when supported, else <code>null</code>. + */ + public static final Processor getProcessor() { + return getProcessor(SystemUtils.OS_ARCH); + } + + /** + * Returns a {@link Processor} object the given value {@link String}. The {@link String} must be + * like a value returned by the os.arch System Property. + * + * @param value A {@link String} like a value returned by the os.arch System Property. + * @return A {@link Processor} when it exists, else <code>null</code>. + */ + public static final Processor getProcessor(String value) { + return ARCH_TO_PROCESSOR.get(value); + } + +} http://git-wip-us.apache.org/repos/asf/commons-lang/blob/90f0a680/src/main/java/org/apache/commons/lang3/arch/Processor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/arch/Processor.java b/src/main/java/org/apache/commons/lang3/arch/Processor.java new file mode 100644 index 0000000..e37b3f6 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/arch/Processor.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.arch; + +/** + * The {@link Processor} represents a microprocessor and defines + * some properties like architecture and type of the microprocessor. + */ +public class Processor { + + /** + * The {@link Arch} enum defines the architecture of + * a microprocessor. The architecture represents the bit value + * of the microprocessor. + * The following architectures are defined: + * <ul> + * <li>32 bit</li> + * <li>64 bit</li> + * <li>unknown</li> + * </ul> + */ + public enum Arch { + BIT_32, BIT_64, UNKNOWN + } + + /** + * The {@link Type} enum defines types of a microprocessor. + * The following types are defined: + * <ul> + * <li>x86</li> + * <li>ia64</li> + * <li>ppc</li> + * <li>unknown</li> + * </ul> + */ + public enum Type { + X86, IA_64, PPC, UNKNOWN + } + + private final Arch arch; + private final Type type; + + /** + * Constructs a {@link Processor}object with the given + * parameters. + * + * @param arch The processor architecture. + * @param type The processor type. + */ + public Processor(Arch arch, Type type) { + this.arch = arch; + this.type = type; + } + + /** + * Returns the processor architecture as an {@link Arch} enum. + * The processor architecture defines, if the processor has + * a 32 or 64 bit architecture. + * + * @return A {@link Arch} enum. + */ + public Arch getArch() { + return arch; + } + + /** + * Returns the processor type as {@link Type} enum. + * The processor type defines, if the processor is for example + * a x86 or PPA. + * + * @return A {@link Type} enum. + */ + public Type getType() { + return type; + } + + /** + * Checks if {@link Processor} is 32 bit. + * + * @return <code>true</code>, if {@link Processor} is {@link Arch#BIT_32}, else <code>false</code>. + */ + public boolean is32Bit() { + return Arch.BIT_32.equals(arch); + } + + /** + * Checks if {@link Processor} is 64 bit. + * + * @return <code>true</code>, if {@link Processor} is {@link Arch#BIT_64}, else <code>false</code>. + */ + public boolean is64Bit() { + return Arch.BIT_64.equals(arch); + } + + /** + * Checks if {@link Processor} is type of x86. + * + * @return <code>true</code>, if {@link Processor} is {@link Type#X86}, else <code>false</code>. + */ + public boolean isX86() { + return Type.X86.equals(type); + } + + /** + * Checks if {@link Processor} is type of Intel Itanium. + * + * @return <code>true</code>. if {@link Processor} is {@link Type#IA_64}, else <code>false</code>. + */ + public boolean isIA64() { + return Type.IA_64.equals(type); + } + + /** + * Checks if {@link Processor} is type of Power PC. + * + * @return <code>true</code>. if {@link Processor} is {@link Type#PPC}, else <code>false</code>. + */ + public boolean isPPC() { + return Type.PPC.equals(type); + } + +} http://git-wip-us.apache.org/repos/asf/commons-lang/blob/90f0a680/src/main/java/org/apache/commons/lang3/arch/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/arch/package-info.java b/src/main/java/org/apache/commons/lang3/arch/package-info.java new file mode 100644 index 0000000..9b832e3 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/arch/package-info.java @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Provides classes to work with the values of the os.arch system property. + */ +package org.apache.commons.lang3.arch; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-lang/blob/90f0a680/src/test/java/org/apache/commons/lang3/ArchUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/ArchUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArchUtilsTest.java new file mode 100644 index 0000000..fb2a57a --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/ArchUtilsTest.java @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3; + +import org.apache.commons.lang3.arch.Processor; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * Test class for {@link ArchUtils}. + * + * @author Tomschi + */ +public class ArchUtilsTest { + + private static final String X86 = "x86"; + private static final String X86_64 = "x86_64"; + private static final String IA64 = "ia64"; + private static final String IA64_32 = "ia64_32"; + private static final String PPC = "ppc"; + private static final String PPC64 = "ppc64"; + + @Test + public void testIs32BitJVM() { + Processor processor = ArchUtils.getProcessor(X86); + assertEqualsArchNotNull(Processor.Arch.BIT_32, processor); + assertTrue(processor.is32Bit()); + + processor = ArchUtils.getProcessor(IA64_32); + assertEqualsArchNotNull(Processor.Arch.BIT_32, processor); + assertTrue(processor.is32Bit()); + + processor = ArchUtils.getProcessor(PPC); + assertEqualsArchNotNull(Processor.Arch.BIT_32, processor); + processor.is32Bit(); + + processor = ArchUtils.getProcessor(X86_64); + assertNotEqualsArchNotNull(Processor.Arch.BIT_32, processor); + assertFalse(processor.is32Bit()); + + processor = ArchUtils.getProcessor(PPC64); + assertNotEqualsArchNotNull(Processor.Arch.BIT_32, processor); + assertFalse(processor.is32Bit()); + + processor = ArchUtils.getProcessor(IA64); + assertNotEqualsArchNotNull(Processor.Arch.BIT_32, processor); + assertFalse(processor.is32Bit()); + } + + @Test + public void testIs64BitJVM() { + Processor processor = ArchUtils.getProcessor(X86_64); + assertEqualsArchNotNull(Processor.Arch.BIT_64, processor); + assertTrue(processor.is64Bit()); + + processor = ArchUtils.getProcessor(PPC64); + assertEqualsArchNotNull(Processor.Arch.BIT_64, processor); + assertTrue(processor.is64Bit()); + + processor = ArchUtils.getProcessor(IA64); + assertEqualsArchNotNull(Processor.Arch.BIT_64, processor); + assertTrue(processor.is64Bit()); + + processor = ArchUtils.getProcessor(X86); + assertNotEqualsArchNotNull(Processor.Arch.BIT_64, processor); + assertFalse(processor.is64Bit()); + + processor = ArchUtils.getProcessor(PPC); + assertNotEqualsArchNotNull(Processor.Arch.BIT_64, processor); + assertFalse(processor.is64Bit()); + + processor = ArchUtils.getProcessor(IA64_32); + assertNotEqualsArchNotNull(Processor.Arch.BIT_64, processor); + assertFalse(processor.is64Bit()); + } + + @Test + public void testArch() { + Processor processor = ArchUtils.getProcessor(X86); + assertEqualsTypeNotNull(Processor.Type.X86, processor); + assertTrue(processor.isX86()); + assertNotEqualsTypeNotNull(Processor.Type.PPC, processor); + assertFalse(processor.isPPC()); + + processor = ArchUtils.getProcessor(X86_64); + assertEqualsTypeNotNull(Processor.Type.X86, processor); + assertTrue(processor.isX86()); + + processor = ArchUtils.getProcessor(IA64_32); + assertEqualsTypeNotNull(Processor.Type.IA_64, processor); + assertTrue(processor.isIA64()); + + processor = ArchUtils.getProcessor(IA64); + assertEqualsTypeNotNull(Processor.Type.IA_64, processor); + assertTrue(processor.isIA64()); + assertNotEqualsTypeNotNull(Processor.Type.X86, processor); + assertFalse(processor.isX86()); + + processor = ArchUtils.getProcessor(PPC); + assertEqualsTypeNotNull(Processor.Type.PPC, processor); + assertTrue(processor.isPPC()); + assertNotEqualsTypeNotNull(Processor.Type.IA_64, processor); + assertFalse(processor.isIA64()); + + processor = ArchUtils.getProcessor(PPC64); + assertEqualsTypeNotNull(Processor.Type.PPC, processor); + assertTrue(processor.isPPC()); + } + + @Test + public void testGetProcessor() { + assertNotNull(ArchUtils.getProcessor(X86)); + assertNull(ArchUtils.getProcessor("NA")); + } + + private void assertEqualsArchNotNull(Processor.Arch arch, Processor processor) { + assertNotNull(arch); + assertNotNull(processor); + assertEquals(arch, processor.getArch()); + } + + private void assertNotEqualsArchNotNull(Processor.Arch arch, Processor processor) { + assertNotNull(arch); + assertNotNull(processor); + assertNotEquals(arch, processor.getArch()); + } + + private void assertEqualsTypeNotNull(Processor.Type type, Processor processor) { + assertNotNull(type); + assertNotNull(processor); + assertEquals(type, processor.getType()); + } + + private void assertNotEqualsTypeNotNull(Processor.Type type, Processor processor) { + assertNotNull(type); + assertNotNull(processor); + assertNotEquals(type, processor.getType()); + } + +}