For IntelliJ IDEA and
Android Studio


@Override public int compare(ServiceProvider p1, ServiceProvider p2) { return p2.getPriority() - p1.getPriority(); } });
/** * Returns the default units factory. This factory uses the unit service provider which is * {@linkplain ServiceProvider#current() current} at the time of the first invocation of this method. * * @return the default units factory. */ public static synchronized Units getDefault() { if (DEFAULT == null) { DEFAULT = new Units(ServiceProvider.current().getSystemOfUnitsService().getSystemOfUnits()); } return DEFAULT; }
/** * Returns the list of available service providers. * * @return all available service providers. */ public static List<ServiceProvider> available() { return Arrays.asList(getProviders()); }
@Test(expected = NullPointerException.class) public void testSetDefault_Null() { ServiceProvider.setCurrent(null); }
/** * Tests {@link ServiceProvider#current()} and {@link ServiceProvider#setCurrent(ServiceProvider)}. The getter and setter are tested in a single * method for avoiding issues with the order in which JUnit executes tests. */ @Test public void testGetAndSetDefault() { assertEquals(0, ServiceProvider.available().size()); try { ServiceProvider.current(); fail("Expected no ServiceProvider before we set one."); } catch (IllegalStateException e) { // This is the expected exception. } TestServiceProvider testProv = new TestServiceProvider(); assertNull("Expected no ServiceProvider before we set one.", ServiceProvider.setCurrent(testProv)); assertSame("Setting the same ServiceProvider twice should be a no-op.", testProv, ServiceProvider.setCurrent(testProv)); assertSame(testProv, ServiceProvider.current()); assertArrayEquals(new ServiceProvider[] { testProv }, ServiceProvider.available().toArray()); }
private static void printSoU(final SystemOfUnits sou, final boolean showIndex) { int index = 0; System.out.println("Reporting " + sou.getName()); for (Unit<?> u : sou.getUnits()) { index++; if (showIndex) { System.out.println(index + "; " + u.getName() + "; " + u.getSymbol() + "; " + u); } else { System.out.println(u.getName() + "; " + u.getSymbol() + "; " + u); } } } }
/** * Creates a new factory which will use the given system of units. * * @param system the system of units to use for creating base units. */ protected Units(final SystemOfUnits system) { metre = system.getUnit(Length.class); radian = system.getUnit(Angle.class); second = system.getUnit(Time.class); one = getDimensionless(system); kilometre = metre .multiply(1000); degree = radian.multiply(Math.PI/180); day = second.multiply(24*60*60); ppm = one .divide(1000000); }
int compareTo(ServiceProvider o) { return compare(getPriority(), o.getPriority()); }
/** * Returns the dimension for the specified quantity type by aggregating the results of {@link DimensionService} or <code>null</code> if the * specified quantity is unknown. * * @param quantityType * the quantity type. * @return the dimension for the quantity type or <code>null</code>. * @since 1.0.2 */ public static <Q extends Quantity<Q>> Dimension of(Class<Q> quantityType) { // TODO: Track services and aggregate results (register custom // types) Unit<Q> siUnit = Units.getInstance().getUnit(quantityType); if (siUnit == null) logger.log(Level.FINER, "Quantity type: " + quantityType + " unknown"); // we're logging but probably FINER is // enough? return (siUnit != null) ? siUnit.getDimension() : null; }
/** * Returns the dimensionless unit. This is a workaround for what seems to be a bug * in the reference implementation 1.0.1 of unit API. */ private static Unit<Dimensionless> getDimensionless(final SystemOfUnits system) { Unit<Dimensionless> unit = system.getUnit(Dimensionless.class); if (unit == null) try { unit = ((Unit<?>) Class.forName("tec.units.ri.AbstractUnit").getField("ONE").get(null)).asType(Dimensionless.class); } catch (ReflectiveOperationException | ClassCastException e) { throw new IllegalArgumentException("Can not create a dimensionless unit from the given provider."); } return unit; }
@Override public int compareTo(ServiceProvider o) { return Integer.compare(getPriority(), o.getPriority()); }
/** * Returns the current {@link ServiceProvider}. If necessary the {@link ServiceProvider} will be lazily loaded. * <p> * If there are no providers available, an {@linkplain IllegalStateException} is thrown, otherwise the provider with the highest priority is used or * the one explicitly designated via {@link setCurrent()} . * </p> * * @return the {@link ServiceProvider} used. * @throws IllegalStateException * if no {@link ServiceProvider} has been found. * @see #getPriority() * @see #setCurrent() */ public static ServiceProvider current() { ServiceProvider[] p = getProviders(); if (p.length != 0) { return p[0]; } throw new IllegalStateException("No measurement ServiceProvider found."); }
@Override public int compare(ServiceProvider p1, ServiceProvider p2) { return p2.getPriority() - p1.getPriority(); } });
/** * Returns the list of available service providers. * * @return all available service providers. */ public static List<ServiceProvider> available() { return Arrays.asList(getProviders()); }
/** * Returns the current {@link ServiceProvider}. If necessary the {@link ServiceProvider} will be lazily loaded. * <p> * If there are no providers available, an {@linkplain IllegalStateException} is thrown, otherwise the provider with the highest priority is used or * the one explicitly designated via {@link #setCurrent(ServiceProvider)} . * </p> * * @return the {@link ServiceProvider} used. * @throws IllegalStateException * if no {@link ServiceProvider} has been found. * @see #getPriority() * @see #setCurrent(ServiceProvider) */ public static ServiceProvider current() { ServiceProvider[] p = getProviders(); if (p.length != 0) { return p[0]; } throw new IllegalStateException("No measurement ServiceProvider found."); }