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.spi;
018:
019: import org.compass.core.CompassException;
020: import org.compass.core.Property;
021: import org.compass.core.Resource;
022: import org.compass.core.engine.subindex.SubIndexHash;
023: import org.compass.core.engine.utils.ResourceHelper;
024: import org.compass.core.mapping.ResourceMapping;
025:
026: /**
027: * A simple resource id key helper.
028: *
029: * @author kimchy
030: */
031: public final class ResourceKey {
032:
033: private String alias;
034:
035: private String subIndex;
036:
037: private Property[] ids;
038:
039: private int hashCode = Integer.MIN_VALUE;
040:
041: private ResourceMapping resourceMapping;
042:
043: public ResourceKey(ResourceMapping resourceMapping,
044: Resource idResource) {
045: this (resourceMapping, ResourceHelper.toIds(idResource,
046: resourceMapping));
047: }
048:
049: public ResourceKey(ResourceMapping resourceMapping, Property[] ids) {
050: this .resourceMapping = resourceMapping;
051: this .ids = ids;
052: this .alias = resourceMapping.getAlias();
053: }
054:
055: public String getAlias() {
056: return alias;
057: }
058:
059: public String getSubIndex() {
060: if (subIndex == null) {
061: SubIndexHash subIndexHash = getResourceMapping()
062: .getSubIndexHash();
063: subIndex = subIndexHash.mapSubIndex(getAlias(), getIds());
064: }
065: return subIndex;
066: }
067:
068: public Property[] getIds() {
069: return ids;
070: }
071:
072: public String buildUID() throws CompassException {
073: StringBuilder sb = new StringBuilder();
074: sb.append(getAlias()).append("#");
075: for (Property idProp : getIds()) {
076: String idValue = idProp.getStringValue();
077: if (idValue == null) {
078: throw new CompassException("Missing id ["
079: + idProp.getName() + "] for alias ["
080: + getAlias() + "]");
081: }
082: sb.append(idValue);
083: sb.append("#");
084: }
085: return sb.toString();
086: }
087:
088: public String getUIDPath() {
089: return this .resourceMapping.getUIDPath();
090: }
091:
092: public ResourceMapping getResourceMapping() {
093: return this .resourceMapping;
094: }
095:
096: public boolean equals(Object other) {
097: if (this == other)
098: return true;
099:
100: // We will make sure that it never happens
101: // if (!(other instanceof ResourceKey))
102: // return false;
103:
104: final ResourceKey key = (ResourceKey) other;
105: if (!key.alias.equals(alias)) {
106: return false;
107: }
108:
109: for (int i = 0; i < ids.length; i++) {
110: if (!key.ids[i].getStringValue().equals(
111: ids[i].getStringValue())) {
112: return false;
113: }
114: }
115:
116: return true;
117: }
118:
119: public int hashCode() {
120: if (hashCode == Integer.MIN_VALUE) {
121: hashCode = getHashCode();
122: }
123: return hashCode;
124: }
125:
126: private int getHashCode() {
127: int result = alias.hashCode();
128: for (Property id : ids) {
129: result = 29 * result + id.getStringValue().hashCode();
130: }
131: return result;
132: }
133:
134: public String toString() {
135: StringBuilder sb = new StringBuilder();
136: sb.append("alias [").append(getAlias()).append("]");
137: sb.append("uid [").append(buildUID()).append("]");
138: return sb.toString();
139: }
140: }
|