diff --git a/src/main/java/com/conversions/BinaryToHexadecimal.java b/src/main/java/com/conversions/BinaryToHexadecimal.java index fc891570..c7810f11 100644 --- a/src/main/java/com/conversions/BinaryToHexadecimal.java +++ b/src/main/java/com/conversions/BinaryToHexadecimal.java @@ -36,7 +36,7 @@ public class BinaryToHexadecimal { String hex = ""; int currentBit; - BigInteger tenValue = new BigInteger("10"); + BigInteger tenValue = BigInteger.valueOf(10); while (binary.compareTo(BigInteger.ZERO) != 0) { // to store decimal equivalent of number formed by 4 decimal digits int code4 = 0; diff --git a/src/main/java/com/conversions/DecimalToHexadecimal.java b/src/main/java/com/conversions/DecimalToHexadecimal.java index e0e9360d..0c812f52 100644 --- a/src/main/java/com/conversions/DecimalToHexadecimal.java +++ b/src/main/java/com/conversions/DecimalToHexadecimal.java @@ -4,7 +4,7 @@ import java.math.BigInteger; public class DecimalToHexadecimal { private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - private static final BigInteger valueHex = new BigInteger("16"); + private static final BigInteger valueHex = BigInteger.valueOf(16); /** * This method converts and decimal number to a Hexadecimal number diff --git a/src/main/java/com/conversions/DecimalToOctal.java b/src/main/java/com/conversions/DecimalToOctal.java index 6ab6e72b..64542298 100644 --- a/src/main/java/com/conversions/DecimalToOctal.java +++ b/src/main/java/com/conversions/DecimalToOctal.java @@ -4,7 +4,7 @@ import java.math.BigInteger; public class DecimalToOctal { private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'}; - private static final BigInteger valueOctal = new BigInteger("8"); + private static final BigInteger valueOctal = BigInteger.valueOf(8); /** * This method converts and decimal number to a octal number diff --git a/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java b/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java new file mode 100644 index 00000000..4cffba43 --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java @@ -0,0 +1,8 @@ +package src.main.java.com.designpatterns.structural.adapter; + +public class BugattiVeyron implements Movable { + @Override + public double getSpeed() { + return 268; + } +} diff --git a/src/main/java/com/designpatterns/structural/adapter/Movable.java b/src/main/java/com/designpatterns/structural/adapter/Movable.java new file mode 100644 index 00000000..eeca412a --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/Movable.java @@ -0,0 +1,6 @@ +package src.main.java.com.designpatterns.structural.adapter; + +public interface Movable { + // Returns the speed of the movable in MPH + double getSpeed(); +} diff --git a/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java b/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java new file mode 100644 index 00000000..3cd11eac --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java @@ -0,0 +1,16 @@ +package src.main.java.com.designpatterns.structural.adapter; + +/** + * An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected + * directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s + * interface. + *
+ * The main motive behind using this pattern is to convert an existing interface into another interface that the client + * expects. It’s usually implemented once the application is designed. + * + * @see Adapter Pattern + */ +public interface MovableAdapter { + // Returns the speed of the movable in KPH + double getSpeed(); +} diff --git a/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java b/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java new file mode 100644 index 00000000..c8d819a1 --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java @@ -0,0 +1,18 @@ +package src.main.java.com.designpatterns.structural.adapter; + +public class MovableAdapterImpl implements MovableAdapter { + private Movable luxuryCars; + + public MovableAdapterImpl(Movable luxuryCars) { + this.luxuryCars = luxuryCars; + } + + @Override + public double getSpeed() { + return convertMPHtoKMPH(luxuryCars.getSpeed()); + } + + private double convertMPHtoKMPH(double mph) { + return mph * 1.60934; + } +} diff --git a/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java b/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java new file mode 100644 index 00000000..17ff9e49 --- /dev/null +++ b/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java @@ -0,0 +1,18 @@ +package src.test.java.com.designpatterns.structural.adapter; + +import org.junit.Test; +import src.main.java.com.designpatterns.structural.adapter.BugattiVeyron; +import src.main.java.com.designpatterns.structural.adapter.Movable; +import src.main.java.com.designpatterns.structural.adapter.MovableAdapter; +import src.main.java.com.designpatterns.structural.adapter.MovableAdapterImpl; + +import static org.junit.Assert.assertEquals; + +public class MovableAdapterTest { + @Test + public void testMovableAdapter() { + Movable bugattiVeyron = new BugattiVeyron(); + MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron); + assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001); + } +}