001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.mapping;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022:
023: /**
024: * @author kimchy
025: */
026: public abstract class AbstractMultipleMapping extends AbstractMapping
027: implements MultipleMapping {
028:
029: protected ArrayList mappings = new ArrayList();
030:
031: protected HashMap mappingsByNameMap = new HashMap();
032:
033: public void removeExistingByName(Mapping mapping) {
034: if (mappingsByNameMap.get(mapping.getName()) != null) {
035: for (int i = 0; i < mappings.size(); i++) {
036: Mapping tempMapping = (Mapping) mappings.get(i);
037: if (tempMapping.getName() != null
038: && tempMapping.getName().equals(
039: mapping.getName())) {
040: mappings.remove(i);
041: break;
042: }
043: }
044: mappingsByNameMap.remove(mapping.getName());
045: }
046: }
047:
048: public int addMapping(Mapping mapping) {
049: if (mapping instanceof OverrideByNameMapping) {
050: if (((OverrideByNameMapping) mapping).isOverrideByName()) {
051: removeExistingByName(mapping);
052: }
053: }
054: mappingsByNameMap.put(mapping.getName(), mapping);
055: mappings.add(mapping);
056: return mappings.size() - 1;
057: }
058:
059: public void addMappings(MultipleMapping mapping) {
060: for (Iterator it = mapping.mappingsIt(); it.hasNext();) {
061: addMapping((Mapping) it.next());
062: }
063: }
064:
065: public void replaceMappings(MultipleMapping mapping) {
066: clearMappings();
067: addMappings(mapping);
068: }
069:
070: public Iterator mappingsIt() {
071: return mappings.iterator();
072: }
073:
074: public int mappingsSize() {
075: return mappings.size();
076: }
077:
078: public Mapping getMapping(String name) {
079: return (Mapping) mappingsByNameMap.get(name);
080: }
081:
082: public Mapping getMapping(int index) {
083: return (Mapping) mappings.get(index);
084: }
085:
086: public void clearMappings() {
087: mappings.clear();
088: mappingsByNameMap.clear();
089: }
090:
091: protected void copy(MultipleMapping mapping) {
092: super .copy(mapping);
093: for (Iterator it = mappingsIt(); it.hasNext();) {
094: mapping.addMapping(((Mapping) it.next()).copy());
095: }
096: }
097:
098: protected void shallowCopy(MultipleMapping mapping) {
099: super.copy(mapping);
100: }
101: }
|