Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -7,8 +7,7 @@ import org.junit.Test;
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
import static java.util.stream.Collectors.*;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -20,9 +19,9 @@ public class ArrayListTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<String> xs = LongStream.range(0, 16)
|
||||
.boxed()
|
||||
.map(Long::toHexString)
|
||||
.collect(toList());
|
||||
.boxed()
|
||||
.map(Long::toHexString)
|
||||
.collect(toCollection(ArrayList::new));
|
||||
stringsToSearch = new ArrayList<>(xs);
|
||||
stringsToSearch.addAll(xs);
|
||||
}
|
||||
@@ -36,7 +35,7 @@ public class ArrayListTest {
|
||||
@Test
|
||||
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
|
||||
Collection<Integer> numbers =
|
||||
IntStream.range(0, 10).boxed().collect(toSet());
|
||||
IntStream.range(0, 10).boxed().collect(toSet());
|
||||
|
||||
List<Integer> xs = new ArrayList<>(numbers);
|
||||
assertEquals(10, xs.size());
|
||||
@@ -57,8 +56,8 @@ public class ArrayListTest {
|
||||
@Test
|
||||
public void givenCollection_whenAddToArrayList_thenIsAdded() {
|
||||
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
|
||||
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(toList());
|
||||
xs.addAll(0, ys);
|
||||
LongStream.range(4, 10).boxed()
|
||||
.collect(collectingAndThen(toCollection(ArrayList::new), ys -> xs.addAll(0, ys)));
|
||||
|
||||
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
|
||||
}
|
||||
@@ -90,9 +89,9 @@ public class ArrayListTest {
|
||||
Set<String> matchingStrings = new HashSet<>(Arrays.asList("a", "c", "9"));
|
||||
|
||||
List<String> result = stringsToSearch
|
||||
.stream()
|
||||
.filter(matchingStrings::contains)
|
||||
.collect(toList());
|
||||
.stream()
|
||||
.filter(matchingStrings::contains)
|
||||
.collect(toCollection(ArrayList::new));
|
||||
|
||||
assertEquals(6, result.size());
|
||||
}
|
||||
@@ -107,7 +106,7 @@ public class ArrayListTest {
|
||||
|
||||
@Test
|
||||
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
|
||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
||||
Collections.reverse(xs);
|
||||
|
||||
xs.remove(0);
|
||||
@@ -119,7 +118,7 @@ public class ArrayListTest {
|
||||
|
||||
@Test
|
||||
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
|
||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
||||
ListIterator<Integer> it = xs.listIterator(xs.size());
|
||||
List<Integer> result = new ArrayList<>(xs.size());
|
||||
while (it.hasPrevious()) {
|
||||
@@ -133,7 +132,7 @@ public class ArrayListTest {
|
||||
@Test
|
||||
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
|
||||
Set<String> matchingStrings
|
||||
= Sets.newHashSet("a", "b", "c", "d", "e", "f");
|
||||
= Sets.newHashSet("a", "b", "c", "d", "e", "f");
|
||||
|
||||
Iterator<String> it = stringsToSearch.iterator();
|
||||
while (it.hasNext()) {
|
||||
|
||||
@@ -5,63 +5,59 @@ import java.util.HashSet;
|
||||
|
||||
public class ComplexClass {
|
||||
|
||||
private ArrayList<?> genericArrayList;
|
||||
private HashSet<Integer> integerHashSet;
|
||||
private ArrayList<?> genericArrayList;
|
||||
private HashSet<Integer> integerHashSet;
|
||||
|
||||
public ComplexClass(ArrayList<?> genericArrayList,
|
||||
HashSet<Integer> integerHashSet) {
|
||||
super();
|
||||
this.genericArrayList = genericArrayList;
|
||||
this.integerHashSet = integerHashSet;
|
||||
}
|
||||
public ComplexClass(ArrayList<?> genericArrayList, HashSet<Integer> integerHashSet) {
|
||||
super();
|
||||
this.genericArrayList = genericArrayList;
|
||||
this.integerHashSet = integerHashSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime
|
||||
* result
|
||||
+ ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
|
||||
result = prime * result
|
||||
+ ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
|
||||
result = prime * result + ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof ComplexClass))
|
||||
return false;
|
||||
ComplexClass other = (ComplexClass) obj;
|
||||
if (genericArrayList == null) {
|
||||
if (other.genericArrayList != null)
|
||||
return false;
|
||||
} else if (!genericArrayList.equals(other.genericArrayList))
|
||||
return false;
|
||||
if (integerHashSet == null) {
|
||||
if (other.integerHashSet != null)
|
||||
return false;
|
||||
} else if (!integerHashSet.equals(other.integerHashSet))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof ComplexClass))
|
||||
return false;
|
||||
ComplexClass other = (ComplexClass) obj;
|
||||
if (genericArrayList == null) {
|
||||
if (other.genericArrayList != null)
|
||||
return false;
|
||||
} else if (!genericArrayList.equals(other.genericArrayList))
|
||||
return false;
|
||||
if (integerHashSet == null) {
|
||||
if (other.integerHashSet != null)
|
||||
return false;
|
||||
} else if (!integerHashSet.equals(other.integerHashSet))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected ArrayList<?> getGenericArrayList() {
|
||||
return genericArrayList;
|
||||
}
|
||||
protected ArrayList<?> getGenericArrayList() {
|
||||
return genericArrayList;
|
||||
}
|
||||
|
||||
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
|
||||
this.genericArrayList = genericArrayList;
|
||||
}
|
||||
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
|
||||
this.genericArrayList = genericArrayList;
|
||||
}
|
||||
|
||||
protected HashSet<Integer> getIntegerHashSet() {
|
||||
return integerHashSet;
|
||||
}
|
||||
protected HashSet<Integer> getIntegerHashSet() {
|
||||
return integerHashSet;
|
||||
}
|
||||
|
||||
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
|
||||
this.integerHashSet = integerHashSet;
|
||||
}
|
||||
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
|
||||
this.integerHashSet = integerHashSet;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,53 +2,53 @@ package org.baeldung.equalshashcode.entities;
|
||||
|
||||
public class PrimitiveClass {
|
||||
|
||||
private boolean primitiveBoolean;
|
||||
private int primitiveInt;
|
||||
private boolean primitiveBoolean;
|
||||
private int primitiveInt;
|
||||
|
||||
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
|
||||
super();
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
|
||||
super();
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
|
||||
protected boolean isPrimitiveBoolean() {
|
||||
return primitiveBoolean;
|
||||
}
|
||||
protected boolean isPrimitiveBoolean() {
|
||||
return primitiveBoolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (primitiveBoolean ? 1231 : 1237);
|
||||
result = prime * result + primitiveInt;
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (primitiveBoolean ? 1231 : 1237);
|
||||
result = prime * result + primitiveInt;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
PrimitiveClass other = (PrimitiveClass) obj;
|
||||
if (primitiveBoolean != other.primitiveBoolean)
|
||||
return false;
|
||||
if (primitiveInt != other.primitiveInt)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
PrimitiveClass other = (PrimitiveClass) obj;
|
||||
if (primitiveBoolean != other.primitiveBoolean)
|
||||
return false;
|
||||
if (primitiveInt != other.primitiveInt)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
}
|
||||
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
}
|
||||
|
||||
protected int getPrimitiveInt() {
|
||||
return primitiveInt;
|
||||
}
|
||||
protected int getPrimitiveInt() {
|
||||
return primitiveInt;
|
||||
}
|
||||
|
||||
protected void setPrimitiveInt(int primitiveInt) {
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
protected void setPrimitiveInt(int primitiveInt) {
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,60 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private double width;
|
||||
private double length;
|
||||
private double width;
|
||||
private double length;
|
||||
|
||||
public Rectangle(double width, double length) {
|
||||
this.width = width;
|
||||
this.length = length;
|
||||
}
|
||||
public Rectangle(double width, double length) {
|
||||
this.width = width;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double area() {
|
||||
// A = w * l
|
||||
return width * length;
|
||||
}
|
||||
@Override
|
||||
public double area() {
|
||||
// A = w * l
|
||||
return width * length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double perimeter() {
|
||||
// P = 2(w + l)
|
||||
return 2 * (width + length);
|
||||
}
|
||||
@Override
|
||||
public double perimeter() {
|
||||
// P = 2(w + l)
|
||||
return 2 * (width + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(length);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(width);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(length);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(width);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Rectangle other = (Rectangle) obj;
|
||||
if (Double.doubleToLongBits(length) != Double
|
||||
.doubleToLongBits(other.length))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(width) != Double
|
||||
.doubleToLongBits(other.width))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Rectangle other = (Rectangle) obj;
|
||||
if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected double getWidth() {
|
||||
return width;
|
||||
}
|
||||
protected double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
protected double getLength() {
|
||||
return length;
|
||||
}
|
||||
protected double getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
public abstract class Shape {
|
||||
public abstract double area();
|
||||
public abstract double area();
|
||||
|
||||
public abstract double perimeter();
|
||||
public abstract double perimeter();
|
||||
}
|
||||
|
||||
@@ -4,44 +4,55 @@ import java.awt.Color;
|
||||
|
||||
public class Square extends Rectangle {
|
||||
|
||||
Color color;
|
||||
Color color;
|
||||
|
||||
public Square(double width, Color color) {
|
||||
super(width, width);
|
||||
this.color = color;
|
||||
}
|
||||
public Square(double width, Color color) {
|
||||
super(width, width);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result + ((color == null) ? 0 : color.hashCode());
|
||||
return result;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result + ((color == null) ? 0 : color.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!super.equals(obj))
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Square other = (Square) obj;
|
||||
if (color == null) {
|
||||
if (other.color != null)
|
||||
return false;
|
||||
} else if (!color.equals(other.color))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!super.equals(obj)) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof Square)) {
|
||||
return false;
|
||||
}
|
||||
Square other = (Square) obj;
|
||||
if (color == null) {
|
||||
if (other.color != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!color.equals(other.color)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Color getColor() {
|
||||
return color;
|
||||
}
|
||||
protected Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
protected void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
protected void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,32 +8,27 @@ import org.junit.Test;
|
||||
|
||||
public class ComplexClassTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
|
||||
ArrayList<String> strArrayList = new ArrayList<String>();
|
||||
strArrayList.add("abc");
|
||||
strArrayList.add("def");
|
||||
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
||||
ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
||||
ComplexClass cObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
||||
|
||||
ArrayList<String> strArrayListD = new ArrayList<String>();
|
||||
strArrayListD.add("lmn");
|
||||
strArrayListD.add("pqr");
|
||||
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45,67));
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject));
|
||||
Assert.assertTrue(bObject.equals(cObject));
|
||||
Assert.assertTrue(aObject.equals(cObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
|
||||
ArrayList<String> strArrayList = new ArrayList<String>();
|
||||
strArrayList.add("abc");
|
||||
strArrayList.add("def");
|
||||
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
|
||||
ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
|
||||
|
||||
ArrayList<String> strArrayListD = new ArrayList<String>();
|
||||
strArrayListD.add("lmn");
|
||||
strArrayListD.add("pqr");
|
||||
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-18
@@ -5,25 +5,20 @@ import org.junit.Test;
|
||||
|
||||
public class PrimitiveClassTest {
|
||||
|
||||
@Test
|
||||
public void testTwoEqualsObjects() {
|
||||
@Test
|
||||
public void testTwoEqualsObjects() {
|
||||
|
||||
PrimitiveClass aObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass cObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
||||
PrimitiveClass aObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject));
|
||||
Assert.assertTrue(bObject.equals(cObject));
|
||||
Assert.assertTrue(aObject.equals(cObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,26 +7,21 @@ import org.junit.Test;
|
||||
|
||||
public class SquareClassTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
|
||||
Square aObject = new Square(10, Color.BLUE);
|
||||
Square bObject = new Square(10, Color.BLUE);
|
||||
Square cObject = new Square(10, Color.BLUE);
|
||||
|
||||
Square dObject = new Square(20, Color.BLUE);
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject));
|
||||
Assert.assertTrue(bObject.equals(cObject));
|
||||
Assert.assertTrue(aObject.equals(cObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
|
||||
Square aObject = new Square(10, Color.BLUE);
|
||||
Square bObject = new Square(10, Color.BLUE);
|
||||
|
||||
Square dObject = new Square(20, Color.BLUE);
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
logs
|
||||
target
|
||||
/.idea
|
||||
/.idea_modules
|
||||
/.classpath
|
||||
/.project
|
||||
/.settings
|
||||
/RUNNING_PID
|
||||
@@ -0,0 +1,8 @@
|
||||
This software is licensed under the Apache 2 license, quoted below.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with
|
||||
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
||||
language governing permissions and limitations under the License.
|
||||
@@ -0,0 +1,49 @@
|
||||
This is your new Play application
|
||||
=================================
|
||||
|
||||
This file will be packaged with your application when using `activator dist`.
|
||||
|
||||
There are several demonstration files available in this template.
|
||||
|
||||
Controllers
|
||||
===========
|
||||
|
||||
- HomeController.java:
|
||||
|
||||
Shows how to handle simple HTTP requests.
|
||||
|
||||
- AsyncController.java:
|
||||
|
||||
Shows how to do asynchronous programming when handling a request.
|
||||
|
||||
- CountController.java:
|
||||
|
||||
Shows how to inject a component into a controller and use the component when
|
||||
handling requests.
|
||||
|
||||
Components
|
||||
==========
|
||||
|
||||
- Module.java:
|
||||
|
||||
Shows how to use Guice to bind all the components needed by your application.
|
||||
|
||||
- Counter.java:
|
||||
|
||||
An example of a component that contains state, in this case a simple counter.
|
||||
|
||||
- ApplicationTimer.java:
|
||||
|
||||
An example of a component that starts when the application starts and stops
|
||||
when the application stops.
|
||||
|
||||
Filters
|
||||
=======
|
||||
|
||||
- Filters.java:
|
||||
|
||||
Creates the list of HTTP filters used by your application.
|
||||
|
||||
- ExampleFilter.java
|
||||
|
||||
A simple filter that adds a header to every response.
|
||||
@@ -0,0 +1,46 @@
|
||||
import javax.inject.*;
|
||||
import play.*;
|
||||
import play.mvc.EssentialFilter;
|
||||
import play.http.HttpFilters;
|
||||
import play.mvc.*;
|
||||
|
||||
import filters.ExampleFilter;
|
||||
|
||||
/**
|
||||
* This class configures filters that run on every request. This
|
||||
* class is queried by Play to get a list of filters.
|
||||
*
|
||||
* Play will automatically use filters from any class called
|
||||
* <code>Filters</code> that is placed the root package. You can load filters
|
||||
* from a different class by adding a `play.http.filters` setting to
|
||||
* the <code>application.conf</code> configuration file.
|
||||
*/
|
||||
@Singleton
|
||||
public class Filters implements HttpFilters {
|
||||
|
||||
private final Environment env;
|
||||
private final EssentialFilter exampleFilter;
|
||||
|
||||
/**
|
||||
* @param env Basic environment settings for the current application.
|
||||
* @param exampleFilter A demonstration filter that adds a header to
|
||||
*/
|
||||
@Inject
|
||||
public Filters(Environment env, ExampleFilter exampleFilter) {
|
||||
this.env = env;
|
||||
this.exampleFilter = exampleFilter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EssentialFilter[] filters() {
|
||||
// Use the example filter if we're running development mode. If
|
||||
// we're running in production or test mode then don't use any
|
||||
// filters at all.
|
||||
if (env.mode().equals(Mode.DEV)) {
|
||||
return new EssentialFilter[] { exampleFilter };
|
||||
} else {
|
||||
return new EssentialFilter[] {};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import com.google.inject.AbstractModule;
|
||||
import java.time.Clock;
|
||||
|
||||
import services.ApplicationTimer;
|
||||
import services.AtomicCounter;
|
||||
import services.Counter;
|
||||
|
||||
/**
|
||||
* This class is a Guice module that tells Guice how to bind several
|
||||
* different types. This Guice module is created when the Play
|
||||
* application starts.
|
||||
*
|
||||
* Play will automatically use any class called `Module` that is in
|
||||
* the root package. You can create modules in other locations by
|
||||
* adding `play.modules.enabled` settings to the `application.conf`
|
||||
* configuration file.
|
||||
*/
|
||||
public class Module extends AbstractModule {
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
// Use the system clock as the default implementation of Clock
|
||||
bind(Clock.class).toInstance(Clock.systemDefaultZone());
|
||||
// Ask Guice to create an instance of ApplicationTimer when the
|
||||
// application starts.
|
||||
bind(ApplicationTimer.class).asEagerSingleton();
|
||||
// Set AtomicCounter as the implementation for Counter.
|
||||
bind(Counter.class).to(AtomicCounter.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package controllers;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import javax.inject.*;
|
||||
import play.*;
|
||||
import play.mvc.*;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import scala.concurrent.duration.Duration;
|
||||
import scala.concurrent.ExecutionContextExecutor;
|
||||
|
||||
/**
|
||||
* This controller contains an action that demonstrates how to write
|
||||
* simple asynchronous code in a controller. It uses a timer to
|
||||
* asynchronously delay sending a response for 1 second.
|
||||
*
|
||||
* @param actorSystem We need the {@link ActorSystem}'s
|
||||
* {@link Scheduler} to run code after a delay.
|
||||
* @param exec We need a Java {@link Executor} to apply the result
|
||||
* of the {@link CompletableFuture} and a Scala
|
||||
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
|
||||
* An {@link ExecutionContextExecutor} implements both interfaces.
|
||||
*/
|
||||
@Singleton
|
||||
public class AsyncController extends Controller {
|
||||
|
||||
private final ActorSystem actorSystem;
|
||||
private final ExecutionContextExecutor exec;
|
||||
|
||||
@Inject
|
||||
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
|
||||
this.actorSystem = actorSystem;
|
||||
this.exec = exec;
|
||||
}
|
||||
|
||||
/**
|
||||
* An action that returns a plain text message after a delay
|
||||
* of 1 second.
|
||||
*
|
||||
* The configuration in the <code>routes</code> file means that this method
|
||||
* will be called when the application receives a <code>GET</code> request with
|
||||
* a path of <code>/message</code>.
|
||||
*/
|
||||
public CompletionStage<Result> message() {
|
||||
return getFutureMessage(1, TimeUnit.SECONDS).thenApplyAsync(Results::ok, exec);
|
||||
}
|
||||
|
||||
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
actorSystem.scheduler().scheduleOnce(
|
||||
Duration.create(time, timeUnit),
|
||||
() -> future.complete("Hi!"),
|
||||
exec
|
||||
);
|
||||
return future;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package controllers;
|
||||
|
||||
import javax.inject.*;
|
||||
import play.*;
|
||||
import play.mvc.*;
|
||||
|
||||
import services.Counter;
|
||||
|
||||
/**
|
||||
* This controller demonstrates how to use dependency injection to
|
||||
* bind a component into a controller class. The class contains an
|
||||
* action that shows an incrementing count to users. The {@link Counter}
|
||||
* object is injected by the Guice dependency injection system.
|
||||
*/
|
||||
@Singleton
|
||||
public class CountController extends Controller {
|
||||
|
||||
private final Counter counter;
|
||||
|
||||
@Inject
|
||||
public CountController(Counter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
/**
|
||||
* An action that responds with the {@link Counter}'s current
|
||||
* count. The result is plain text. This action is mapped to
|
||||
* <code>GET</code> requests with a path of <code>/count</code>
|
||||
* requests by an entry in the <code>routes</code> config file.
|
||||
*/
|
||||
public Result count() {
|
||||
return ok(Integer.toString(counter.nextCount()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package controllers;
|
||||
|
||||
import play.mvc.*;
|
||||
|
||||
import views.html.*;
|
||||
|
||||
/**
|
||||
* This controller contains an action to handle HTTP requests
|
||||
* to the application's home page.
|
||||
*/
|
||||
public class HomeController extends Controller {
|
||||
|
||||
/**
|
||||
* An action that renders an HTML page with a welcome message.
|
||||
* The configuration in the <code>routes</code> file means that
|
||||
* this method will be called when the application receives a
|
||||
* <code>GET</code> request with a path of <code>/</code>.
|
||||
*/
|
||||
public Result index() {
|
||||
return ok(index.render("Your new application is ready."));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package controllers;
|
||||
import models.*;
|
||||
import util.*;
|
||||
import play.mvc.*;
|
||||
import play.libs.Json;
|
||||
import play.libs.Json.*;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.List;
|
||||
|
||||
public class StudentController extends Controller {
|
||||
public Result create() {
|
||||
JsonNode json = request().body().asJson();
|
||||
if(json == null)
|
||||
return badRequest(Util.createResponse("Expecting Json data",false));
|
||||
Student student=StudentStore.getInstance().addStudent((Student)Json.fromJson(json,Student.class));
|
||||
JsonNode jsonObject=Json.toJson(student);
|
||||
return created(Util.createResponse(jsonObject,true));
|
||||
}
|
||||
public Result update() {
|
||||
JsonNode json = request().body().asJson();
|
||||
if(json == null)
|
||||
return badRequest(Util.createResponse("Expecting Json data",false));
|
||||
Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class));
|
||||
if(student==null){
|
||||
return notFound(Util.createResponse("Student not found",false));
|
||||
}
|
||||
|
||||
JsonNode jsonObject=Json.toJson(student);
|
||||
return ok(Util.createResponse(jsonObject,true));
|
||||
}
|
||||
public Result retrieve(int id) {
|
||||
Student student=StudentStore.getInstance().getStudent(id);
|
||||
if(student==null){
|
||||
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
|
||||
}
|
||||
JsonNode jsonObjects=Json.toJson(student);
|
||||
return ok(Util.createResponse(jsonObjects,true));
|
||||
}
|
||||
public Result listStudents() {
|
||||
List<Student> result=StudentStore.getInstance().getAllStudents();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
JsonNode jsonData=mapper.convertValue(result, JsonNode.class);
|
||||
return ok(Util.createResponse(jsonData,true));
|
||||
|
||||
}
|
||||
public Result delete(int id) {
|
||||
boolean status=StudentStore.getInstance().deleteStudent(id);
|
||||
if(!status){
|
||||
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
|
||||
}
|
||||
return ok(Util.createResponse("Student with id:"+id+" deleted",true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package filters;
|
||||
|
||||
import akka.stream.Materializer;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Function;
|
||||
import javax.inject.*;
|
||||
import play.mvc.*;
|
||||
import play.mvc.Http.RequestHeader;
|
||||
|
||||
|
||||
/**
|
||||
* This is a simple filter that adds a header to all requests. It's
|
||||
* added to the application's list of filters by the
|
||||
* {@link Filters} class.
|
||||
*/
|
||||
@Singleton
|
||||
public class ExampleFilter extends Filter {
|
||||
|
||||
private final Executor exec;
|
||||
|
||||
/**
|
||||
* @param mat This object is needed to handle streaming of requests
|
||||
* and responses.
|
||||
* @param exec This class is needed to execute code asynchronously.
|
||||
* It is used below by the <code>thenAsyncApply</code> method.
|
||||
*/
|
||||
@Inject
|
||||
public ExampleFilter(Materializer mat, Executor exec) {
|
||||
super(mat);
|
||||
this.exec = exec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<Result> apply(
|
||||
Function<RequestHeader, CompletionStage<Result>> next,
|
||||
RequestHeader requestHeader) {
|
||||
|
||||
return next.apply(requestHeader).thenApplyAsync(
|
||||
result -> result.withHeader("X-ExampleFilter", "foo"),
|
||||
exec
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package models;
|
||||
public class Student {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
private int id;
|
||||
public Student(){}
|
||||
public Student(String firstName, String lastName, int age) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package models;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StudentStore {
|
||||
private static StudentStore instance;
|
||||
private Map<Integer, Student> students = new HashMap<>();
|
||||
|
||||
public static StudentStore getInstance() {
|
||||
if (instance == null)
|
||||
instance = new StudentStore();
|
||||
return instance;
|
||||
}
|
||||
|
||||
public Student addStudent(Student student) {
|
||||
int id = students.size() + 1;
|
||||
student.setId(id);
|
||||
students.put(id, student);
|
||||
return student;
|
||||
}
|
||||
|
||||
public Student getStudent(int id) {
|
||||
if (students.containsKey(id))
|
||||
return students.get(id);
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Student> getAllStudents() {
|
||||
return new ArrayList<Student>(students.values());
|
||||
}
|
||||
|
||||
public Student updateStudent(Student student) {
|
||||
int id=student.getId();
|
||||
if (students.containsKey(id)) {
|
||||
student.setId(id);
|
||||
students.put(id, student);
|
||||
return student;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean deleteStudent(int id) {
|
||||
|
||||
if (!students.containsKey(id))
|
||||
return false;
|
||||
students.remove(id);
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package services;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import javax.inject.*;
|
||||
import play.Logger;
|
||||
import play.inject.ApplicationLifecycle;
|
||||
|
||||
/**
|
||||
* This class demonstrates how to run code when the
|
||||
* application starts and stops. It starts a timer when the
|
||||
* application starts. When the application stops it prints out how
|
||||
* long the application was running for.
|
||||
*
|
||||
* This class is registered for Guice dependency injection in the
|
||||
* {@link Module} class. We want the class to start when the application
|
||||
* starts, so it is registered as an "eager singleton". See the code
|
||||
* in the {@link Module} class to see how this happens.
|
||||
*
|
||||
* This class needs to run code when the server stops. It uses the
|
||||
* application's {@link ApplicationLifecycle} to register a stop hook.
|
||||
*/
|
||||
@Singleton
|
||||
public class ApplicationTimer {
|
||||
|
||||
private final Clock clock;
|
||||
private final ApplicationLifecycle appLifecycle;
|
||||
private final Instant start;
|
||||
|
||||
@Inject
|
||||
public ApplicationTimer(Clock clock, ApplicationLifecycle appLifecycle) {
|
||||
this.clock = clock;
|
||||
this.appLifecycle = appLifecycle;
|
||||
// This code is called when the application starts.
|
||||
start = clock.instant();
|
||||
Logger.info("ApplicationTimer demo: Starting application at " + start);
|
||||
|
||||
// When the application starts, register a stop hook with the
|
||||
// ApplicationLifecycle object. The code inside the stop hook will
|
||||
// be run when the application stops.
|
||||
appLifecycle.addStopHook(() -> {
|
||||
Instant stop = clock.instant();
|
||||
Long runningTime = stop.getEpochSecond() - start.getEpochSecond();
|
||||
Logger.info("ApplicationTimer demo: Stopping application at " + clock.instant() + " after " + runningTime + "s.");
|
||||
return CompletableFuture.completedFuture(null);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package services;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.inject.*;
|
||||
|
||||
/**
|
||||
* This class is a concrete implementation of the {@link Counter} trait.
|
||||
* It is configured for Guice dependency injection in the {@link Module}
|
||||
* class.
|
||||
*
|
||||
* This class has a {@link Singleton} annotation because we need to make
|
||||
* sure we only use one counter per application. Without this
|
||||
* annotation we would get a new instance every time a {@link Counter} is
|
||||
* injected.
|
||||
*/
|
||||
@Singleton
|
||||
public class AtomicCounter implements Counter {
|
||||
|
||||
private final AtomicInteger atomicCounter = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public int nextCount() {
|
||||
return atomicCounter.getAndIncrement();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package services;
|
||||
|
||||
/**
|
||||
* This interface demonstrates how to create a component that is injected
|
||||
* into a controller. The interface represents a counter that returns a
|
||||
* incremented number each time it is called.
|
||||
*
|
||||
* The {@link Modules} class binds this interface to the
|
||||
* {@link AtomicCounter} implementation.
|
||||
*/
|
||||
public interface Counter {
|
||||
int nextCount();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package util;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import play.libs.Json;
|
||||
import play.libs.Json.*;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class Util{
|
||||
public static ObjectNode createResponse(Object response,boolean ok){
|
||||
ObjectNode result = Json.newObject();
|
||||
result.put("isSuccessfull", ok);
|
||||
if(response instanceof String)
|
||||
result.put("body",(String)response);
|
||||
else result.put("body",(JsonNode)response);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
@*
|
||||
* This template takes a single argument, a String containing a
|
||||
* message to display.
|
||||
*@
|
||||
@(message: String)
|
||||
|
||||
@*
|
||||
* Call the `main` template with two arguments. The first
|
||||
* argument is a `String` with the title of the page, the second
|
||||
* argument is an `Html` object containing the body of the page.
|
||||
*@
|
||||
@main("Welcome to Play") {
|
||||
|
||||
@*
|
||||
* Get an `Html` object by calling the built-in Play welcome
|
||||
* template and passing a `String` message.
|
||||
*@
|
||||
@play20.welcome(message, style = "Java")
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
@*
|
||||
* This template is called from the `index` template. This template
|
||||
* handles the rendering of the page header and body tags. It takes
|
||||
* two arguments, a `String` for the title of the page and an `Html`
|
||||
* object to insert into the body of the page.
|
||||
*@
|
||||
@(title: String)(content: Html)
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
@* Here's where we render the page title `String`. *@
|
||||
<title>@title</title>
|
||||
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
|
||||
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
|
||||
<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
|
||||
</head>
|
||||
<body>
|
||||
@* And here's where we render the `Html` object containing
|
||||
* the page content. *@
|
||||
@content
|
||||
</body>
|
||||
</html>
|
||||
+397
File diff suppressed because it is too large
Load Diff
+248
@@ -0,0 +1,248 @@
|
||||
@REM activator launcher script
|
||||
@REM
|
||||
@REM Environment:
|
||||
@REM In order for Activator to work you must have Java available on the classpath
|
||||
@REM JAVA_HOME - location of a JDK home dir (optional if java on path)
|
||||
@REM CFG_OPTS - JVM options (optional)
|
||||
@REM Configuration:
|
||||
@REM activatorconfig.txt found in the ACTIVATOR_HOME or ACTIVATOR_HOME/ACTIVATOR_VERSION
|
||||
@setlocal enabledelayedexpansion
|
||||
|
||||
@echo off
|
||||
|
||||
set "var1=%~1"
|
||||
if defined var1 (
|
||||
if "%var1%"=="help" (
|
||||
echo.
|
||||
echo Usage activator [options] [command]
|
||||
echo.
|
||||
echo Commands:
|
||||
echo ui Start the Activator UI
|
||||
echo new [name] [template-id] Create a new project with [name] using template [template-id]
|
||||
echo list-templates Print all available template names
|
||||
echo help Print this message
|
||||
echo.
|
||||
echo Options:
|
||||
echo -jvm-debug [port] Turn on JVM debugging, open at the given port. Defaults to 9999 if no port given.
|
||||
echo.
|
||||
echo Environment variables ^(read from context^):
|
||||
echo JAVA_OPTS Environment variable, if unset uses ""
|
||||
echo SBT_OPTS Environment variable, if unset uses ""
|
||||
echo ACTIVATOR_OPTS Environment variable, if unset uses ""
|
||||
echo.
|
||||
echo Please note that in order for Activator to work you must have Java available on the classpath
|
||||
echo.
|
||||
goto :end
|
||||
)
|
||||
)
|
||||
|
||||
@REM determine ACTIVATOR_HOME environment variable
|
||||
set BIN_DIRECTORY=%~dp0
|
||||
set BIN_DIRECTORY=%BIN_DIRECTORY:~0,-1%
|
||||
for %%d in (%BIN_DIRECTORY%) do set ACTIVATOR_HOME=%%~dpd
|
||||
set ACTIVATOR_HOME=%ACTIVATOR_HOME:~0,-1%
|
||||
|
||||
echo ACTIVATOR_HOME=%ACTIVATOR_HOME%
|
||||
|
||||
set ERROR_CODE=0
|
||||
set APP_VERSION=1.3.10
|
||||
set ACTIVATOR_LAUNCH_JAR=activator-launch-%APP_VERSION%.jar
|
||||
|
||||
rem Detect if we were double clicked, although theoretically A user could
|
||||
rem manually run cmd /c
|
||||
for %%x in (%cmdcmdline%) do if %%~x==/c set DOUBLECLICKED=1
|
||||
|
||||
set SBT_HOME=%BIN_DIRECTORY
|
||||
|
||||
rem Detect if we were double clicked, although theoretically A user could
|
||||
rem manually run cmd /c
|
||||
for %%x in (%cmdcmdline%) do if %%~x==/c set DOUBLECLICKED=1
|
||||
|
||||
rem FIRST we load the config file of extra options.
|
||||
set FN=%SBT_HOME%\..\conf\sbtconfig.txt
|
||||
set CFG_OPTS=
|
||||
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%FN%") DO (
|
||||
set DO_NOT_REUSE_ME=%%i
|
||||
rem ZOMG (Part #2) WE use !! here to delay the expansion of
|
||||
rem CFG_OPTS, otherwise it remains "" for this loop.
|
||||
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
|
||||
)
|
||||
|
||||
rem FIRST we load a config file of extra options (if there is one)
|
||||
set "CFG_FILE_HOME=%UserProfile%\.activator\activatorconfig.txt"
|
||||
set "CFG_FILE_VERSION=%UserProfile%\.activator\%APP_VERSION%\activatorconfig.txt"
|
||||
if exist %CFG_FILE_VERSION% (
|
||||
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE_VERSION%") DO (
|
||||
set DO_NOT_REUSE_ME=%%i
|
||||
rem ZOMG (Part #2) WE use !! here to delay the expansion of
|
||||
rem CFG_OPTS, otherwise it remains "" for this loop.
|
||||
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
|
||||
)
|
||||
)
|
||||
if "%CFG_OPTS%"=="" (
|
||||
if exist %CFG_FILE_HOME% (
|
||||
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE_HOME%") DO (
|
||||
set DO_NOT_REUSE_ME=%%i
|
||||
rem ZOMG (Part #2) WE use !! here to delay the expansion of
|
||||
rem CFG_OPTS, otherwise it remains "" for this loop.
|
||||
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
rem We use the value of the JAVACMD environment variable if defined
|
||||
set _JAVACMD=%JAVACMD%
|
||||
|
||||
if "%_JAVACMD%"=="" (
|
||||
if not "%JAVA_HOME%"=="" (
|
||||
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
|
||||
|
||||
rem if there is a java home set we make sure it is the first picked up when invoking 'java'
|
||||
SET "PATH=%JAVA_HOME%\bin;%PATH%"
|
||||
)
|
||||
)
|
||||
|
||||
if "%_JAVACMD%"=="" set _JAVACMD=java
|
||||
|
||||
rem Detect if this java is ok to use.
|
||||
for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do (
|
||||
if %%~j==java set JAVAINSTALLED=1
|
||||
if %%~j==openjdk set JAVAINSTALLED=1
|
||||
)
|
||||
|
||||
rem Detect the same thing about javac
|
||||
if "%_JAVACCMD%"=="" (
|
||||
if not "%JAVA_HOME%"=="" (
|
||||
if exist "%JAVA_HOME%\bin\javac.exe" set "_JAVACCMD=%JAVA_HOME%\bin\javac.exe"
|
||||
)
|
||||
)
|
||||
if "%_JAVACCMD%"=="" set _JAVACCMD=javac
|
||||
for /F %%j in ('"%_JAVACCMD%" -version 2^>^&1') do (
|
||||
if %%~j==javac set JAVACINSTALLED=1
|
||||
)
|
||||
|
||||
rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style
|
||||
set JAVAOK=true
|
||||
if not defined JAVAINSTALLED set JAVAOK=false
|
||||
if not defined JAVACINSTALLED set JAVAOK=false
|
||||
|
||||
if "%JAVAOK%"=="false" (
|
||||
echo.
|
||||
echo A Java JDK is not installed or can't be found.
|
||||
if not "%JAVA_HOME%"=="" (
|
||||
echo JAVA_HOME = "%JAVA_HOME%"
|
||||
)
|
||||
echo.
|
||||
echo Please go to
|
||||
echo http://www.oracle.com/technetwork/java/javase/downloads/index.html
|
||||
echo and download a valid Java JDK and install before running Activator.
|
||||
echo.
|
||||
echo If you think this message is in error, please check
|
||||
echo your environment variables to see if "java.exe" and "javac.exe" are
|
||||
echo available via JAVA_HOME or PATH.
|
||||
echo.
|
||||
if defined DOUBLECLICKED pause
|
||||
exit /B 1
|
||||
)
|
||||
|
||||
rem Check what Java version is being used to determine what memory options to use
|
||||
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
|
||||
set JAVA_VERSION=%%g
|
||||
)
|
||||
|
||||
rem Strips away the " characters
|
||||
set JAVA_VERSION=%JAVA_VERSION:"=%
|
||||
|
||||
rem TODO Check if there are existing mem settings in JAVA_OPTS/CFG_OPTS and use those instead of the below
|
||||
for /f "delims=. tokens=1-3" %%v in ("%JAVA_VERSION%") do (
|
||||
set MAJOR=%%v
|
||||
set MINOR=%%w
|
||||
set BUILD=%%x
|
||||
|
||||
set META_SIZE=-XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=256M
|
||||
if "!MINOR!" LSS "8" (
|
||||
set META_SIZE=-XX:PermSize=64M -XX:MaxPermSize=256M
|
||||
)
|
||||
|
||||
set MEM_OPTS=!META_SIZE!
|
||||
)
|
||||
|
||||
rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
|
||||
set _JAVA_OPTS=%JAVA_OPTS%
|
||||
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS%
|
||||
|
||||
set DEBUG_OPTS=
|
||||
|
||||
rem Loop through the arguments, building remaining args in args variable
|
||||
set args=
|
||||
:argsloop
|
||||
if not "%~1"=="" (
|
||||
rem Checks if the argument contains "-D" and if true, adds argument 1 with 2 and puts an equal sign between them.
|
||||
rem This is done since batch considers "=" to be a delimiter so we need to circumvent this behavior with a small hack.
|
||||
set arg1=%~1
|
||||
if "!arg1:~0,2!"=="-D" (
|
||||
set "args=%args% "%~1"="%~2""
|
||||
shift
|
||||
shift
|
||||
goto argsloop
|
||||
)
|
||||
|
||||
if "%~1"=="-jvm-debug" (
|
||||
if not "%~2"=="" (
|
||||
rem This piece of magic somehow checks that an argument is a number
|
||||
for /F "delims=0123456789" %%i in ("%~2") do (
|
||||
set var="%%i"
|
||||
)
|
||||
if defined var (
|
||||
rem Not a number, assume no argument given and default to 9999
|
||||
set JPDA_PORT=9999
|
||||
) else (
|
||||
rem Port was given, shift arguments
|
||||
set JPDA_PORT=%~2
|
||||
shift
|
||||
)
|
||||
) else (
|
||||
set JPDA_PORT=9999
|
||||
)
|
||||
shift
|
||||
|
||||
set DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=!JPDA_PORT!
|
||||
goto argsloop
|
||||
)
|
||||
rem else
|
||||
set "args=%args% "%~1""
|
||||
shift
|
||||
goto argsloop
|
||||
)
|
||||
|
||||
:run
|
||||
|
||||
if "!args!"=="" (
|
||||
if defined DOUBLECLICKED (
|
||||
set CMDS="ui"
|
||||
) else set CMDS=!args!
|
||||
) else set CMDS=!args!
|
||||
|
||||
rem We add a / in front, so we get file:///C: instead of file://C:
|
||||
rem Java considers the later a UNC path.
|
||||
rem We also attempt a solid effort at making it URI friendly.
|
||||
rem We don't even bother with UNC paths.
|
||||
set JAVA_FRIENDLY_HOME_1=/!ACTIVATOR_HOME:\=/!
|
||||
set JAVA_FRIENDLY_HOME=/!JAVA_FRIENDLY_HOME_1: =%%20!
|
||||
|
||||
rem Checks if the command contains spaces to know if it should be wrapped in quotes or not
|
||||
set NON_SPACED_CMD=%_JAVACMD: =%
|
||||
if "%_JAVACMD%"=="%NON_SPACED_CMD%" %_JAVACMD% %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS%
|
||||
if NOT "%_JAVACMD%"=="%NON_SPACED_CMD%" "%_JAVACMD%" %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS%
|
||||
|
||||
if ERRORLEVEL 1 goto error
|
||||
goto end
|
||||
|
||||
:error
|
||||
set ERROR_CODE=1
|
||||
|
||||
:end
|
||||
|
||||
@endlocal
|
||||
|
||||
exit /B %ERROR_CODE%
|
||||
@@ -0,0 +1,13 @@
|
||||
name := """student-api"""
|
||||
|
||||
version := "1.0-SNAPSHOT"
|
||||
|
||||
lazy val root = (project in file(".")).enablePlugins(PlayJava)
|
||||
|
||||
scalaVersion := "2.11.7"
|
||||
|
||||
libraryDependencies ++= Seq(
|
||||
javaJdbc,
|
||||
cache,
|
||||
javaWs
|
||||
)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user