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