Statistics
| Revision:

root / tags / v1_1_Build_1004 / applications / appgvSIG / src / com / vividsolutions / jump / util / LangUtil.java @ 12319

History | View | Annotate | Download (3.39 KB)

1 312 fernando
/*
2
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
3
 * for visualizing and manipulating spatial features with geometry and attributes.
4
 *
5
 * Copyright (C) 2003 Vivid Solutions
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 *
21
 * For more information, contact:
22
 *
23
 * Vivid Solutions
24
 * Suite #1A
25
 * 2328 Government Street
26
 * Victoria BC  V8T 5G5
27
 * Canada
28
 *
29
 * (250)385-6040
30
 * www.vividsolutions.com
31
 */
32
package com.vividsolutions.jump.util;
33
34
import java.util.ArrayList;
35
import java.util.Arrays;
36
import java.util.Collection;
37
import java.util.HashMap;
38
import java.util.Map;
39
40
import com.vividsolutions.jts.util.Assert;
41
42
/**
43
 * Utilities to support the Java language.
44
 */
45
public class LangUtil {
46
    private static Map primitiveToWrapperMap = new HashMap() {
47
48
        {
49
            put(byte.class, Byte.class);
50
            put(char.class, Character.class);
51
            put(short.class, Short.class);
52
            put(int.class, Integer.class);
53
            put(long.class, Long.class);
54
            put(float.class, Float.class);
55
            put(double.class, Double.class);
56
            put(boolean.class, Boolean.class);
57
        }
58
    };
59
60
    public static String emptyStringIfNull(String s) {
61
        return (s == null) ? "" : s;
62
    }
63
64
    /**
65
     * Useful because an expression used to generate o need only be
66
     * evaluated once.
67
     */
68
    public static Object ifNull(Object o, Object alternative) {
69
        return (o == null) ? alternative : o;
70
    }
71
72
    public static Object ifNotNull(Object o, Object alternative) {
73
        return (o != null) ? alternative : o;
74
    }
75
76
    public static Class toPrimitiveWrapperClass(Class primitiveClass) {
77
        return (Class) primitiveToWrapperMap.get(primitiveClass);
78
    }
79
80
    public static boolean isPrimitive(Class c) {
81
        return primitiveToWrapperMap.containsKey(c);
82
    }
83
84
    public static boolean bothNullOrEqual(Object a, Object b) {
85
        return (a == null && b == null) || (a != null && b != null && a.equals(b));
86
    }
87
88
    public static Object newInstance(Class c) {
89
        try {
90
            return c.newInstance();
91
        } catch (Exception e) {
92
            Assert.shouldNeverReachHere(e.toString());
93
            return null;
94
        }
95
    }
96
97
    public static Collection classesAndInterfaces(Class c) {
98
        ArrayList classesAndInterfaces = new ArrayList();
99
        classesAndInterfaces.add(c);
100
        superclasses(c, classesAndInterfaces);
101
        classesAndInterfaces.addAll(Arrays.asList(c.getInterfaces()));
102
        return classesAndInterfaces;
103
    }
104
105
    private static void superclasses(Class c, Collection results) {
106
        if (c.getSuperclass() == null) {
107
            return;
108
        }
109
        results.add(c.getSuperclass());
110
        superclasses(c.getSuperclass(), results);
111
    }
112
}