d2dc95af72e261b460972b9193ee13213e02ab1b
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / bind / RNGTrackBinder.java
1 /*
2  * Copyright (c) 2015. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.xml.xspf.bind;
18
19 import java.math.BigInteger;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.hedgecode.xml.xspf.rng.Album;
24 import org.hedgecode.xml.xspf.rng.Duration;
25 import org.hedgecode.xml.xspf.rng.Identifier;
26 import org.hedgecode.xml.xspf.rng.Location;
27 import org.hedgecode.xml.xspf.rng.Track;
28 import org.hedgecode.xml.xspf.rng.TrackNum;
29
30 /**
31  *
32  *
33  * @author Dmitry Samoshin aka gotty
34  */
35 public class RNGTrackBinder extends AbstractRNGBinder implements TrackBinder {
36
37     private Track rngTrack;
38
39     public RNGTrackBinder() {
40         this.rngTrack = RNG_FACTORY.createTrack();
41     }
42
43     public RNGTrackBinder(Track rngTrack) {
44         this.rngTrack = rngTrack;
45     }
46
47     @Override
48     protected Object getElement(Class elementClass) {
49         for (Object element : rngTrack.getLocationOrIdentifierOrTitle()) {
50             if (element.getClass().equals(elementClass))
51                 return element;
52         }
53         return null;
54     }
55
56     @Override
57     protected List<Object> getElements(Class elementClass) {
58         List<Object> elements = new ArrayList<>();
59         for (Object element : rngTrack.getLocationOrIdentifierOrTitle()) {
60             if (element.getClass().equals(elementClass))
61                 elements.add(element);
62         }
63         return elements;
64     }
65
66     @Override
67     protected List<Object> getAllElements() {
68         return rngTrack.getLocationOrIdentifierOrTitle();
69     }
70
71     @Override
72     public List<String> getLocations() {
73         List<String> locations = new ArrayList<>();
74         for (Object element : getElements(Location.class)) {
75             Location locationElement = (Location) element;
76             locations.add(
77                     locationElement.getValue()
78             );
79         }
80         return locations;
81     }
82
83     @Override
84     public void addLocation(String location) {
85         if (location != null) {
86             Location locationElement = RNG_FACTORY.createLocation();
87             locationElement.setValue(location);
88             getAllElements().add(locationElement);
89         }
90     }
91
92     @Override
93     public List<String> getIdentifiers() {
94         List<String> identifiers = new ArrayList<>();
95         for (Object element : getElements(Identifier.class)) {
96             Identifier identifierElement = (Identifier) element;
97             identifiers.add(
98                     identifierElement.getValue()
99             );
100         }
101         return identifiers;
102     }
103
104     @Override
105     public void addIdentifier(String identifier) {
106         if (identifier != null) {
107             Identifier identifierElement = RNG_FACTORY.createIdentifier();
108             identifierElement.setValue(identifier);
109             getAllElements().add(identifierElement);
110         }
111     }
112
113     @Override
114     public String getAlbum() {
115         Album albumElement = (Album) getElement(Album.class);
116         return albumElement != null ? albumElement.getContent() : null;
117     }
118
119     @Override
120     public void setAlbum(String album) {
121         Album albumElement = (Album) getElement(Album.class);
122         if (albumElement != null)
123             getAllElements().remove(albumElement);
124         if (album != null) {
125             albumElement = RNG_FACTORY.createAlbum();
126             albumElement.setContent(album);
127             getAllElements().add(albumElement);
128         }
129     }
130
131     @Override
132     public Integer getTrackNum() {
133         TrackNum trackNumElement = (TrackNum) getElement(TrackNum.class);
134         return trackNumElement != null && trackNumElement.getValue() != null
135                 ? trackNumElement.getValue().intValue()
136                 : null;
137     }
138
139     @Override
140     public void setTrackNum(Integer trackNum) {
141         TrackNum trackNumElement = (TrackNum) getElement(TrackNum.class);
142         if (trackNumElement != null)
143             getAllElements().remove(trackNumElement);
144         if (trackNum != null) {
145             trackNumElement = RNG_FACTORY.createTrackNum();
146             trackNumElement.setValue(
147                     BigInteger.valueOf(trackNum)
148             );
149             getAllElements().add(trackNumElement);
150         }
151     }
152
153     @Override
154     public Integer getDuration() {
155         Duration durationElement = (Duration) getElement(Duration.class);
156         return durationElement != null && durationElement.getValue() != null
157                 ? durationElement.getValue().intValue()
158                 : null;
159     }
160
161     @Override
162     public void setDuration(Integer duration) {
163         Duration durationElement = (Duration) getElement(Duration.class);
164         if (durationElement != null)
165             getAllElements().remove(durationElement);
166         if (duration != null) {
167             durationElement = RNG_FACTORY.createDuration();
168             durationElement.setValue(
169                     BigInteger.valueOf(duration)
170             );
171             getAllElements().add(durationElement);
172         }
173     }
174
175 }