summaryrefslogtreecommitdiff
path: root/israfil-foundation-core/src/main/java
diff options
context:
space:
mode:
authorBenjamin Culkin <scorpress@gmail.com>2024-05-27 11:38:33 -0400
committerBenjamin Culkin <scorpress@gmail.com>2024-05-27 11:38:33 -0400
commit7c279747beb43c7e88633a6228a155a30e6834f7 (patch)
tree511176048944fa7332dc1a163a6148c46e7c61b3 /israfil-foundation-core/src/main/java
Initial import
Diffstat (limited to 'israfil-foundation-core/src/main/java')
-rwxr-xr-xisrafil-foundation-core/src/main/java/net/israfil/foundation/core/Copyright.java59
-rw-r--r--israfil-foundation-core/src/main/java/net/israfil/foundation/core/Counter.java52
-rw-r--r--israfil-foundation-core/src/main/java/net/israfil/foundation/core/SimpleCounter.java54
-rw-r--r--israfil-foundation-core/src/main/java/net/israfil/foundation/core/Strings.java58
-rw-r--r--israfil-foundation-core/src/main/java/net/israfil/foundation/core/Types.java195
5 files changed, 418 insertions, 0 deletions
diff --git a/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Copyright.java b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Copyright.java
new file mode 100755
index 0000000..203793c
--- /dev/null
+++ b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Copyright.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2006-2009 Israfil Consulting Services Corporation
+ * Copyright (c) 2006-2009 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Copyright.java 640 2009-01-04 16:48:31Z christianedwardgruber $
+ */
+package net.israfil.foundation.core;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * An attribute used to tag the copyright year, owner, and license. This
+ * is purely for documentation purposes, and it has only a SOURCE scope.
+ *
+ * For example:
+ * <pre>
+ * @Copyright(years={"2003","2004","2005"} owner="Israfil Consulting Services Corporation" license="BSD")
+ * </pre>
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ *
+ * @deprecated
+ */
+@Documented
+@Retention(RetentionPolicy.SOURCE)
+public @interface Copyright {
+ String[] years();
+ String owner();
+ String license();
+}
diff --git a/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Counter.java b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Counter.java
new file mode 100644
index 0000000..e87b86a
--- /dev/null
+++ b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Counter.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Counter.java 618 2008-04-14 14:03:03Z christianedwardgruber $
+ */
+package net.israfil.foundation.core;
+
+
+/**
+ * A simple counter interface
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public interface Counter {
+
+ public int getCount();
+
+ public void increment();
+
+ public void decrement();
+
+ public void reset();
+
+}
diff --git a/israfil-foundation-core/src/main/java/net/israfil/foundation/core/SimpleCounter.java b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/SimpleCounter.java
new file mode 100644
index 0000000..042f623
--- /dev/null
+++ b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/SimpleCounter.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SimpleCounter.java 618 2008-04-14 14:03:03Z christianedwardgruber $
+ */
+package net.israfil.foundation.core;
+
+
+/**
+ * A simple counter interface
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class SimpleCounter implements Counter {
+
+ private int count = 0;
+
+ public int getCount() { return count; }
+
+ public void increment() { count++; }
+
+ public void decrement() { count--; }
+
+ public void reset() { count = 0; }
+
+}
diff --git a/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Strings.java b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Strings.java
new file mode 100644
index 0000000..edde901
--- /dev/null
+++ b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Strings.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * $Id: Strings.java 617 2008-04-14 13:46:15Z christianedwardgruber $
+ */
+package net.israfil.foundation.core;
+
+import java.util.Arrays;
+
+/**
+ * A Utility class that provides general string manipulation help.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ * @author Latest: $Author: christianedwardgruber $
+ * @version $Revision: 617 $
+ */
+public class Strings {
+
+ public static String camel(String string) {
+ if (string == null) return null;
+ if (string.length() < 1) return string;
+ StringBuffer sb = new StringBuffer(string);
+ sb.replace(0,1,sb.substring(0,1).toUpperCase());
+ return sb.toString();
+ }
+
+ public static String camelAllWords(String string, String separator) {
+ StringBuffer sb = new StringBuffer();
+ boolean first = true;
+ for (String _string : string.split(separator)) {
+ if (!first) sb.append(separator);
+ else first = false;
+ sb.append(camel(_string));
+ }
+ return sb.toString();
+ }
+
+ public static String concatenate(String separator, Iterable<String> strings) {
+ if (strings == null) return "";
+ boolean first = true;
+ StringBuffer sb = new StringBuffer();
+ for (String string : strings) {
+ if (first) {
+ first = false;
+ sb.append(string);
+ } else {
+ sb.append(separator).append(string);
+ }
+ }
+ return sb.toString();
+ }
+ public static String concatenate(String separator, String ... strings) {
+ if (strings == null) return "";
+ return concatenate(separator,Arrays.asList(strings));
+ }
+}
diff --git a/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Types.java b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Types.java
new file mode 100644
index 0000000..f720fe9
--- /dev/null
+++ b/israfil-foundation-core/src/main/java/net/israfil/foundation/core/Types.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * $Id: Types.java 644 2009-01-04 18:02:21Z christianedwardgruber $
+ */
+package net.israfil.foundation.core;
+
+/**
+ * A Utility class that provides type-conversion facilities. For
+ * example, this class provides well-deliniated logic for taking
+ * a generic object and converting it into another concrete type, such
+ * as a String. Another example would be transforming the string "yes"
+ * into a boolean in a well-defined, well-ordered way.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ * @author Latest: $Author: christianedwardgruber $
+ * @version $Revision: 644 $
+ */
+public class Types {
+
+ private static final byte BOOLEAN_FALSE_NUMERIC = 0;
+ private static final byte BOOLEAN_TRUE_NUMERIC = 1;
+
+ /**
+ * A delegate interface that, if provided to the convert(String,Class,Converter)
+ * method will add fallback conversions. This allows the existing
+ * convert method to be extended without subclassing, overriding, or
+ * modifying source.
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+ public static interface Converter {
+ public Object convert(Object val, Class<?> aClass);
+ }
+
+ /**
+ * A method to convert an object to another object of a given type.
+ * @param value The object to be converted.
+ * @param aClass The class to which you wish to convert the object.
+ * @return The converted object.
+ */
+ public static Object convert(Object value, Class<?> aClass) {
+ return convert(value,aClass,null);
+ }
+
+ /**
+ * A method to convert an object to another object of a given type.
+ * @param value The object to be converted.
+ * @param aClass The class to which you wish to convert the object.
+ * @param converter An optional delegate that will convert the object if it fails - for extensibility.
+ * @return The converted object.
+ */
+ public static Object convert(Object value, Class<?> aClass,
+ Converter converter) {
+ if (value == null) return null;
+ if (aClass == null) throw new IllegalArgumentException("Cannot convert to a null class.");
+ if (Void.class.isAssignableFrom(aClass)) throw new IllegalArgumentException("Cannot convert to void.");
+ if (aClass.isInstance(value) ) return value;
+
+ if (aClass.isAssignableFrom(String.class)) {
+
+ // TODO: Insert any specific conversions
+ return String.valueOf(value);
+ } else if (Boolean.class.isAssignableFrom(aClass)) {
+ if (value instanceof String) {
+ if (((String)value).equalsIgnoreCase("Yes")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("True")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("Y")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("T")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("1")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("Aye")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("Yar")) return Boolean.TRUE;
+ if (((String)value).equalsIgnoreCase("No")) return Boolean.FALSE;
+ if (((String)value).equalsIgnoreCase("False")) return Boolean.FALSE;
+ if (((String)value).equalsIgnoreCase("N")) return Boolean.FALSE;
+ if (((String)value).equalsIgnoreCase("F")) return Boolean.FALSE;
+ if (((String)value).equalsIgnoreCase("0")) return Boolean.FALSE;
+ if (((String)value).equalsIgnoreCase("Avast")) return Boolean.FALSE;
+ if (((String)value).equalsIgnoreCase("Nay")) return Boolean.FALSE;
+ throw new IllegalArgumentException("Cannot convert value '"+value+"' to a java.lang.Boolean");
+ }
+ if(value instanceof Number) {
+ return (((Number)value).intValue() != 0) ? Boolean.TRUE : Boolean.FALSE;
+ }
+ } else if (aClass.isAssignableFrom(Byte.class)) {
+ if (value instanceof String) return Byte.valueOf((String)value);
+ if (value instanceof Number) return new Byte(((Number)value).byteValue());
+ if (value instanceof Boolean) return new Byte(((Boolean)value).booleanValue() ? BOOLEAN_TRUE_NUMERIC : BOOLEAN_FALSE_NUMERIC);
+ } else if (aClass.isAssignableFrom(Short.class)) {
+ if (value instanceof String) return Short.valueOf((String)value);
+ if (value instanceof Number) return new Short(((Number)value).shortValue());
+ if (value instanceof Boolean) return new Short(((Boolean)value).booleanValue() ? BOOLEAN_TRUE_NUMERIC : BOOLEAN_FALSE_NUMERIC);
+ } else if (aClass.isAssignableFrom(Integer.class)) {
+ if (value instanceof String) return Integer.valueOf((String)value);
+ if (value instanceof Number) return new Integer(((Number)value).intValue());
+ if (value instanceof Boolean) return new Integer(((Boolean)value).booleanValue() ? BOOLEAN_TRUE_NUMERIC : BOOLEAN_FALSE_NUMERIC);
+ } else if (aClass.isAssignableFrom(Long.class)) {
+ if (value instanceof String) return Long.valueOf((String)value);
+ if (value instanceof Number) return new Long(((Number)value).longValue());
+ if (value instanceof Boolean) return new Long(((Boolean)value).booleanValue() ? BOOLEAN_TRUE_NUMERIC : BOOLEAN_FALSE_NUMERIC);
+ } else if (aClass.isAssignableFrom(Float.class)) {
+ if (value instanceof String) return Float.valueOf((String)value);
+ if (value instanceof Number) return new Float(((Number)value).floatValue());
+ if (value instanceof Boolean) return new Float(((Boolean)value).booleanValue() ? BOOLEAN_TRUE_NUMERIC : BOOLEAN_FALSE_NUMERIC);
+ } else if (aClass.isAssignableFrom(Double.class)) {
+ if (value instanceof String) return Double.valueOf((String)value);
+ if (value instanceof Number) return new Double(((Number)value).doubleValue());
+ if (value instanceof Boolean) return new Double(((Boolean)value).booleanValue() ? BOOLEAN_TRUE_NUMERIC : BOOLEAN_FALSE_NUMERIC);
+ } else if (aClass.isAssignableFrom(Character.class)) {
+ if (value instanceof String) return new Character(((String)value).charAt(0));
+ if (value instanceof Number) return new Character((char)((Number)value).byteValue());
+ if (value instanceof Boolean) return new Character(((Boolean)value).booleanValue() ? 'T' : 'F');
+ }
+
+ if (value instanceof String) {
+ String stringVal = (String)value;
+ if (Float.class.isAssignableFrom(aClass)) return new Float((String)value);
+ if (Double.class.isAssignableFrom(aClass)) return new Double((String)value);
+ if (Byte.class.isAssignableFrom(aClass)) return new Byte((String)value);
+ if (Short.class.isAssignableFrom(aClass)) return new Short((String)value);
+ if (Integer.class.isAssignableFrom(aClass)) return new Integer((String)value);
+ if (Long.class.isAssignableFrom(aClass)) return new Long((String)value);
+ if (Character.class.isAssignableFrom(aClass)) return new Character(stringVal.charAt(0));
+ }
+
+ if (converter != null) return converter.convert(value,aClass);
+ throw new IllegalArgumentException("Don't know how to convert a String to a " + aClass);
+ }
+
+ public static Object convert(boolean object, Class<?> aClass) {
+ return convert(new Boolean(object),aClass);
+ }
+
+ public static Object convert(byte object, Class<?> aClass) {
+ return convert(new Byte(object),aClass);
+ }
+
+ public static Object convert(short object, Class<?> aClass) {
+ return convert(new Short(object),aClass);
+ }
+
+ public static Object convert(int object, Class<?> aClass) {
+ return convert(new Integer(object),aClass);
+ }
+
+ public static Object convert(long object, Class<?> aClass) {
+ return convert(new Long(object),aClass);
+ }
+
+ public static Object convert(float object, Class<?> aClass) {
+ return convert(new Float(object),aClass);
+ }
+
+ public static Object convert(double object, Class<?> aClass) {
+ return convert(new Double(object),aClass);
+ }
+
+ public static Object convert(char object, Class<?> aClass) {
+ return convert(new Character(object),aClass);
+ }
+
+ public static boolean convertToBoolean(Object object) {
+ return ((Boolean)convert(object,Boolean.class)).booleanValue();
+ }
+
+ public static byte convertToByte(Object object) {
+ return ((Number)convert(object,Byte.class)).byteValue();
+ }
+
+ public static short convertToShort(Object object) {
+ return ((Number)convert(object,Short.class)).shortValue();
+ }
+
+ public static int convertToInt(Object object) {
+ return ((Number)convert(object,Integer.class)).intValue();
+ }
+
+ public static long convertToLong(Object object) {
+ return ((Number)convert(object,Long.class)).longValue();
+ }
+
+ public static float convertToFloat(Object object) {
+ return ((Number)convert(object,Float.class)).floatValue();
+ }
+
+ public static double convertToDouble(Object object) {
+ return ((Number)convert(object,Double.class)).doubleValue();
+ }
+
+ public static char convertToChar(Object object) {
+ return ((Character)convert(object,Character.class)).charValue();
+ }
+
+}