summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/util/IdentityValuesDTO.java
blob: 6cca3ae52b2e619b93eb63f02b5f75970d9b555f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2014 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.restconf.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class IdentityValuesDTO {

    private final List<IdentityValue> elementData = new ArrayList<>();
    private final String originValue;

    public IdentityValuesDTO(
            final String namespace, final String value, final String prefix, final String originValue) {
        elementData.add(new IdentityValue(namespace, value));
        this.originValue = originValue;
    }

    public IdentityValuesDTO(final String originValue) {
        this.originValue = originValue;
    }

    public IdentityValuesDTO() {
        originValue = null;
    }

    public void add(final String namespace, final String value, final String prefix) {
        elementData.add(new IdentityValue(namespace, value));
    }

    public void add(final IdentityValue identityValue) {
        elementData.add(identityValue);
    }

    public List<IdentityValue> getValuesWithNamespaces() {
        return Collections.unmodifiableList(elementData);
    }

    @Override
    public String toString() {
        return elementData.toString();
    }

    public String getOriginValue() {
        return originValue;
    }

    public static final class IdentityValue {

        private final String namespace;
        private final String value;
        private List<Predicate> predicates;

        public IdentityValue(final String namespace, final String value) {
            this.namespace = namespace;
            this.value = value;
        }

        public String getNamespace() {
            return namespace;
        }

        public String getValue() {
            return value;
        }


        public List<Predicate> getPredicates() {
            if (predicates == null) {
                return Collections.emptyList();
            }
            return Collections.unmodifiableList(predicates);
        }

        public void setPredicates(final List<Predicate> predicates) {
            this.predicates = predicates;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder();
            if (namespace != null) {
                sb.append(namespace);
            }
            if (value != null) {
                sb.append(" - ").append(value);
            }
            if (predicates != null && !predicates.isEmpty()) {
                for (final Predicate predicate : predicates) {
                    sb.append("[").append(predicate.toString()).append("]");
                }
            }
            return sb.toString();
        }

    }

    public static final class Predicate {

        private final IdentityValue name;
        private final String value;

        public Predicate(final IdentityValue name, final String value) {
            this.name = name;
            this.value = value;
        }

        public IdentityValue getName() {
            return name;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder();
            if (name != null) {
                sb.append(name.toString());
            }
            if (value != null) {
                sb.append("=").append(value);
            }
            return sb.toString();
        }

        public boolean isLeafList() {
            return name == null;
        }

    }
}