001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.io.fs;
013:
014: import java.io.Serializable;
015:
016: /**
017: * @version 1.1.1
018: * @author TMate Software Ltd.
019: */
020: public class FSID implements Serializable {
021:
022: private String myNodeID;
023: private String myCopyID;
024: private String myTxnID;
025: private long myRevision;
026: private long myOffset;
027:
028: public boolean isTxn() {
029: return myTxnID != null;
030: }
031:
032: public static FSID createTxnId(String nodeId, String copyId,
033: String txnId) {
034: return new FSID(nodeId, txnId, copyId,
035: FSRepository.SVN_INVALID_REVNUM, -1);
036: }
037:
038: public static FSID createRevId(String nodeId, String copyId,
039: long revision, long offset) {
040: return new FSID(nodeId, null, copyId, revision, offset);
041: }
042:
043: private FSID(String nodeId, String txnId, String copyId,
044: long revision, long offset) {
045: myNodeID = nodeId;
046: myCopyID = copyId;
047: myTxnID = txnId;
048: myRevision = revision;
049: myOffset = offset;
050: }
051:
052: public FSID copy() {
053: return new FSID(getNodeID(), getTxnID(), getCopyID(),
054: getRevision(), getOffset());
055: }
056:
057: public String getNodeID() {
058: return myNodeID;
059: }
060:
061: public String getTxnID() {
062: return myTxnID;
063: }
064:
065: public String getCopyID() {
066: return myCopyID;
067: }
068:
069: public long getRevision() {
070: return myRevision;
071: }
072:
073: public long getOffset() {
074: return myOffset;
075: }
076:
077: public boolean equals(Object obj) {
078: if (obj == null || obj.getClass() != FSID.class) {
079: return false;
080: }
081: FSID id = (FSID) obj;
082: if (this == id) {
083: return true;
084: }
085:
086: if (myNodeID != null && !myNodeID.equals(id.getNodeID())) {
087: return false;
088: } else if (myNodeID == null && id.getNodeID() != null) {
089: return false;
090: }
091:
092: if (myCopyID != null && !myCopyID.equals(id.getCopyID())) {
093: return false;
094: } else if (myCopyID == null && id.getCopyID() != null) {
095: return false;
096: }
097:
098: if (myTxnID != null && !myTxnID.equals(id.getTxnID())) {
099: return false;
100: } else if (myTxnID == null && id.getTxnID() != null) {
101: return false;
102: }
103:
104: if (myRevision != id.getRevision()
105: || myOffset != id.getOffset()) {
106: return false;
107: }
108:
109: return true;
110: }
111:
112: public int hashCode() {
113: final int PRIME = 31;
114: int result = 1;
115: result = PRIME * result
116: + ((myNodeID == null) ? 0 : myNodeID.hashCode());
117: result = PRIME * result
118: + ((myCopyID == null) ? 0 : myCopyID.hashCode());
119: result = PRIME * result
120: + ((myTxnID == null) ? 0 : myTxnID.hashCode());
121: result = PRIME * result
122: + (int) (myRevision ^ (myRevision >>> 32));
123: result = PRIME * result + (int) (myOffset ^ (myOffset >>> 32));
124: return result;
125: }
126:
127: /*
128: * Return values: 0 - id1 equals to id2 1 - id1 is related to id2 (id2 is a
129: * result of user's modifications) -1 - id1 is not related to id2
130: * (absolutely different items)
131: */
132: public int compareTo(FSID otherID) {
133: if (otherID == null) {
134: return -1;
135: } else if (otherID.equals(this )) {
136: return 0;
137: }
138: return isRelated(otherID) ? 1 : -1;
139: }
140:
141: public boolean isRelated(FSID otherID) {
142: if (otherID == null) {
143: return false;
144: }
145:
146: if (this == otherID) {
147: return true;
148: }
149:
150: if (myNodeID != null && myNodeID.startsWith("_")) {
151: if (myTxnID != null && !myTxnID.equals(otherID.getTxnID())) {
152: return false;
153: } else if (myTxnID == null && otherID.getTxnID() != null) {
154: return false;
155: }
156: }
157: return myNodeID.equals(otherID.getNodeID());
158: }
159:
160: public String toString() {
161: return myNodeID
162: + "."
163: + myCopyID
164: + "."
165: + (isTxn() ? "t" + myTxnID : "r" + myRevision + "/"
166: + myOffset);
167: }
168:
169: public static FSID fromString(String revNodeId) {
170: int dotInd = revNodeId.indexOf('.');
171: if (dotInd == -1) {
172: return null;
173: }
174:
175: String nodeId = revNodeId.substring(0, dotInd);
176: revNodeId = revNodeId.substring(dotInd + 1);
177:
178: dotInd = revNodeId.indexOf('.');
179: if (dotInd == -1) {
180: return null;
181: }
182:
183: String copyId = revNodeId.substring(0, dotInd);
184: revNodeId = revNodeId.substring(dotInd + 1);
185:
186: if (revNodeId.charAt(0) == 'r') {
187: int slashInd = revNodeId.indexOf('/');
188: long rev = -1;
189: long offset = -1;
190: try {
191: rev = Long.parseLong(revNodeId.substring(1, slashInd));
192: offset = Long.parseLong(revNodeId
193: .substring(slashInd + 1));
194: } catch (NumberFormatException nfe) {
195: return null;
196: }
197: return createRevId(nodeId, copyId, rev, offset);
198: } else if (revNodeId.charAt(0) == 't') {
199: String txnId = revNodeId.substring(1);
200: return createTxnId(nodeId, copyId, txnId);
201: }
202: return null;
203: }
204: }
|