| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
} |