1 /* 2 * ModeShape (http://www.modeshape.org) 3 * See the COPYRIGHT.txt file distributed with this work for information 4 * regarding copyright ownership. Some portions may be licensed 5 * to Red Hat, Inc. under one or more contributor license agreements. 6 * See the AUTHORS.txt file in the distribution for a full listing of 7 * individual contributors. 8 * 9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape 10 * is licensed to you under the terms of the GNU Lesser General Public License as 11 * published by the Free Software Foundation; either version 2.1 of 12 * the License, or (at your option) any later version. 13 * 14 * ModeShape is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this software; if not, write to the Free 21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 23 */ 24 package org.modeshape.graph; 25 26 import java.util.UUID; 27 import net.jcip.annotations.Immutable; 28 import org.modeshape.graph.property.Path; 29 import org.modeshape.graph.property.Property; 30 import org.modeshape.graph.property.basic.BasicSingleValueProperty; 31 32 /** 33 * General purpose location type that supports a path and a UUID property. This class should never be directly instantiated by 34 * users of the ModeShape framework. Instead, use @{link Location#create} to create the correct location. 35 * 36 * @see Location 37 */ 38 @Immutable 39 final class LocationWithPathAndUuid extends LocationWithPathAndProperty { 40 41 private static final long serialVersionUID = 1L; 42 43 private final UUID uuid; 44 45 /** 46 * Create a new location with a given path and identification property. 47 * 48 * @param path the path 49 * @param uuid the UUID 50 */ 51 LocationWithPathAndUuid( Path path, 52 UUID uuid ) { 53 super(path, new BasicSingleValueProperty(ModeShapeLexicon.UUID, uuid)); 54 assert uuid != null; 55 this.uuid = uuid; 56 } 57 58 /** 59 * {@inheritDoc} 60 * 61 * @see Location#getUuid() 62 */ 63 @Override 64 public final UUID getUuid() { 65 return uuid; 66 } 67 68 /** 69 * {@inheritDoc} 70 * 71 * @see Location#with(Property) 72 */ 73 @Override 74 public Location with( Property idProperty ) { 75 if (idProperty == null || idProperty.isEmpty()) return this; 76 if (ModeShapeLexicon.UUID.equals(idProperty.getName())) { 77 if (idProperty.isSingle() && uuid.equals(idProperty.getFirstValue())) return this; 78 return Location.create(getPath(), idProperty); 79 } 80 return Location.create(getPath(), getIdProperties().get(0), idProperty); 81 } 82 83 /** 84 * {@inheritDoc} 85 * 86 * @see Location#with(Path) 87 */ 88 @Override 89 public Location with( Path path ) { 90 if (path == null) return Location.create(uuid); 91 if (getPath().equals(path)) return this; 92 return Location.create(path, uuid); 93 } 94 95 /** 96 * {@inheritDoc} 97 * 98 * @see Location#with(UUID) 99 */ 100 @Override 101 public Location with( UUID uuid ) { 102 if (uuid == null) return Location.create(getPath()); 103 if (uuid.equals(this.uuid)) return this; 104 return Location.create(getPath(), uuid); 105 } 106 }