/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 jena3.graph.impl; import java.util.UUID ; import jena3.graph.Node ; import jena3.graph.NodeVisitor ; import com.hp.hpl.jena.shared.uuid.JenaUUID ; import com.hp.hpl.jena.shared.uuid.UUIDFactory ; import com.hp.hpl.jena.shared.uuid.UUID_V1_Gen ; public class NodeBlank extends NodeImpl { // XXX Switch to: org.apache.commons.id.uuid // Expect it's a snadbox and seemlying inactive project. // Others? // Or just go with large random numbers? private static UUIDFactory factory = new UUID_V1_Gen() ; // new UUID_V4_Gen() ; // Save the space of a UUID? Obsessive? private long mostSignificantBits ; private long leastSignificantBits ; private String label = null ; private NodeBlank(JenaUUID uuid) { mostSignificantBits = uuid.getMostSignificantBits() ; leastSignificantBits = uuid.getLeastSignificantBits() ; } public NodeBlank(UUID uuid) { mostSignificantBits = uuid.getMostSignificantBits() ; leastSignificantBits = uuid.getLeastSignificantBits() ; } public NodeBlank() { this(factory.generate()) ; } public NodeBlank(String string) { // Is it a UUID? If so, use that and don't store the string. try { // UUID.fromString isn't very strict. JenaUUID uuid = JenaUUID.parse(string) ; mostSignificantBits = uuid.getMostSignificantBits() ; leastSignificantBits = uuid.getLeastSignificantBits() ; return ; } catch (Exception ex) {} label = string ; // a UUID of all zeros is illegal. mostSignificantBits = 0 ; leastSignificantBits = 0 ; } @Override public boolean isBlank() { return true ; } @Override public UUID getBlankNodeId() { return new UUID(mostSignificantBits, leastSignificantBits) ; } @Override public String getBlankNodeLabel() { return getLabel() ; } private String getLabel() { if ( label == null ) label = genLabel() ; return label ; // if ( label != null ) // return label ; // // XXX Cache label? // return genLabel() ; } private String genLabel() { return JenaUUID.toString(mostSignificantBits, leastSignificantBits) ; } @Override public Object visit(NodeVisitor v) { return null ; } @Override public boolean sameValueAs(Node n) { return equals(n) ; } @Override public String toString() { return getLabel() ; } @Override public int hashCode() { final int prime = 57 ; int result = 1 ; result = prime * result + (int)(leastSignificantBits ^ (leastSignificantBits >>> 32)) ; result = prime * result + (int)(mostSignificantBits ^ (mostSignificantBits >>> 32)) ; return result ; } @Override public boolean equals(Object obj) { if ( this == obj ) return true ; if ( obj == null ) return false ; if ( !(obj instanceof NodeBlank) ) return false ; NodeBlank other = (NodeBlank)obj ; if ( leastSignificantBits != other.leastSignificantBits ) return false ; if ( mostSignificantBits != other.mostSignificantBits ) return false ; return true ; } }