2 * Copyright (C) 2014 Cisco Systems, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Thomas Bachman
11 package org.opendaylight.groupbasedpolicy.renderer.opflex.mit;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
19 * Class used for building and extending URIs
21 * Returning the URI could be optimized by building this
22 * as we go, rather than building it when requested.
24 * This counts and levels used in this class are for
25 * the level of hierarchy, and do not include any
26 * separator characters ("/"). For example, the following
29 * /tenants/tenant/51134b1e-6047-4d51-8d07-4135afd3672f
36 public class PolicyUri {
38 public final static String POLICY_URI_SEP = "/";
40 private List<String> uri;
43 uri = new ArrayList<String>();
51 public PolicyUri(PolicyUri policyUri) {
52 this.uri = new ArrayList<String>(policyUri.uri);
56 * Constructor using a full string, which gets split
61 public PolicyUri(String uri) {
62 String [] tmpUri = uri.split(PolicyUri.POLICY_URI_SEP);
63 if (tmpUri.length > 0) {
64 this.uri = new ArrayList<String>();
65 // gets rid of leading empty element
66 for (int i = 1; i < tmpUri.length; i++) {
76 * Constructor using a list of URI elements, which
77 * excludes separator characters
81 public PolicyUri(List<String> tokens) {
82 if (tokens.size() > 0) {
83 this.uri = new ArrayList<String>();
84 for (String t: tokens) {
91 * Return the URI as a Uri object, including
92 * separator characters
97 return new Uri(this.toString());
101 * Push a new leaf on to the URI
105 public void push(String leaf) {
106 uri.add(POLICY_URI_SEP);
111 * Returns the String representation of parent object URI
115 public String getParent() {
116 if (uri.size() == 0) return null;
118 PolicyUri parentUri = new PolicyUri(this);
120 return parentUri.toString();
125 * Remove (and return) the leaf of the URI.
126 * Never pop off the "/" root element.
130 public String pop() {
131 if (uri.size() <= 0) {
136 String s = uri.remove(uri.size()-1);
137 // remove the separator
138 uri.remove(uri.size()-1);
139 // return just the node
145 * Determine if the URI is valid.
149 public boolean valid() {
150 if (uri.size() > 0) {
158 * Get the element at depth "level". Returns
159 * null if level exceeds the hierarchy depth.
164 public String getElement(int level) {
165 if ((level*2+1) > this.uri.size()) return null;
167 return this.uri.get(level*2+1);
171 * Return the index of the first instance
172 * where the named element is found in the URI,
173 * starting the search from the root
178 public int whichElement(String needle) {
179 return (this.uri.indexOf(needle)/2);
183 * Return the number of levels in the parsed URI
188 public int totalElements() {
189 return (this.uri.size()/2);
193 * Check to see if the parsed URI contains
194 * the named element in the hierarchy
199 public boolean contains(String needle) {
200 return this.uri.contains(needle);
203 public String originalPath() {
204 return this.toString();
208 public int hashCode() {
209 final int prime = 31;
211 result = prime * result + ((uri == null) ? 0 : uri.hashCode());
216 public boolean equals(Object obj) {
221 if (getClass() != obj.getClass())
223 PolicyUri other = (PolicyUri) obj;
225 if (other.uri != null)
227 } else if (!uri.equals(other.uri))
233 public String toString() {
234 if (uri.size() == 0) return "";
235 StringBuffer sb = new StringBuffer();
237 for (String s: uri) {
240 return sb.toString();