001/* 002 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 */ 013 014package org.nuxeo.ecm.core.api; 015 016/** 017 * A PATH reference to a document. 018 * 019 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 020 */ 021public class PathRef implements DocumentRef { 022 023 private static final long serialVersionUID = 4817248580727120854L; 024 025 public final String value; 026 027 public PathRef(String parentPath, String name) { 028 checkName(name); 029 if (parentPath == null) { 030 value = name; 031 } else if ("/".equals(parentPath)) { 032 value = '/' + name; 033 } else { 034 value = parentPath + '/' + name; 035 } 036 } 037 038 public PathRef(String path) { 039 value = path; 040 } 041 042 /** 043 * @since 5.6 044 */ 045 public PathRef(PathRef parentRef, String name) { 046 this(parentRef.value, name); 047 } 048 049 public static void checkName(String name) { 050 // checks the name does not contains slash 051 if (name != null && name.indexOf('/') >= 0) { 052 throw new IllegalArgumentException( 053 String.format("Invalid name '%s'. A DocumentModel's name cannot contain slash character. " 054 + "Use the parentPath to specificy the document's path.", name)); 055 } 056 } 057 058 @Override 059 public int type() { 060 return PATH; 061 } 062 063 @Override 064 public Object reference() { 065 return value; 066 } 067 068 @Override 069 public boolean equals(Object obj) { 070 if (this == obj) { 071 return true; 072 } 073 if (obj instanceof PathRef) { 074 return ((PathRef) obj).value.equals(value); 075 } 076 // it is not possible to compare a PathRef with an IdRef 077 return false; 078 } 079 080 @Override 081 public int hashCode() { 082 return value.hashCode(); 083 } 084 085 @Override 086 public String toString() { 087 return value; 088 } 089 090}