Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.util;

abstract class NoEqualsBase<E, C extends Comparable<C>>
{
    protected final NoEqualsHelper<E, C> mHelper;

    protected NoEqualsBase( NoEqualsHelper<E, C> pHelper )
    {
        mHelper = pHelper;
    }

    @SuppressWarnings({"unchecked"})
    protected class Wrapper implements Comparable<Wrapper>
    {
        private final E mObjectWithProxyValue;

        public Wrapper( E pObjectWithProxyValue )
        {
            mObjectWithProxyValue = pObjectWithProxyValue;
        }

        public E getObject()
        {
            return mObjectWithProxyValue;
        }

        public C getProxyValue()
        {
            return mHelper.getProxyValue( mObjectWithProxyValue );
        }

        @Override
        public boolean equals( Object obj )
        {
            return (obj instanceof Wrapper) && equals( (Wrapper) obj );
        }

        public boolean equals( Wrapper them )
        {
            return (them != null) && areEqual( mHelper.getProxyValue( this.mObjectWithProxyValue ), mHelper.getProxyValue( them.mObjectWithProxyValue ) );
        }

        @Override
        public int hashCode()
        {
            Comparable<?> zProxyValue = mHelper.getProxyValue( mObjectWithProxyValue );
            return (zProxyValue == null) ? 0 : zProxyValue.hashCode();
        }

        @Override
        public int compareTo( Wrapper them )
        {
            return compare( mHelper.getProxyValue( this.mObjectWithProxyValue ), mHelper.getProxyValue( them.mObjectWithProxyValue ) );
        }

        @Override
        public String toString()
        {
            return mObjectWithProxyValue.toString();
        }

        /**
         * @return >0, 0, <0 as pThis > pThem, pThis == pThem, pThis < pThem
         */
        private int compare( C pThis, C pThem )
        {
            if ( pThis == pThem )
            {
                return 0; // the same (or both null)
            }
            if ( pThis == null ) // null - !null
            {
                return -1;
            }
            if ( pThem == null ) // !null - null
            {
                return 1;
            }
            return pThis.compareTo( pThem );
        }
    }

    protected Wrapper wrap( E pObject )
    {
        return new Wrapper( pObject );
    }

    protected E unwrap( Wrapper pWrapper )
    {
        return (pWrapper != null) ? pWrapper.mObjectWithProxyValue : null;
    }

    protected Wrapper wrapNotNull( E pObject )
    {
        validateNotNull( pObject );
        return wrap( pObject );
    }

    protected void validateNotNull( Object o )
    {
        if ( o == null )
        {
            throw new NullPointerException();
        }
    }

    protected E castNotNull( Object o )
    {
        validateNotNull( o );
        if ( mHelper.isInstance( o ) )
        {
            return castNullOK( o );
        }
        throw new ClassCastException( "Unexpected object type '" + o.getClass() + "', expected '" + mHelper.getObjectType() + "' from object: " + o );
    }

    protected E castNullOK( Object o )
    {
        return cast( o );
    }

    @SuppressWarnings({"unchecked"})
    protected <T> T cast( Object o )
    {
        return (T) o;
    }

    protected static boolean areEqual( Object p1, Object p2 )
    {
        return (p1 == p2) || ((p1 != null) && p1.equals( p2 ));
    }

    protected static final Wrapper[] EMPTY_ARRAY = new Wrapper[0];
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/NoEqualsBase.java

Diff revisions: vs.
Revision Author Commited Message
584 GeorgeS picture GeorgeS Fri 18 Nov, 2011 17:53:52 +0000