Package: CddbAlbum$CddbAlbumBuilder
CddbAlbum$CddbAlbumBuilder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CddbAlbum.CddbAlbumBuilder() |
|
|
|
|
|
||||||||||||||||||||
build() |
|
|
|
|
|
||||||||||||||||||||
cddb(MediaItem.Metadata.Cddb) |
|
|
|
|
|
||||||||||||||||||||
properties(Map) |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.metadata.cddb;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.Immutable;
31: import java.util.Arrays;
32: import java.util.Map;
33: import java.util.Optional;
34: import java.util.TreeMap;
35: import java.util.regex.Matcher;
36: import java.util.regex.Pattern;
37: import java.util.stream.Stream;
38: import it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
39: import lombok.Builder;
40: import lombok.EqualsAndHashCode;
41: import lombok.Getter;
42: import lombok.ToString;
43: import static java.util.stream.Collectors.*;
44:
45: /***********************************************************************************************************************
46: *
47: * @author Fabrizio Giudici
48: *
49: **********************************************************************************************************************/
50: @Immutable @Getter @EqualsAndHashCode @ToString @Builder
51: public class CddbAlbum
52: {
53: private static final Pattern PATTERN_DISC_LENGTH = Pattern.compile("# *Disc length: ([0-9]+) *seconds");
54:
55: private static final Pattern PATTERN_TRACK_FRAME_OFFSETS = Pattern.compile("#\\s*[0-9]+");
56:
57: @Nonnull
58: private final Map<String, String> properties;
59:
60: @Nonnull
61: private final Cddb cddb;
62:
63: @Nonnull
64: public static CddbAlbum of (@Nonnull final String stringResponse)
65: {
66: final String[] split = stringResponse.split("\n");
67:
68: final Cddb cddb = Cddb.builder()
69: .discId("") // FIXME
70: .trackFrameOffsets(
71: Stream.of(split)
72: .skip(2)
73: .filter(string -> PATTERN_TRACK_FRAME_OFFSETS.matcher(string).matches())
74: .mapToInt(s -> Integer.parseInt(s.substring(1).trim()))
75: .toArray())
76: .discLength(
77: Stream.of(split)
78: .skip(2)
79: .map(PATTERN_DISC_LENGTH::matcher)
80: .filter(Matcher::matches)
81: .map(matcher -> Integer.parseInt(matcher.group(1)))
82: .findFirst()
83: .get())
84: .build();
85:
86: return builder().cddb(cddb)
87: .properties(
88: Stream.of(split)
89: .skip(2)
90: .filter(string -> !string.trim().isEmpty() && !string.startsWith("#"))
91: .map(string -> string.split("="))
92: .filter(strings -> strings.length == 2)
93: .collect(toMap(strings -> strings[0].trim(),
94: strings -> strings[1].trim(),
95: (u, v) -> u,
96: TreeMap::new)))
97: .build();
98: }
99:
100: @Nonnull
101: public Optional<String> getProperty (@Nonnull final String key)
102: {
103: return Optional.ofNullable(properties.get(key));
104: }
105:
106: @Nonnull
107: public String toDumpString()
108: {
109: return "CddbAlbum\n"
110: + "trackFrameOffsets: " + Arrays.stream(cddb.getTrackFrameOffsets())
111: .mapToObj(String::valueOf)
112: .collect(joining(" ")) + "\n"
113: + "discLenght: " + cddb.getDiscLength() + "\n"
114: + properties.entrySet().stream()
115: .map(e -> String.format("%s: %s", e.getKey(), e.getValue()))
116: .collect(joining("\n"));
117: }
118: }