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
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.orsup.base;

import org.litesoft.commonfoundation.base.*;
import org.litesoft.core.simpletypes.nonpublic.*;
import org.litesoft.orsup.nonpublic.*;
import org.litesoft.orsup.selection.*;

import java.util.*;

/**
 * Limited Class due to inability to convert to/from String for any type - currently limited to Long & String
 */
public final class UniqueKeyMulti extends AbstractUniqueKeyObject {
    private static final long serialVersionUID = 1L;

    private static final char SEP_CHAR = ',';
    private static final char ESC_CHAR = '\\';

    private final Object[] mIDs;
    transient private AttributeAccessorSCD[] mAASCDs;

    @SuppressWarnings({"unchecked"})
    public UniqueKeyMulti( PersistentObject<?> pPO, AttributeAccessorSCD<PersistentObject<?>>... pAASCDs ) {
        IllegalArgument.ifNull( "PersistentObject", pPO );
        Confirm.isNotNullOrEmpty( "Must provide some AttributeAccessorSCDs", mAASCDs = pAASCDs );
        mIDs = new Object[mAASCDs.length];
        for ( int i = 0; i < mAASCDs.length; i++ ) {
            mIDs[i] = mAASCDs[i].db_getValueOnPO( pPO );
        }
    }

    public UniqueKeyMulti( String pFromString, AttributeAccessorSCD<PersistentObject<?>>... pAASCDs )
            throws IllegalArgumentException {
        String[] parts = parseString( pFromString, (mAASCDs = pAASCDs).length );
        mIDs = new Object[pAASCDs.length];
        for ( int i = 0; i < pAASCDs.length; i++ ) {
            mIDs[i] = convertIDfrom( parts[i], pAASCDs[i] );
        }
    }

    @Override
    protected int calcHashCode() {
        int rv = 0;
        for ( Object o : mIDs ) {
            rv += EqualSupport.calcHashCode( o );
        }
        return rv;
    }

    @Override
    protected boolean equalsNotNullNotUs( PersistentObjectUniqueKey pThem ) {
        if ( pThem instanceof UniqueKeyMulti ) {
            UniqueKeyMulti them = (UniqueKeyMulti) pThem;
            if ( this.mIDs.length == them.mIDs.length ) {
                for ( int i = 0; i < mIDs.length; i++ ) {
                    if ( !EqualSupport.equal( this.mIDs[i], them.mIDs[i] ) ) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    @Override
    protected String generateToStringForConstructor() {
        StringBuilder sb = new StringBuilder();
        for ( Object o : mIDs ) {
            sb.append( SEP_CHAR );
            if ( o == null ) {
                sb.append( NULL_CHAR );
            } else {
                sb.append( DATA_CHAR );
                String s = o.toString();
                for ( int k = 0; k < s.length(); ++k ) {
                    char c = s.charAt( k );
                    if ( c == SEP_CHAR || c == ESC_CHAR ) {
                        sb.append( ESC_CHAR );
                    }
                    sb.append( c );
                }
            }
        }
        return sb.toString().substring( 1 );
    }

    private String[] parseString( String pFromString, int pParts )
            throws IllegalArgumentException {
        if ( pParts < 1 ) {
            throw new IllegalArgumentException( "Need at least 1 part" );
        }
        List<String> parts = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        int stridx = 0;
        while ( stridx < pFromString.length() ) {
            char c = pFromString.charAt( stridx++ );
            if ( c == ESC_CHAR ) {
                if ( stridx >= pFromString.length() ) {
                    throw new IllegalArgumentException( "Key malformed - too short" );
                }
                c = pFromString.charAt( stridx++ );
                sb.append( c );
            } else if ( c == SEP_CHAR ) {
                parts.add( nullifyfromString( sb.toString() ) );
                sb = new StringBuilder();
            } else {
                sb.append( c );
            }
        }
        parts.add( nullifyfromString( sb.toString() ) );
        if ( pParts < parts.size() ) {
            throw new IllegalArgumentException( "Key has too few entries" );
        }
        if ( pParts > parts.size() ) {
            throw new IllegalArgumentException( "Key has too many entries" );
        }
        return parts.toArray( new String[parts.size()] );
    }

    @Override
    public WhereClause convertToWC() {
        if ( mAASCDs == null ) {
            throw new IllegalStateException( "convertToWC not allowed for transferred UniqueKeys" );
        }
        WhereClauseFactory f = WhereClauseFactory.INSTANCE;
        WhereClause wc = f.isEqual( mAASCDs[0], mIDs[0] );
        for ( int i = 1; i < mIDs.length; i++ ) {
            wc = f.and( wc, f.isEqual( mAASCDs[i], mIDs[i] ) );
        }
        return wc;
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/base/UniqueKeyMulti.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

849 Diff Diff GeorgeS picture GeorgeS Tue 11 Sep, 2012 17:11:59 +0000

Clean up serialization

823 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 16:10:13 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
49 GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text