001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.util;
033:
034: import java.util.ArrayList;
035: import java.util.Arrays;
036: import java.util.Collection;
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.Map;
040:
041: import com.vividsolutions.jts.util.Assert;
042:
043: /**
044: * Utilities to support the Java language.
045: */
046: public class LangUtil {
047: private static Map primitiveToWrapperMap = new HashMap() {
048:
049: {
050: put(byte.class, Byte.class);
051: put(char.class, Character.class);
052: put(short.class, Short.class);
053: put(int.class, Integer.class);
054: put(long.class, Long.class);
055: put(float.class, Float.class);
056: put(double.class, Double.class);
057: put(boolean.class, Boolean.class);
058: }
059: };
060:
061: public static String emptyStringIfNull(String s) {
062: return (s == null) ? "" : s;
063: }
064:
065: /**
066: * Useful because an expression used to generate o need only be
067: * evaluated once.
068: */
069: public static Object ifNull(Object o, Object alternative) {
070: return (o == null) ? alternative : o;
071: }
072:
073: public static Object ifNotNull(Object o, Object alternative) {
074: return (o != null) ? alternative : o;
075: }
076:
077: public static Class toPrimitiveWrapperClass(Class primitiveClass) {
078: return (Class) primitiveToWrapperMap.get(primitiveClass);
079: }
080:
081: public static boolean isPrimitive(Class c) {
082: return primitiveToWrapperMap.containsKey(c);
083: }
084:
085: public static boolean bothNullOrEqual(Object a, Object b) {
086: return (a == null && b == null)
087: || (a != null && b != null && a.equals(b));
088: }
089:
090: public static Object newInstance(Class c) {
091: try {
092: return c.newInstance();
093: } catch (Exception e) {
094: Assert.shouldNeverReachHere(e.toString());
095: return null;
096: }
097: }
098:
099: public static Collection classesAndInterfaces(Class c) {
100: ArrayList classesAndInterfaces = new ArrayList();
101: classesAndInterfaces.add(c);
102: super classes(c, classesAndInterfaces);
103: for (Iterator i = new ArrayList(classesAndInterfaces)
104: .iterator(); i.hasNext();) {
105: Class x = (Class) i.next();
106: classesAndInterfaces.addAll(Arrays
107: .asList(x.getInterfaces()));
108: }
109: return classesAndInterfaces;
110: }
111:
112: private static void super classes(Class c, Collection results) {
113: if (c.getSuperclass() == null) {
114: return;
115: }
116: results.add(c.getSuperclass());
117: superclasses(c.getSuperclass(), results);
118: }
119:
120: }
|