/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.binding; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.concepts.Mutable; /** * Uniquely identifies instance of data tree. * */ public final class InstanceIdentifier implements Immutable { private final List path; private final Class targetType; public InstanceIdentifier(Class type) { path = Collections. singletonList(new Item<>(type)); this.targetType = type; } public InstanceIdentifier(List path, Class type) { this.path = Collections. unmodifiableList(new ArrayList<>(path)); this.targetType = type; } /** * * @return path */ public List getPath() { return this.path; } public Class getTargetType() { return this.targetType; } @Override public String toString() { return "InstanceIdentifier [path=" + path + "]"; } /** * Path argument of instance identifier. * * Interface which implementations are used as path components of the * instance path. * * @author ttkacik * */ public interface PathArgument { Class getType(); } public static final class Item implements PathArgument { private final Class type; public Item(Class type) { this.type = type; } public Class getType() { return type; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Item other = (Item) obj; if (type == null) { if (other.type != null) return false; } else if (!type.equals(other.type)) return false; return true; } } public static final class IdentifiableItem & DataObject, T extends Identifier> implements PathArgument { private final T key; private final Class type; public IdentifiableItem(Class type, T key) { if (type == null) throw new IllegalArgumentException("Type must not be null."); if (key == null) throw new IllegalArgumentException("Key must not be null."); this.type = type; this.key = key; } T getKey() { return this.key; } @Override public Class getType() { return this.type; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj.hashCode() != hashCode()) { return false; } if (!(obj instanceof IdentifiableItem)) { return false; } IdentifiableItem foreign = (IdentifiableItem) obj; return key.equals(foreign.getKey()); } @Override public int hashCode() { return key.hashCode(); } @Override public String toString() { return type.getName() + "[key=" + key + "]"; } } public interface InstanceIdentifierBuilder extends Builder { InstanceIdentifierBuilder node(Class container); & DataObject, T extends Identifier> InstanceIdentifierBuilder node( Class listItem, T listKey); } public static InstanceIdentifierBuilder builder() { return new BuilderImpl(); } private static class BuilderImpl implements InstanceIdentifierBuilder { private List path; private Class target = null; @Override public InstanceIdentifier toInstance() { List immutablePath = Collections.unmodifiableList(new ArrayList(path)); return new InstanceIdentifier(immutablePath, target); } @Override public InstanceIdentifierBuilder node(Class container) { target = container; path.add(new Item(container)); return this; } @Override public & DataObject, T extends Identifier> InstanceIdentifierBuilder node( Class listItem, T listKey) { target = listItem; path.add(new IdentifiableItem(listItem, listKey)); return this; } } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((path == null) ? 0 : path.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } InstanceIdentifier other = (InstanceIdentifier) obj; if (path == null) { if (other.path != null) { return false; } } else if (!path.equals(other.path)) { return false; } return true; } }