summaryrefslogtreecommitdiffstats
path: root/yang-compiler/src/main/java/org/onap/modeling/yangkit/plugin/yangpackage/SubComponentInfo.java
blob: 6cdd00565049afdc677dea1e85fc62d04feda661 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2023 Huawei Technologies

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.onap.modeling.yangkit.plugin.yangpackage;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public abstract class SubComponentInfo {
    private final String name;
    private String revision;
    private final List<String> replaceRevisions = new ArrayList<>();
    private List<URI> locations;

    /**
     * constructor.
     * @param name the name of subcomponent.
     * @param revision the revision of subcomponent.
     */
    public SubComponentInfo(String name, String revision) {
        this.name = name;
        this.revision = revision;
    }

    /**
     * constructor.
     * @param name the name of subcomponent.
     */
    public SubComponentInfo(String name) {
        this.name = name;
    }

    /**
     * get the name of subcomponent.
     * @return name.
     */
    public String getName() {
        return name;
    }

    /**
     * get the revision of subcomponent.
     * @return revision
     */
    public String getRevision() {
        return revision;
    }

    /**
     * set the revision of subcomponent.
     * @param revision revision
     */
    public void setRevision(String revision) {
        this.revision = revision;
    }

    /**
     * get the replaced revisions.
     * @return the list of revisions.
     */
    public List<String> getReplaceRevisions() {
        return replaceRevisions;
    }

    /**
     * add replace revision.
     * @param revision the revision to be replaced.
     */
    public void addReplaceRevision(String revision) {
        if (replaceRevisions.contains(revision)) {
            return;
        }
        replaceRevisions.add(revision);
    }

    /**
     * get locations of subcomponent.
     * @return list of location uri.
     */
    public List<URI> getLocations() {
        return locations;
    }

    /**
     * add location.
     * @param location location uri.
     */
    public void addLocation(URI location) {
        if (locations.contains(location)) {
            return;
        }
        locations.add(location);
    }

    /**
     * equals method.
     * @param obj other object.
     * @return true or false.
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof SubComponentInfo)) {
            return false;
        }
        SubComponentInfo that = (SubComponentInfo) obj;
        return getName().equals(that.getName()) && getRevision().equals(that.getRevision());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getRevision());
    }

    /**
     * the abstract method of serialize the revision.
     * @return serialized revision.
     */
    protected abstract Map.Entry<String, String> serializeRevision();

    /**
     * serialize the subcomponent to json element.
     * @return json element.
     */
    public JsonElement serialize() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", this.getName());
        if (this.getRevision() != null) {
            Map.Entry<String, String> revisionEntry = serializeRevision();
            jsonObject.addProperty(revisionEntry.getKey(), revisionEntry.getValue());
        }
        if (!this.getReplaceRevisions().isEmpty()) {
            JsonArray replaceRevisions = new JsonArray();
            for (String replaceRevision : this.getReplaceRevisions()) {
                replaceRevisions.add(replaceRevision);
            }
            jsonObject.add("replaces-version", replaceRevisions);
        }
        if (!this.getLocations().isEmpty()) {
            JsonArray locations = new JsonArray();
            for (URI location : this.getLocations()) {
                locations.add(location.toString());
            }
            jsonObject.add("location", locations);
        }
        return jsonObject;
    }
}