001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.asn1;
022:
023: import java.util.Arrays;
024:
025: import org.apache.harmony.security.x501.AttributeType;
026:
027: /**
028: * Represents Information Object Set.
029: *
030: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
031: */
032:
033: public class InformationObjectSet {
034:
035: private final int capacity;
036:
037: private final Entry[][] pool;
038:
039: public InformationObjectSet() {
040: this (64, 10);
041: }
042:
043: public InformationObjectSet(int capacity, int size) {
044: this .capacity = capacity;
045: pool = new Entry[capacity][size];
046: }
047:
048: public void put(AttributeType at) {
049: put(at.oid.getOid(), at);
050: }
051:
052: public void put(int[] oid, Object object) {
053:
054: int index = hashIntArray(oid) % capacity;
055: // look for OID in the pool
056: Entry[] list = pool[index];
057: int i = 0;
058: for (; list[i] != null; i++) {
059:
060: // check wrong static initialization: no duplicate OIDs
061: if (Arrays.equals(oid, list[i].oid)) {
062: throw new Error(); //FIXME message
063: }
064: }
065:
066: // check : to avoid NPE
067: if (i == (capacity - 1)) {
068: throw new Error(); //FIXME message
069: }
070: list[i] = new Entry(oid, object);
071: }
072:
073: public Object get(int[] oid) {
074: int index = hashIntArray(oid) % capacity;
075:
076: // look for OID in the pool
077: Entry[] list = pool[index];
078: for (int i = 0; list[i] != null; i++) {
079: if (Arrays.equals(oid, list[i].oid)) {
080: return list[i].object;
081: }
082: }
083: return null;
084: }
085:
086: // FIXME change me to Arrays.hashCode(int[])
087: private int hashIntArray(int[] array) {
088: int intHash = 0;
089: for (int i = 0; i < array.length && i < 4; i++) {
090: intHash += array[i] << (8 * i); //TODO what about to find better one?
091: }
092: return intHash & 0x7FFFFFFF; // only positive
093: }
094:
095: private static class Entry {
096: public int[] oid;
097:
098: public Object object;
099:
100: public Entry(int[] oid, Object object) {
101: this.oid = oid;
102: this.object = object;
103: }
104: }
105: }
|