001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.core.api; 022 023/** 024 * A PATH reference to a document. 025 * 026 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 027 */ 028public class PathRef implements DocumentRef { 029 030 private static final long serialVersionUID = 4817248580727120854L; 031 032 public final String value; 033 034 public PathRef(String parentPath, String name) { 035 checkName(name); 036 if (parentPath == null) { 037 value = name; 038 } else if ("/".equals(parentPath)) { 039 value = '/' + name; 040 } else { 041 value = parentPath + '/' + name; 042 } 043 } 044 045 public PathRef(String path) { 046 value = path; 047 } 048 049 /** 050 * @since 5.6 051 */ 052 public PathRef(PathRef parentRef, String name) { 053 this(parentRef.value, name); 054 } 055 056 public static void checkName(String name) { 057 // checks the name does not contains slash 058 if (name != null && name.indexOf('/') >= 0) { 059 throw new IllegalArgumentException( 060 String.format("Invalid name '%s'. A DocumentModel's name cannot contain slash character. " 061 + "Use the parentPath to specificy the document's path.", name)); 062 } 063 } 064 065 @Override 066 public int type() { 067 return PATH; 068 } 069 070 @Override 071 public Object reference() { 072 return value; 073 } 074 075 @Override 076 public boolean equals(Object obj) { 077 if (this == obj) { 078 return true; 079 } 080 if (obj instanceof PathRef) { 081 return ((PathRef) obj).value.equals(value); 082 } 083 // it is not possible to compare a PathRef with an IdRef 084 return false; 085 } 086 087 @Override 088 public int hashCode() { 089 return value.hashCode(); 090 } 091 092 @Override 093 public String toString() { 094 return value; 095 } 096 097}