MCPcopy Create free account
hub / github.com/MenoData/Time4J / getPartialAmount

Method getPartialAmount

base/src/main/java/net/time4j/Duration.java:1108–1145  ·  view source on GitHub ↗
(IsoUnit unit)

Source from the content-addressed store, hash-verified

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

Callers 1

withMethod · 0.95

Calls 8

isFractionUnitMethod · 0.95
getMethod · 0.65
getSymbolMethod · 0.65
sizeMethod · 0.45
getUnitMethod · 0.45
equalsMethod · 0.45
getAmountMethod · 0.45
absMethod · 0.45

Tested by

no test coverage detected