| 1106 | * @return non-negative amount associated with given unit ({@code >= 0}) |
| 1107 | */ |
| 1108 | @Override |
| 1109 | public long getPartialAmount(IsoUnit unit) { |
| 1110 | |
| 1111 | if (unit == null) { |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | boolean fractional = isFractionUnit(unit); |
| 1116 | |
| 1117 | for (int i = 0, n = this.items.size(); i < n; i++) { |
| 1118 | Item<U> item = this.items.get(i); |
| 1119 | U u = item.getUnit(); |
| 1120 | |
| 1121 | if (u.equals(unit)) { |
| 1122 | return item.getAmount(); |
| 1123 | } else if ( |
| 1124 | fractional |
| 1125 | && isFractionUnit(u) |
| 1126 | ) { |
| 1127 | int d1 = u.getSymbol() - '0'; |
| 1128 | int d2 = unit.getSymbol() - '0'; |
| 1129 | int factor = 1; |
| 1130 | |
| 1131 | for (int j = 0, m = Math.abs(d1 - d2); j < m; j++) { |
| 1132 | factor *= 10; |
| 1133 | } |
| 1134 | |
| 1135 | if (d1 >= d2) { |
| 1136 | return item.getAmount() / factor; |
| 1137 | } else { |
| 1138 | return item.getAmount() * factor; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | return 0; |
| 1144 | |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * <p>Creates a {@code Comparator} which compares durations based on |