001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.impl;
007:
008: import org.openrdf.model.URI;
009: import org.openrdf.model.util.URIUtil;
010:
011: /**
012: * The default implementation of the {@link URI} interface.
013: */
014: public class URIImpl implements URI {
015:
016: /*-----------*
017: * Constants *
018: *-----------*/
019:
020: private static final long serialVersionUID = -7330406348751485330L;
021:
022: /**
023: * The URI string.
024: */
025: private final String uriString;
026:
027: /*-----------*
028: * Variables *
029: *-----------*/
030:
031: /**
032: * An index indicating the first character of the local name in the URI
033: * string, -1 if not yet set.
034: */
035: private int localNameIdx;
036:
037: /*--------------*
038: * Constructors *
039: *--------------*/
040:
041: /**
042: * Creates a new URI from the supplied string.
043: *
044: * @param uriString
045: * A String representing a valid, absolute URI.
046: * @throws IllegalArgumentException
047: * If the supplied URI is not a valid (absolute) URI.
048: */
049: public URIImpl(String uriString) {
050: assert uriString != null;
051:
052: if (uriString.indexOf(':') < 0) {
053: throw new IllegalArgumentException(
054: "Not a valid (absolute) URI: " + uriString);
055: }
056:
057: this .uriString = uriString;
058: this .localNameIdx = -1;
059: }
060:
061: /*---------*
062: * Methods *
063: *---------*/
064:
065: // Implements URI.toString()
066: @Override
067: public String toString() {
068: return uriString;
069: }
070:
071: public String stringValue() {
072: return uriString;
073: }
074:
075: public String getNamespace() {
076: if (localNameIdx < 0) {
077: localNameIdx = URIUtil.getLocalNameIndex(uriString);
078: }
079:
080: return uriString.substring(0, localNameIdx);
081: }
082:
083: public String getLocalName() {
084: if (localNameIdx < 0) {
085: localNameIdx = URIUtil.getLocalNameIndex(uriString);
086: }
087:
088: return uriString.substring(localNameIdx);
089: }
090:
091: // Implements URI.equals(Object)
092: @Override
093: public boolean equals(Object o) {
094: if (this == o) {
095: return true;
096: }
097:
098: if (o instanceof URI) {
099: return toString().equals(o.toString());
100: }
101:
102: return false;
103: }
104:
105: // Implements URI.hashCode()
106: @Override
107: public int hashCode() {
108: return uriString.hashCode();
109: }
110: }
|