Skip to content

Provides a simple way to make measurement conversions between METRIC and IMPERIAL systems.

Notifications You must be signed in to change notification settings

jonathan-sh/measurement-convert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

run-test

About

Provides a simple way to make measurement conversions between METRIC and IMPERIAL systems.

A special thanks to @lat94 for being my guinea pig and for the suggestions 😊.

Install

Step 1: add the dependency

<dependency>
    <groupId>io.jonathan</groupId>
    <artifactId>measurement-convert</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

Step 2: add the repository

<repositories>
    <repository>
        <id>github</id>
        <url>https://maven.pkg.github.com/jonathan-sh/measurement-convert</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

Step 3: install the package

mvn install -U

Using

Adding the annotations to my object

import io.jonathan.converter.MeasurementConvert;

import static io.jonathan.converter.Measurement.CELSIUS;
import static io.jonathan.converter.Measurement.FAHRENHEIT;

public record ExampleEntity(
        //the value in numeric form, which receives <conversion>
        @MeasurementConvert(current = CELSIUS, metric = CELSIUS, imperial = FAHRENHEIT)
        Double temperatureNum,
        //the value in text form, which receives <conversion> + <symbol>
        @MeasurementConvert(current = CELSIUS, metric = CELSIUS, imperial = FAHRENHEIT)
        String temperatureTxt,
        //other values of the entity remained untouched
        String notTouchValue
) {
}

Applying the systems

 public static void main(String[] args) {
    var input = new ExampleEntity(38.0, "38", "hey");

    //applying the metric system
    MeasurementConverter
            .applyMetricSystem(input)
            .ifPresent(metricOutput -> {
                System.out.println(metricOutput.temperatureNum());
                //38.0
                System.out.println(metricOutput.temperatureTxt());
                //38ºC
                System.out.println(metricOutput.notTouchValue());
                //hey
            });

    //applying the imperial system
    MeasurementConverter
            .applyImperialSystem(input)
            .ifPresent(imperialOutput -> {
                System.out.println(imperialOutput.temperatureNum());
                //100.4
                System.out.println(imperialOutput.temperatureTxt());
                //100.4ºF
                System.out.println(imperialOutput.notTouchValue());
                //hey
            });
}

About the annotation

public @interface MeasurementConvert {

    /**
     * The unit of measurement in which the object will be instantiated
     */
    Measurement current() default Measurement.NONE;


    /**
     * The desired unit of measurement when the metric system was applied
     */
    Measurement metric() default Measurement.NONE;


    /**
     * The desired unit of measurement when the imperial system was applied
     */
    Measurement imperial() default Measurement.NONE;


    /**
     * Controls the output format in DECIMAL(#.##) or INTEGER(#)
     */
    String formatter() default Processor.DECIMAL;
}

Adding a new conversion function

To add new measurement conversion functions, follow these steps:

  1. Create a class that implements the MeasurementConvertFunction interface.
public class MyNewFunction implements MeasurementConvertFunction {
    @Override
    public Measurement from() {
        return null;
    }

    @Override
    public Measurement to() {
        return null;
    }

    @Override
    public Double apply(Double value) {
        return 0.0;
    }

    @Override
    public Double reverse(Double value) {
        return 0.0;
    }
}
  1. Register your conversion function.
MeasurementConvertProcessor.registerConversionFunctions(new MyNewFunction());

About

Provides a simple way to make measurement conversions between METRIC and IMPERIAL systems.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages