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