source: josm/trunk/src/com/drew/metadata/Age.java@ 14160

Last change on this file since 14160 was 13061, checked in by Don-vip, 7 years ago

fix #15505 - update to metadata-extractor 2.10.1

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.6 KB
Line 
1/*
2 * Copyright 2002-2017 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
22package com.drew.metadata;
23
24import com.drew.lang.annotations.NotNull;
25import 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 */
34public 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.length() != 19 || s.startsWith("9999:99:99"))
54 return null;
55
56 try {
57 int years = Integer.parseInt(s.substring(0, 4));
58 int months = Integer.parseInt(s.substring(5, 7));
59 int days = Integer.parseInt(s.substring(8, 10));
60 int hours = Integer.parseInt(s.substring(11, 13));
61 int minutes = Integer.parseInt(s.substring(14, 16));
62 int seconds = Integer.parseInt(s.substring(17, 19));
63
64 return new Age(years, months, days, hours, minutes, seconds);
65 }
66 catch (NumberFormatException ignored)
67 {
68 return null;
69 }
70 }
71
72 public Age(int years, int months, int days, int hours, int minutes, int seconds)
73 {
74 _years = years;
75 _months = months;
76 _days = days;
77 _hours = hours;
78 _minutes = minutes;
79 _seconds = seconds;
80 }
81
82 public int getYears()
83 {
84 return _years;
85 }
86
87 public int getMonths()
88 {
89 return _months;
90 }
91
92 public int getDays()
93 {
94 return _days;
95 }
96
97 public int getHours()
98 {
99 return _hours;
100 }
101
102 public int getMinutes()
103 {
104 return _minutes;
105 }
106
107 public int getSeconds()
108 {
109 return _seconds;
110 }
111
112 @Override
113 public String toString()
114 {
115 return String.format("%04d:%02d:%02d %02d:%02d:%02d", _years, _months, _days, _hours, _minutes, _seconds);
116 }
117
118 public String toFriendlyString()
119 {
120 StringBuilder result = new StringBuilder();
121 appendAgePart(result, _years, "year");
122 appendAgePart(result, _months, "month");
123 appendAgePart(result, _days, "day");
124 appendAgePart(result, _hours, "hour");
125 appendAgePart(result, _minutes, "minute");
126 appendAgePart(result, _seconds, "second");
127 return result.toString();
128 }
129
130 private static void appendAgePart(StringBuilder result, final int num, final String singularName)
131 {
132 if (num == 0)
133 return;
134 if (result.length()!=0)
135 result.append(' ');
136 result.append(num).append(' ').append(singularName);
137 if (num != 1)
138 result.append('s');
139 }
140
141 @Override
142 public boolean equals(@Nullable Object o)
143 {
144 if (this == o) return true;
145 if (o == null || getClass() != o.getClass()) return false;
146
147 Age age = (Age)o;
148
149 if (_days != age._days) return false;
150 if (_hours != age._hours) return false;
151 if (_minutes != age._minutes) return false;
152 if (_months != age._months) return false;
153 if (_seconds != age._seconds) return false;
154 if (_years != age._years) return false;
155
156 return true;
157 }
158
159 @Override
160 public int hashCode()
161 {
162 int result = _years;
163 result = 31 * result + _months;
164 result = 31 * result + _days;
165 result = 31 * result + _hours;
166 result = 31 * result + _minutes;
167 result = 31 * result + _seconds;
168 return result;
169 }
170}
Note: See TracBrowser for help on using the repository browser.