1 | /*
|
---|
2 | * Copyright 2002-2016 Drew Noakes
|
---|
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 | * More information about this project is available at:
|
---|
17 | *
|
---|
18 | * https://drewnoakes.com/code/exif/
|
---|
19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
20 | */
|
---|
21 |
|
---|
22 | package com.drew.metadata;
|
---|
23 |
|
---|
24 | import com.drew.lang.annotations.NotNull;
|
---|
25 | import com.drew.lang.annotations.Nullable;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Represents an age in years, months, days, hours, minutes and seconds.
|
---|
29 | * <p>
|
---|
30 | * Used by certain Panasonic cameras which have face recognition features.
|
---|
31 | *
|
---|
32 | * @author Drew Noakes https://drewnoakes.com
|
---|
33 | */
|
---|
34 | public class Age
|
---|
35 | {
|
---|
36 | private final int _years;
|
---|
37 | private final int _months;
|
---|
38 | private final int _days;
|
---|
39 | private final int _hours;
|
---|
40 | private final int _minutes;
|
---|
41 | private final int _seconds;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Parses an age object from the string format used by Panasonic cameras:
|
---|
45 | * <code>0031:07:15 00:00:00</code>
|
---|
46 | *
|
---|
47 | * @param s The String in format <code>0031:07:15 00:00:00</code>.
|
---|
48 | * @return The parsed Age object, or null if the value could not be parsed
|
---|
49 | */
|
---|
50 | @Nullable
|
---|
51 | public static Age fromPanasonicString(@NotNull String s)
|
---|
52 | {
|
---|
53 | if (s == null)
|
---|
54 | throw new NullPointerException();
|
---|
55 |
|
---|
56 | if (s.length() != 19 || s.startsWith("9999:99:99"))
|
---|
57 | return null;
|
---|
58 |
|
---|
59 | try {
|
---|
60 | int years = Integer.parseInt(s.substring(0, 4));
|
---|
61 | int months = Integer.parseInt(s.substring(5, 7));
|
---|
62 | int days = Integer.parseInt(s.substring(8, 10));
|
---|
63 | int hours = Integer.parseInt(s.substring(11, 13));
|
---|
64 | int minutes = Integer.parseInt(s.substring(14, 16));
|
---|
65 | int seconds = Integer.parseInt(s.substring(17, 19));
|
---|
66 |
|
---|
67 | return new Age(years, months, days, hours, minutes, seconds);
|
---|
68 | }
|
---|
69 | catch (NumberFormatException ignored)
|
---|
70 | {
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public Age(int years, int months, int days, int hours, int minutes, int seconds)
|
---|
76 | {
|
---|
77 | _years = years;
|
---|
78 | _months = months;
|
---|
79 | _days = days;
|
---|
80 | _hours = hours;
|
---|
81 | _minutes = minutes;
|
---|
82 | _seconds = seconds;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public int getYears()
|
---|
86 | {
|
---|
87 | return _years;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public int getMonths()
|
---|
91 | {
|
---|
92 | return _months;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public int getDays()
|
---|
96 | {
|
---|
97 | return _days;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public int getHours()
|
---|
101 | {
|
---|
102 | return _hours;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public int getMinutes()
|
---|
106 | {
|
---|
107 | return _minutes;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public int getSeconds()
|
---|
111 | {
|
---|
112 | return _seconds;
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public String toString()
|
---|
117 | {
|
---|
118 | return String.format("%04d:%02d:%02d %02d:%02d:%02d", _years, _months, _days, _hours, _minutes, _seconds);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public String toFriendlyString()
|
---|
122 | {
|
---|
123 | StringBuilder result = new StringBuilder();
|
---|
124 | appendAgePart(result, _years, "year");
|
---|
125 | appendAgePart(result, _months, "month");
|
---|
126 | appendAgePart(result, _days, "day");
|
---|
127 | appendAgePart(result, _hours, "hour");
|
---|
128 | appendAgePart(result, _minutes, "minute");
|
---|
129 | appendAgePart(result, _seconds, "second");
|
---|
130 | return result.toString();
|
---|
131 | }
|
---|
132 |
|
---|
133 | private static void appendAgePart(StringBuilder result, final int num, final String singularName)
|
---|
134 | {
|
---|
135 | if (num == 0)
|
---|
136 | return;
|
---|
137 | if (result.length()!=0)
|
---|
138 | result.append(' ');
|
---|
139 | result.append(num).append(' ').append(singularName);
|
---|
140 | if (num != 1)
|
---|
141 | result.append('s');
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public boolean equals(Object o)
|
---|
146 | {
|
---|
147 | if (this == o) return true;
|
---|
148 | if (o == null || getClass() != o.getClass()) return false;
|
---|
149 |
|
---|
150 | Age age = (Age)o;
|
---|
151 |
|
---|
152 | if (_days != age._days) return false;
|
---|
153 | if (_hours != age._hours) return false;
|
---|
154 | if (_minutes != age._minutes) return false;
|
---|
155 | if (_months != age._months) return false;
|
---|
156 | if (_seconds != age._seconds) return false;
|
---|
157 | if (_years != age._years) return false;
|
---|
158 |
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public int hashCode()
|
---|
164 | {
|
---|
165 | int result = _years;
|
---|
166 | result = 31 * result + _months;
|
---|
167 | result = 31 * result + _days;
|
---|
168 | result = 31 * result + _hours;
|
---|
169 | result = 31 * result + _minutes;
|
---|
170 | result = 31 * result + _seconds;
|
---|
171 | return result;
|
---|
172 | }
|
---|
173 | }
|
---|