Ignore:
Timestamp:
2017-04-15T02:00:43+02:00 (7 years ago)
Author:
Don-vip
Message:

sonar - squid:S2972 - Inner classes should not have too many lines of code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java

    r11746 r11914  
    3535import java.util.Hashtable;
    3636import java.util.List;
    37 import java.util.Locale;
    38 import java.util.Objects;
    3937import java.util.Optional;
    4038import java.util.TimeZone;
     
    12511249        return endIndex;
    12521250    }
    1253 
    1254     static final class Timezone {
    1255 
    1256         static final Timezone ZERO = new Timezone(0.0);
    1257         private final double timezone;
    1258 
    1259         Timezone(double hours) {
    1260             this.timezone = hours;
    1261         }
    1262 
    1263         public double getHours() {
    1264             return timezone;
    1265         }
    1266 
    1267         String formatTimezone() {
    1268             StringBuilder ret = new StringBuilder();
    1269 
    1270             double timezone = this.timezone;
    1271             if (timezone < 0) {
    1272                 ret.append('-');
    1273                 timezone = -timezone;
    1274             } else {
    1275                 ret.append('+');
    1276             }
    1277             ret.append((long) timezone).append(':');
    1278             int minutes = (int) ((timezone % 1) * 60);
    1279             if (minutes < 10) {
    1280                 ret.append('0');
    1281             }
    1282             ret.append(minutes);
    1283 
    1284             return ret.toString();
    1285         }
    1286 
    1287         static Timezone parseTimezone(String timezone) throws ParseException {
    1288 
    1289             if (timezone.isEmpty())
    1290                 return ZERO;
    1291 
    1292             String error = tr("Error while parsing timezone.\nExpected format: {0}", "+H:MM");
    1293 
    1294             char sgnTimezone = '+';
    1295             StringBuilder hTimezone = new StringBuilder();
    1296             StringBuilder mTimezone = new StringBuilder();
    1297             int state = 1; // 1=start/sign, 2=hours, 3=minutes.
    1298             for (int i = 0; i < timezone.length(); i++) {
    1299                 char c = timezone.charAt(i);
    1300                 switch (c) {
    1301                     case ' ':
    1302                         if (state != 2 || hTimezone.length() != 0)
    1303                             throw new ParseException(error, i);
    1304                         break;
    1305                     case '+':
    1306                     case '-':
    1307                         if (state == 1) {
    1308                             sgnTimezone = c;
    1309                             state = 2;
    1310                         } else
    1311                             throw new ParseException(error, i);
    1312                         break;
    1313                     case ':':
    1314                     case '.':
    1315                         if (state == 2) {
    1316                             state = 3;
    1317                         } else
    1318                             throw new ParseException(error, i);
    1319                         break;
    1320                     case '0':
    1321                     case '1':
    1322                     case '2':
    1323                     case '3':
    1324                     case '4':
    1325                     case '5':
    1326                     case '6':
    1327                     case '7':
    1328                     case '8':
    1329                     case '9':
    1330                         switch (state) {
    1331                             case 1:
    1332                             case 2:
    1333                                 state = 2;
    1334                                 hTimezone.append(c);
    1335                                 break;
    1336                             case 3:
    1337                                 mTimezone.append(c);
    1338                                 break;
    1339                             default:
    1340                                 throw new ParseException(error, i);
    1341                         }
    1342                         break;
    1343                     default:
    1344                         throw new ParseException(error, i);
    1345                 }
    1346             }
    1347 
    1348             int h = 0;
    1349             int m = 0;
    1350             try {
    1351                 h = Integer.parseInt(hTimezone.toString());
    1352                 if (mTimezone.length() > 0) {
    1353                     m = Integer.parseInt(mTimezone.toString());
    1354                 }
    1355             } catch (NumberFormatException nfe) {
    1356                 // Invalid timezone
    1357                 throw (ParseException) new ParseException(error, 0).initCause(nfe);
    1358             }
    1359 
    1360             if (h > 12 || m > 59)
    1361                 throw new ParseException(error, 0);
    1362             else
    1363                 return new Timezone((h + m / 60.0) * (sgnTimezone == '-' ? -1 : 1));
    1364         }
    1365 
    1366         @Override
    1367         public boolean equals(Object o) {
    1368             if (this == o) return true;
    1369             if (!(o instanceof Timezone)) return false;
    1370             Timezone timezone1 = (Timezone) o;
    1371             return Double.compare(timezone1.timezone, timezone) == 0;
    1372         }
    1373 
    1374         @Override
    1375         public int hashCode() {
    1376             return Objects.hash(timezone);
    1377         }
    1378     }
    1379 
    1380     static final class Offset {
    1381 
    1382         static final Offset ZERO = new Offset(0);
    1383         private final long milliseconds;
    1384 
    1385         private Offset(long milliseconds) {
    1386             this.milliseconds = milliseconds;
    1387         }
    1388 
    1389         static Offset milliseconds(long milliseconds) {
    1390             return new Offset(milliseconds);
    1391         }
    1392 
    1393         static Offset seconds(long seconds) {
    1394             return new Offset(1000 * seconds);
    1395         }
    1396 
    1397         long getMilliseconds() {
    1398             return milliseconds;
    1399         }
    1400 
    1401         long getSeconds() {
    1402             return milliseconds / 1000;
    1403         }
    1404 
    1405         String formatOffset() {
    1406             if (milliseconds % 1000 == 0) {
    1407                 return Long.toString(milliseconds / 1000);
    1408             } else if (milliseconds % 100 == 0) {
    1409                 return String.format(Locale.ENGLISH, "%.1f", milliseconds / 1000.);
    1410             } else {
    1411                 return String.format(Locale.ENGLISH, "%.3f", milliseconds / 1000.);
    1412             }
    1413         }
    1414 
    1415         static Offset parseOffset(String offset) throws ParseException {
    1416             String error = tr("Error while parsing offset.\nExpected format: {0}", "number");
    1417 
    1418             if (!offset.isEmpty()) {
    1419                 try {
    1420                     if (offset.startsWith("+")) {
    1421                         offset = offset.substring(1);
    1422                     }
    1423                     return Offset.milliseconds(Math.round(Double.parseDouble(offset) * 1000));
    1424                 } catch (NumberFormatException nfe) {
    1425                     throw (ParseException) new ParseException(error, 0).initCause(nfe);
    1426                 }
    1427             } else {
    1428                 return Offset.ZERO;
    1429             }
    1430         }
    1431 
    1432         int getDayOffset() {
    1433             // Find day difference
    1434             return (int) Math.round(((double) getMilliseconds()) / TimeUnit.DAYS.toMillis(1));
    1435         }
    1436 
    1437         Offset withoutDayOffset() {
    1438             return milliseconds(getMilliseconds() - TimeUnit.DAYS.toMillis(getDayOffset()));
    1439         }
    1440 
    1441         Pair<Timezone, Offset> splitOutTimezone() {
    1442             // In hours
    1443             final double tz = ((double) withoutDayOffset().getSeconds()) / TimeUnit.HOURS.toSeconds(1);
    1444 
    1445             // Due to imprecise clocks we might get a "+3:28" timezone, which should obviously be 3:30 with
    1446             // -2 minutes offset. This determines the real timezone and finds offset.
    1447             final double timezone = (double) Math.round(tz * 2) / 2; // hours, rounded to one decimal place
    1448             final long delta = Math.round(getMilliseconds() - timezone * TimeUnit.HOURS.toMillis(1));
    1449             return Pair.create(new Timezone(timezone), Offset.milliseconds(delta));
    1450         }
    1451 
    1452         @Override
    1453         public boolean equals(Object o) {
    1454             if (this == o) return true;
    1455             if (!(o instanceof Offset)) return false;
    1456             Offset offset = (Offset) o;
    1457             return milliseconds == offset.milliseconds;
    1458         }
    1459 
    1460         @Override
    1461         public int hashCode() {
    1462             return Objects.hash(milliseconds);
    1463         }
    1464     }
    14651251}
Note: See TracChangeset for help on using the changeset viewer.