Added tests for yang.model.util
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / helpers / IdentityValuesDTO.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson.helpers;
9
10 import com.google.common.annotations.Beta;
11
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15
16 /**
17  * This class is implementation-internal and subject to change. Please do not use it.
18  */
19 @Beta
20 public final class IdentityValuesDTO {
21
22     private final List<IdentityValue> elementData = new ArrayList<>();
23     private final String originValue;
24
25     public IdentityValuesDTO(final String namespace, final String value, final String prefix, final String originValue) {
26         elementData.add(new IdentityValue(namespace, value, prefix));
27         this.originValue = originValue;
28     }
29
30     public IdentityValuesDTO(final String originValue) {
31         this.originValue = originValue;
32     }
33
34     public IdentityValuesDTO() {
35         originValue = null;
36     }
37
38     public void add(final String namespace, final String value, final String prefix) {
39         elementData.add(new IdentityValue(namespace, value, prefix));
40     }
41
42     public void add(final IdentityValue identityValue) {
43         elementData.add(identityValue);
44     }
45
46     public List<IdentityValue> getValuesWithNamespaces() {
47         return Collections.unmodifiableList(elementData);
48     }
49
50     @Override
51     public String toString() {
52         return elementData.toString();
53     }
54
55     public String getOriginValue() {
56         return originValue;
57     }
58
59     public static final class IdentityValue {
60
61         private final String namespace;
62         private final String value;
63         private final String prefix;
64         private List<Predicate> predicates;
65
66         public IdentityValue(final String namespace, final String value, final String prefix) {
67             this.namespace = namespace;
68             this.value = value;
69             this.prefix = prefix;
70         }
71
72         public String getNamespace() {
73             return namespace;
74         }
75
76         public String getValue() {
77             return value;
78         }
79
80         public String getPrefix() {
81             return prefix;
82         }
83
84         public List<Predicate> getPredicates() {
85             if (predicates == null) {
86                 return Collections.emptyList();
87             }
88             return Collections.unmodifiableList(predicates);
89         }
90
91         public void setPredicates(final List<Predicate> predicates) {
92             this.predicates = predicates;
93         }
94
95         @Override
96         public String toString() {
97             StringBuilder sb = new StringBuilder();
98             if (namespace != null) {
99                 sb.append(namespace);
100             }
101             if (prefix != null) {
102                 sb.append("(").append(prefix).append(")");
103             }
104             if (value != null) {
105                 sb.append(" - ").append(value);
106             }
107             if (predicates != null && !predicates.isEmpty()) {
108                 for (Predicate predicate : predicates) {
109                     sb.append("[");
110                     predicate.toString();
111                     sb.append("]");
112                 }
113             }
114             return sb.toString();
115         }
116
117     }
118
119     public static final class Predicate {
120
121         private final IdentityValue name;
122         private final String value;
123
124         public Predicate(final IdentityValue name, final String value) {
125             super();
126             this.name = name;
127             this.value = value;
128         }
129
130         public IdentityValue getName() {
131             return name;
132         }
133
134         public String getValue() {
135             return value;
136         }
137
138         @Override
139         public String toString() {
140             StringBuilder sb = new StringBuilder();
141             if (name != null) {
142                 sb.append(name.toString());
143             }
144             if (value != null) {
145                 sb.append("=").append(value);
146             }
147             return sb.toString();
148         }
149
150         public boolean isLeafList() {
151             return name == null ? true : false;
152         }
153
154     }
155 }