
org.janusgraph.graphdb.relations.RelationIdentifier Maven / Gradle / Ivy
// Copyright 2017 JanusGraph Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.janusgraph.graphdb.relations;
import org.janusgraph.util.encoding.LongEncoding;
import java.io.Serializable;
import java.util.Arrays;
/**
* @author Matthias Broecheler ([email protected])
*/
public final class RelationIdentifier implements Serializable {
public static final String TOSTRING_DELIMITER = "-";
private final Object outVertexId;
private final long typeId;
private final long relationId;
private final Object inVertexId;
private RelationIdentifier() {
outVertexId = null;
typeId = 0;
relationId = 0;
inVertexId = null;
}
public RelationIdentifier(final Object outVertexId, final long typeId, final long relationId, final Object inVertexId) {
this.outVertexId = outVertexId;
this.typeId = typeId;
this.relationId = relationId;
this.inVertexId = inVertexId;
}
public long getRelationId() {
return relationId;
}
public long getTypeId() {
return typeId;
}
public Object getOutVertexId() {
return outVertexId;
}
public Object getInVertexId() {
return inVertexId;
}
public static RelationIdentifier get(Object[] ids) {
if (ids.length != 3 && ids.length != 4)
throw new IllegalArgumentException("Not a valid relation identifier: " + Arrays.toString(ids));
for (int i = 0; i < 3; i++) {
if (ids[i] instanceof Number && ((Number) ids[i]).longValue() < 0)
throw new IllegalArgumentException("Not a valid relation identifier: " + Arrays.toString(ids));
}
return new RelationIdentifier(ids[1], ((Number) ids[2]).longValue(), ((Number) ids[0]).longValue(), ids.length == 4 ? ids[3] : 0);
}
public static RelationIdentifier get(int[] ids) {
if (ids.length != 3 && ids.length != 4)
throw new IllegalArgumentException("Not a valid relation identifier: " + Arrays.toString(ids));
for (int i = 0; i < 3; i++) {
if (ids[i] < 0)
throw new IllegalArgumentException("Not a valid relation identifier: " + Arrays.toString(ids));
}
return new RelationIdentifier(ids[1], ids[2], ids[0], ids.length == 4 ? ids[3] : 0);
}
@Override
public int hashCode() {
return Long.hashCode(relationId);
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
else if (!getClass().isInstance(other)) return false;
RelationIdentifier oth = (RelationIdentifier) other;
return relationId == oth.relationId && typeId == oth.typeId;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(LongEncoding.encode(relationId)).append(TOSTRING_DELIMITER).append(LongEncoding.encode(((Number) outVertexId).longValue()))
.append(TOSTRING_DELIMITER).append(LongEncoding.encode(typeId));
if (inVertexId != null && ((Number) inVertexId).longValue() > 0) {
s.append(TOSTRING_DELIMITER).append(LongEncoding.encode(((Number) inVertexId).longValue()));
}
return s.toString();
}
public static RelationIdentifier parse(String id) {
String[] elements = id.split(TOSTRING_DELIMITER);
if (elements.length != 3 && elements.length != 4)
throw new IllegalArgumentException("Not a valid relation identifier: " + id);
try {
return new RelationIdentifier(LongEncoding.decode(elements[1]),
LongEncoding.decode(elements[2]),
LongEncoding.decode(elements[0]),
elements.length == 4 ? LongEncoding.decode(elements[3]) : 0);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid id - each token expected to be a number", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy