Skip to content

Commit 203ede2

Browse files
committedNov 12, 2015
java demo
0 parents  commit 203ede2

13 files changed

+200
-0
lines changed
 

Diff for: ‎.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

Diff for: ‎.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaPractices</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

Diff for: ‎.settings/org.eclipse.jdt.core.prefs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.7
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.7

Diff for: ‎.settings/org.eclipse.ltk.core.refactoring.prefs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false

Diff for: ‎bin/net/juude/java/generics/CollectionUtils$1.class

908 Bytes
Binary file not shown.

Diff for: ‎bin/net/juude/java/generics/CollectionUtils.class

2.67 KB
Binary file not shown.

Diff for: ‎bin/net/juude/java/generics/Generics.class

1.83 KB
Binary file not shown.

Diff for: ‎bin/net/juude/java/generics/Predicate.class

260 Bytes
Binary file not shown.

Diff for: ‎bin/net/juude/java/math/Angle.class

1.78 KB
Binary file not shown.

Diff for: ‎src/net/juude/java/generics/CollectionUtils.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package net.juude.java.generics;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
7+
public class CollectionUtils {
8+
9+
@SuppressWarnings("unchecked")
10+
public static <T> Collection<T> filter(Collection<T> collection, Predicate<T> predicate) {
11+
if(collection == null || predicate == null) {
12+
return null;
13+
}
14+
Collection<T> resultCollections = null;
15+
try {
16+
resultCollections = collection.getClass().newInstance();
17+
} catch (InstantiationException e) {
18+
e.printStackTrace();
19+
return null;
20+
} catch (IllegalAccessException e) {
21+
e.printStackTrace();
22+
return null;
23+
}
24+
for(T item : collection) {
25+
if(predicate.apply(item)) {
26+
resultCollections.add(item);
27+
}
28+
}
29+
return resultCollections;
30+
}
31+
32+
public static void main(String[] args) {
33+
ArrayList<String> strings= new ArrayList<String>();
34+
strings.add("a");
35+
strings.add("b");
36+
strings.add("abd");
37+
strings.add("c");
38+
strings.add("jlsf");
39+
strings.add("f");
40+
strings.add(null);
41+
strings.add("null");
42+
Collection<String> s = filter(strings, new Predicate<String>() {
43+
@Override
44+
public boolean apply(String input) {
45+
return input != null && input.length() == 1;
46+
}
47+
});
48+
System.out.println(strings);
49+
System.out.println(s + " " + s.getClass());
50+
}
51+
}

Diff for: ‎src/net/juude/java/generics/Generics.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package net.juude.java.generics;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Why generics
9+
* 1. type cast
10+
* 2. type check
11+
* 1. bound
12+
* 2.
13+
* 3.
14+
* */
15+
public class Generics<T> {
16+
private T mValue;
17+
18+
public Generics() {
19+
}
20+
21+
public void setValue(T value) {
22+
mValue = value;
23+
}
24+
25+
public T getValue() {
26+
return mValue;
27+
}
28+
29+
public static void main(String[] args) {
30+
31+
//int value
32+
Generics<Integer> intValue = new Generics<Integer>();
33+
intValue.setValue(100);
34+
System.out.println("intValue : " + intValue.getValue());
35+
36+
//String this will not compile
37+
//intValue.setValue("ff");
38+
39+
//bounds
40+
Generics<? extends Number> numValue = new Generics<Integer>(); //ok
41+
//Generics<? extends Number> numValue2 = new Generics<String>();//error
42+
43+
}
44+
45+
private void testLinkedHashMap() {
46+
LinkedHashMap map = new LinkedHashMap();
47+
map.put(1, "fsf");
48+
map.put("sdf", "");
49+
}
50+
}

Diff for: ‎src/net/juude/java/generics/Predicate.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.juude.java.generics;
2+
3+
/**
4+
* Determines a true or false value for a given input.
5+
*
6+
* <p>The {@link Predicates} class provides common predicates and related utilities.
7+
*
8+
* <p>See the Guava User Guide article on <a href=
9+
* "http://code.google.com/p/guava-libraries/wiki/FunctionalExplained">the use of {@code
10+
* Predicate}</a>.
11+
*
12+
* @author Kevin Bourrillion
13+
* @since 2.0
14+
*/
15+
public interface Predicate<T> {
16+
17+
boolean apply( T input);
18+
19+
@Override
20+
boolean equals(Object object);
21+
}

Diff for: ‎src/net/juude/java/math/Angle.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.juude.java.math;
2+
3+
public class Angle {
4+
5+
public static void main(String[] args) {
6+
float[] startPoint = new float[]{-1, -1};
7+
float[] endPoint = new float[] {-2, -2};
8+
System.out.println(" angle" + getAngle(startPoint, endPoint));
9+
10+
float[] startPoint2 = new float[]{0, 0};
11+
float[] endPoint2 = new float[] {0.8660254037844386f, 0.5f};
12+
System.out.println(" angle2 : " + getAngle(startPoint2, endPoint2));
13+
14+
15+
System.out.println(" differnece of 1 and 359 is : " + getAngleDifference(1, 359));
16+
System.out.println(" differnece of 359 and 1 is : " + getAngleDifference(359, 1));
17+
System.out.println(" differnece of 30 and 180 is : " + getAngleDifference(30, 180));
18+
19+
}
20+
21+
//get angle of the line
22+
public static double getAngle(float[] startAngle, float[] endAngle) {
23+
float distance = (float) Math.sqrt((endAngle[0] - startAngle[0]) * (endAngle[0] - startAngle[0]) + (endAngle[1] - startAngle[1]) * (endAngle[1] - startAngle[1]));
24+
if(distance == 0) {
25+
return 0;
26+
}
27+
float sinx = (endAngle[1] - startAngle[1])/distance;
28+
float cosx = (endAngle[0] - startAngle[0])/distance;
29+
return Math.atan2(sinx, cosx)/Math.PI*180;
30+
}
31+
32+
public static float getAngleDifference(float firstAngle, float secondAngle) {
33+
float difference = secondAngle - firstAngle;
34+
if(difference > 180) {
35+
difference -= 360;
36+
}else if(difference < -180) {
37+
difference += 360;
38+
}
39+
return difference;
40+
}
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.