001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.repository;
043:
044: import java.io.DataInput;
045: import java.io.DataOutput;
046: import java.io.IOException;
047: import java.util.Collection;
048: import java.util.Iterator;
049: import org.netbeans.modules.cnd.repository.spi.Key;
050: import org.netbeans.modules.cnd.repository.support.KeyFactory;
051: import org.netbeans.modules.cnd.repository.support.AbstractObjectFactory;
052: import org.netbeans.modules.cnd.repository.support.SelfPersistent;
053:
054: /**
055: *
056: * @author Nickolay Dalmatov
057: */
058: public class KeyObjectFactory extends KeyFactory {
059:
060: /** Creates a new instance of KeyObjectFactory */
061: public KeyObjectFactory() {
062: }
063:
064: public void writeKey(Key aKey, DataOutput aStream)
065: throws IOException {
066: assert aKey instanceof SelfPersistent;
067: super .writeSelfPersistent((SelfPersistent) aKey, aStream);
068: }
069:
070: public Key readKey(DataInput aStream) throws IOException {
071: assert aStream != null;
072: SelfPersistent out = super .readSelfPersistent(aStream);
073: assert out instanceof Key;
074: return (Key) out;
075: }
076:
077: public void writeKeyCollection(Collection<Key> aCollection,
078: DataOutput aStream) throws IOException {
079: assert aCollection != null;
080: assert aStream != null;
081:
082: int collSize = aCollection.size();
083: aStream.writeInt(collSize);
084:
085: Iterator<Key> iter = aCollection.iterator();
086:
087: while (iter.hasNext()) {
088: Key aKey = iter.next();
089: assert aKey != null;
090: writeKey(aKey, aStream);
091: }
092: }
093:
094: public void readKeyCollection(Collection<Key> aCollection,
095: DataInput aStream) throws IOException {
096: assert aCollection != null;
097: assert aStream != null;
098:
099: int collSize = aStream.readInt();
100:
101: for (int i = 0; i < collSize; ++i) {
102: Key aKey = readKey(aStream);
103: assert aKey != null;
104: aCollection.add(aKey);
105: }
106: }
107:
108: protected int getHandler(Object object) {
109: int aHandle;
110:
111: if (object instanceof ProjectKey) {
112: aHandle = KEY_PROJECT_KEY;
113: } else if (object instanceof NamespaceKey) {
114: aHandle = KEY_NAMESPACE_KEY;
115: } else if (object instanceof FileKey) {
116: aHandle = KEY_FILE_KEY;
117: } else if (object instanceof MacroKey) {
118: aHandle = KEY_MACRO_KEY;
119: } else if (object instanceof IncludeKey) {
120: aHandle = KEY_INCLUDE_KEY;
121: } else if (object instanceof OffsetableDeclarationKey) {
122: aHandle = KEY_DECLARATION_KEY;
123: } else if (object instanceof ProjectSettingsValidatorKey) {
124: aHandle = KEY_PRJ_VALIDATOR_KEY;
125: } else if (object instanceof DeclarationContainerKey) {
126: aHandle = KEY_DECLARATION_CONTAINER_KEY;
127: } else if (object instanceof FileContainerKey) {
128: aHandle = KEY_FILE_CONTAINER_KEY;
129: } else if (object instanceof GraphContainerKey) {
130: aHandle = KEY_GRAPH_CONTAINER_KEY;
131: } else {
132: throw new IllegalArgumentException(
133: "The Key is an instance of the unknown final class "
134: + object.getClass().getName()); // NOI18N
135: }
136:
137: return aHandle;
138: }
139:
140: protected SelfPersistent createObject(int handler, DataInput aStream)
141: throws IOException {
142: SelfPersistent aKey;
143:
144: switch (handler) {
145: case KEY_PROJECT_KEY:
146: aKey = new ProjectKey(aStream);
147: break;
148: case KEY_NAMESPACE_KEY:
149: aKey = new NamespaceKey(aStream);
150: break;
151: case KEY_FILE_KEY:
152: aKey = new FileKey(aStream);
153: break;
154: case KEY_MACRO_KEY:
155: aKey = new MacroKey(aStream);
156: break;
157: case KEY_INCLUDE_KEY:
158: aKey = new IncludeKey(aStream);
159: break;
160: case KEY_DECLARATION_KEY:
161: aKey = new OffsetableDeclarationKey(aStream);
162: break;
163: case KEY_PRJ_VALIDATOR_KEY:
164: aKey = new ProjectSettingsValidatorKey(aStream);
165: break;
166: case KEY_DECLARATION_CONTAINER_KEY:
167: aKey = new DeclarationContainerKey(aStream);
168: break;
169: case KEY_FILE_CONTAINER_KEY:
170: aKey = new FileContainerKey(aStream);
171: break;
172: case KEY_GRAPH_CONTAINER_KEY:
173: aKey = new GraphContainerKey(aStream);
174: break;
175: default:
176: throw new IllegalArgumentException(
177: "Unknown hander was provided: " + handler); // NOI18N
178: }
179:
180: return aKey;
181: }
182:
183: ////////////////////////////////////////////////////////////////////////////
184: // constants which defines the handle of a key in the stream
185:
186: private static final int FIRST_INDEX = AbstractObjectFactory.LAST_INDEX + 1;
187:
188: public static final int KEY_PROJECT_KEY = FIRST_INDEX;
189: public static final int KEY_NAMESPACE_KEY = KEY_PROJECT_KEY + 1;
190: public static final int KEY_FILE_KEY = KEY_NAMESPACE_KEY + 1;
191: public static final int KEY_MACRO_KEY = KEY_FILE_KEY + 1;
192: public static final int KEY_INCLUDE_KEY = KEY_MACRO_KEY + 1;
193: public static final int KEY_DECLARATION_KEY = KEY_INCLUDE_KEY + 1;
194: public static final int KEY_PRJ_VALIDATOR_KEY = KEY_DECLARATION_KEY + 1;
195:
196: public static final int KEY_DECLARATION_CONTAINER_KEY = KEY_PRJ_VALIDATOR_KEY + 1;
197: public static final int KEY_FILE_CONTAINER_KEY = KEY_DECLARATION_CONTAINER_KEY + 1;
198: public static final int KEY_GRAPH_CONTAINER_KEY = KEY_FILE_CONTAINER_KEY + 1;
199:
200: // index to be used in another factory (but only in one)
201: // to start own indeces from the next after LAST_INDEX
202: public static final int LAST_INDEX = KEY_GRAPH_CONTAINER_KEY;
203: }
|