]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterTraits.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ClusterTraits.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.procore.cluster;
13
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.ClusterI;
16 import org.simantics.db.impl.ClusterTraitsBase;
17
18
19 public final class ClusterTraits extends ClusterTraitsBase {
20     private static final int FOREIGN_INDEX_BITS = 16;
21     private static final int FOREIGN_INDEX_MAX = (1<<FOREIGN_INDEX_BITS)-1;
22     private static final int CLUSTER_INDEX_BITS = 16;
23     private static final int CLUSTER_INDEX_MAX =  (1<<CLUSTER_INDEX_BITS)-1;
24     private static final int TYPE_BITS = 2;
25     private static final int NOTYPE_BITS = 32 - TYPE_BITS;
26 //    private static final int CLUSTER_RESERVED_BITS = 15;
27 //    private static final long CLUSTER_RESERVED_BITS2ZERO = ((long)1 << CLUSTER_RESERVED_BITS) - 1; 
28     public static final int createForeignReference(int foreignIndex, int resourceIndex)
29     throws DatabaseException {
30         if (foreignIndex < 1 || foreignIndex > FOREIGN_INDEX_MAX)
31             throw new DatabaseException("Illegal foreign index " + foreignIndex);
32         if (isIllegalResourceIndex(resourceIndex))
33             throw new DatabaseException("Illegal resource index " + resourceIndex);
34         return (resourceIndex<<FOREIGN_INDEX_BITS | foreignIndex)| 0x80000000;
35     }
36     public static int referenceGetType(int ref) {
37         return ref >>> NOTYPE_BITS;
38     }
39     public static final boolean isFlat(int reference) {
40         return reference < 0 && (reference & 0x40000000) != 0;
41     }
42     public static final boolean isLocal(int reference) {
43         return (reference & 0xC0000000) == 0;
44     }
45     public static final int getForeignIndexFromReference(int reference)
46     throws DatabaseException {
47         int foreignIndex = reference & 0x0000FFFF;
48         if (0 == foreignIndex)
49             throw new DatabaseException("Illegal foreign index " + foreignIndex);
50         return foreignIndex;
51     }
52     public static final int getResourceIndexFromForeignReference(int reference) 
53     throws DatabaseException {
54         int resourceIndex = (reference & 0x7FFFFFFF) >> FOREIGN_INDEX_BITS;
55         if (isIllegalResourceIndex(resourceIndex))
56             throw new DatabaseException("Illegal foreign reference " + reference);
57         return resourceIndex;
58     }
59     public static final boolean statementIndexIsDirect(int index) {
60         return (index & 0xC0000000) != 0x40000000;
61     }
62     public static final int statementIndexMake(int index)
63     throws DatabaseException {
64         assert(index > 0);
65         if ((index & 0xC0000000) != 0)
66             throw new DatabaseException("Illegal statement index " + index);
67         return index | 0x40000000;
68     }
69     public static final int statementIndexGet(int index)
70     throws DatabaseException {
71         if ((index & 0xC0000000) == 0x40000000)
72             return index & 0x3FFFFFFF;
73         else
74             return index;
75     }
76     public static boolean completeReferenceIsLocal(int completeRef) {
77         boolean complete = (completeRef & 0xC0000000) != 0;
78         return complete && ((completeRef & 0x0000FFFF) == 0);
79     }
80     public static boolean completeReferenceIsMultiple(int completeRef) {
81         boolean complete = (completeRef & 0xC0000000) != 0;
82         return !complete && (completeRef != 0);
83     }
84     public static int completeReferenceGetResourceIndex(int completeRef) {
85         int low = (completeRef & 0x3FFFFFFF) >>> CLUSTER_INDEX_BITS;
86         return low & 0x00003FFFF;
87     }
88     public static int completeReferenceGetForeignIndex(int completeRef) {
89         return completeRef & 0x0000FFFF;
90     }
91     public static ClusterI.CompleteTypeEnum completeReferenceGetType(int completeRef) {
92         return ClusterI.CompleteTypeEnum.make(completeRef >>> NOTYPE_BITS);
93     }
94     public static int completeReferenceMake(int type, int resourceIndex, int clusterIndex) {
95         assert(type < (1<<TYPE_BITS));
96         assert(!isIllegalResourceIndex(resourceIndex));
97         assert(clusterIndex <= CLUSTER_INDEX_MAX);
98         int ref = (type << NOTYPE_BITS) | (resourceIndex << CLUSTER_INDEX_BITS) | clusterIndex;
99         return ref;
100     }
101     public static int completeReferenceMake(int type, int setIndex) {
102         assert(type < (1<<TYPE_BITS));
103         assert(setIndex < (1<<NOTYPE_BITS));
104         int ref = (type << NOTYPE_BITS) | setIndex;
105         return ref;
106     }
107 }