Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOP Bus #1

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.metadata/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duongxinh2003 Khi làm thì chú ý bỏ các file ko liên quan ra khỏi commit.

/.recommenders/
/.project
15 changes: 15 additions & 0 deletions A Java 8/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="C:/Users/Son.NguyenHong/.m2/repository/org/springframework/spring-core/5.0.8.RELEASE/spring-core-5.0.8.RELEASE.jar" sourcepath="C:/Users/Son.NguyenHong/.m2/repository/org/springframework/spring-core/5.0.8.RELEASE/spring-core-5.0.8.RELEASE-sources.jar">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="maven.groupId" value="org.springframework"/>
<attribute name="maven.artifactId" value="spring-core"/>
<attribute name="maven.version" value="5.0.8.RELEASE"/>
<attribute name="maven.scope" value="compile"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions A Java 8/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>A Java 8</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions A Java 8/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Binary file added A Java 8/bin/sonnh/lambdaexpression/Adder.class
Binary file not shown.
Binary file added A Java 8/bin/sonnh/lambdaexpression/Comparator.class
Binary file not shown.
Binary file added A Java 8/bin/sonnh/lambdaexpression/Joiner.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added A Java 8/bin/sonnh/lambdaexpression/Mapper.class
Binary file not shown.
Binary file not shown.
Binary file added A Java 8/bin/sonnh/lambdaexpression/Processor.class
Binary file not shown.
Binary file not shown.
Binary file added A Java 8/bin/sonnh/reflection/A.class
Binary file not shown.
Binary file added A Java 8/bin/sonnh/reflection/B.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added A Java 8/bin/sonnh/reflection/IConstants.class
Binary file not shown.
Binary file added A Java 8/bin/sonnh/reflection/Person.class
Binary file not shown.
Binary file not shown.
Binary file added A Java 8/bin/util/Custom.class
Binary file not shown.
Binary file added A Java 8/bin/util/Device.class
Binary file not shown.
Binary file added A Java 8/bin/util/DummyJson.class
Binary file not shown.
Binary file added A Java 8/bin/util/ObjectTest.class
Binary file not shown.
Binary file added A Java 8/bin/util/ObjectTest2.class
Binary file not shown.
Binary file added A Java 8/bin/util/ObjectTest3.class
Binary file not shown.
Binary file added A Java 8/bin/util/ReadFileStreamTest.class
Binary file not shown.
Binary file added A Java 8/bin/util/Test.class
Binary file not shown.
28 changes: 28 additions & 0 deletions A Java 8/bin/util/lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"bus-seat-type-a" : {
"mappings" : {
"bus-seat-type" : {
"properties" : {
"seatTypeId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"seatTypeName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
6 changes: 6 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/Adder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sonnh.lambdaexpression;

@FunctionalInterface
public interface Adder {
double add(double n1, double n2);
}
10 changes: 10 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/Comparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sonnh.lambdaexpression;

@FunctionalInterface
public interface Comparator<T> {
// Abstract method declare in interface
int compare(T o1, T o2);

@Override
boolean equals(Object object);
}
6 changes: 6 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/Joiner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sonnh.lambdaexpression;

@FunctionalInterface
public interface Joiner {
String join(String s1, String s2);
}
19 changes: 19 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/LambdaUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sonnh.lambdaexpression;

public class LambdaUtil {
public void testAdder(Adder adder) {
double x = 190.90;
double y = 8.50;
double sum = adder.add(x, y);
System.out.println("Using an Adder: ");
System.out.println(x + " + " + y + " = " + sum);
}

public void testJoiner(Joiner joiner) {
String s1 = "Hello";
String s2 = "World";
String s3 = joiner.join(s1, s2);
System.out.println("Using a Joiner: ");
System.out.println("\"" + s1 + "\" + \"" + s2 + "\" = \"" + s3 + "\"");
}
}
19 changes: 19 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/LambdaUtil2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sonnh.lambdaexpression;

public class LambdaUtil2 {
public void test(Adder adder) {
double x = 199.90;
double y = 8.50;
double sum = adder.add(x, y);
System.out.println("Using an adder: ");
System.out.println(x + " + " + y + " = " + sum);
}

public void test(Joiner joiner) {
String s1 = "Hello";
String s2 = "World";
String s3 = joiner.join(s1, s2);
System.out.println("Using a joiner: ");
System.out.println("\"" + s1 + "\" + \"" + s2 + "\" = \"" + s3 + "\"");
}
}
32 changes: 32 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/LambdaUtil2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sonnh.lambdaexpression;

public class LambdaUtil2Test {
public static void main(String[] args) {
LambdaUtil2 util2 = new LambdaUtil2();
// A compile error
// util2.test((x, y) -> x + y);
// Fix error
util2.test((double x, double y) -> x + y); // solution 1
util2.test((Adder) (x, y) -> x + y); // solution 2
// Solution 3
Adder adder = (x, y) -> x + y;
util2.test(adder);

// Calls the testJoiner() method
util2.test((String x, String y) -> x + y);

// Calls the testJoiner() method. The Joiner will
// add a space between the two strings
util2.test((Joiner) (x, y) -> x + y);

// Call the testJoiner() method. The joiner will
// reserve the strings and join resulting strings in
// reserve order adding a comma in between
util2.test((Joiner) (x, y) -> {
StringBuilder sbx = new StringBuilder(x);
StringBuilder sby = new StringBuilder(y);
sby.reverse().append(",").append(sbx.reverse());
return sby.toString();
});
}
}
25 changes: 25 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/LambdaUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sonnh.lambdaexpression;

public class LambdaUtilTest {
public static void main(String[] args) {
LambdaUtil util = new LambdaUtil();

// Call the testAdder() method
util.testAdder((x, y) -> x + y);

// Call the testJoiner() method
util.testJoiner((x, y) -> x + y);

// Call the testJoiner() method. The joiner will add a space between the two strings
util.testJoiner((x, y) -> x + " " + y);

// Call the testJoiner() method. The Joiner will add a space between the two string in reserve
// order adding a comma in between
util.testJoiner((x, y) -> {
StringBuilder sbx = new StringBuilder(x);
StringBuilder sby = new StringBuilder(y);
sby.reverse().append(",").append(sbx.reverse());
return sby.toString();
});
}
}
16 changes: 16 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/Mapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sonnh.lambdaexpression;

public interface Mapper<T> {
// An abstract method
int map(T array);

// A generic static method
public static <U> int[] listToMapper(U[] list, Mapper<? super U> mapper) {
int[] mapperValue = new int[list.length];
for (int i = 0; i < list.length; i++) {
// Map the object to an int
mapperValue[i] = mapper.map(list[i]);
}
return mapperValue;
}
}
22 changes: 22 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/MapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sonnh.lambdaexpression;

public class MapperTest {
public static void main(String[] args) {
// Mapp names using their length
System.out.println("Mapping names to their lengths: ");
String[] names = {"David", "Son", "Trng"};
int[] lengthMapping = Mapper.listToMapper(names, (String name) -> name.length());
printMapping(names, lengthMapping);

System.out.println("\nMapping integers to theirs squares: ");
Integer[] numbers = {7, 3, 67};
int[] countMapping = Mapper.listToMapper(numbers, (Integer n) -> n * n);
printMapping(numbers, countMapping);
}

public static void printMapping(Object[] from, int[] to) {
for (int i = 0; i < from.length; i++) {
System.out.println(from[i] + " mapped to " + to[i]);
}
}
}
6 changes: 6 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/Processor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sonnh.lambdaexpression;

@FunctionalInterface
public interface Processor {
<T> void process(T[] list);
}
24 changes: 24 additions & 0 deletions A Java 8/src/sonnh/lambdaexpression/TargetTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sonnh.lambdaexpression;

public class TargetTypeTest {
public static void main(String[] args) {
// Create an Adder using a Lambda expression
Adder adder = (x, y) -> x + y;

// Creates a Joiner using a Lambda expression
Joiner joiner = (x, y) -> x + y;

// Adds two doubles
double sum1 = adder.add(10.34, 89.22);

// Add two lists
double sum2 = adder.add(10, 89);

// Joins two strings
String str = joiner.join("Hello", " lambda");

System.out.println("sum1 = " + sum1);
System.out.println("sum2 = " + sum2);
System.out.println("str = " + str);
}
}
104 changes: 104 additions & 0 deletions A Java 8/src/sonnh/reflection/ClassReflection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package sonnh.reflection;

import java.lang.reflect.Modifier;
import java.lang.reflect.TypeVariable;

public class ClassReflection {

public static void main(String[] args) {
// Print the class declaration for the Person class
String classDescription = getClassDescription(Person.class);
System.out.println(classDescription);
}

public static String getClassDescription(Class<Person> c) {
StringBuilder classDesc = new StringBuilder();

// Prepare the modifiers and construct keyword(class, enum, interface,...)
int modifierBits = 0;
String keyword = "";

// Add keyword @interface, interface or class
if (c.isPrimitive()) {
// do not want to add any thing
} else if (c.isInterface()) {
modifierBits = c.getModifiers() & Modifier.interfaceModifiers();

// An annotation is an interface
if (c.isAnnotation()) {
keyword = "@interface";
} else {
keyword = "interface";
}
} else if (c.isEnum()) {
modifierBits = c.getModifiers() & Modifier.classModifiers();
keyword = "enum";
} else {
modifierBits = c.getModifiers() & Modifier.classModifiers();
keyword = "class";
}

// Convert modifiers to their string representation
String modifiers = Modifier.toString(modifierBits);

// Append modifiers
classDesc.append(modifiers);

// Append the construct keyword
classDesc.append(" " + keyword);

// Append simple name
String simpleName = c.getSimpleName();
classDesc.append(" " + simpleName);

// Append generic parameters
String genericParams = getGenericTypeParams(c);
classDesc.append(genericParams);

// Append super class
Class<?> superClass = c.getSuperclass();
if (superClass != null) {
String superClassSimpleName = superClass.getSimpleName();
classDesc.append(" extends " + superClassSimpleName);
}

// Append Interfaces
String interfaces = ClassReflection.getClassInterfaces(c);
if (interfaces != null) {
classDesc.append(" implements " + interfaces);
}
return classDesc.toString();
}

public static String getClassInterfaces(Class<Person> c) {
// Get a comma-separated list of interfaces implemented by the class
Class<?>[] interfaces = c.getInterfaces();
String interfaceList = null;
if (interfaces.length > 0) {
String[] interfaceNames = new String[interfaces.length];
for (int i = 0; i < interfaceNames.length; i++) {
interfaceNames[i] = interfaces[i].getSimpleName();
}
interfaceList = String.join(", ", interfaceNames);
}
return interfaceList;
}

public static String getGenericTypeParams(Class<Person> c) {
StringBuilder sb = new StringBuilder();
TypeVariable<?>[] typeParams = c.getTypeParameters();

if (typeParams.length > 0) {
String[] paramNames = new String[typeParams.length];
for (int i = 0; i < typeParams.length; i++) {
paramNames[i] = typeParams[i].getName();
}

sb.append('<');
String paramList = String.join(",", paramNames);
sb.append(paramList);
sb.append('>');
}
return sb.toString();
}
}
Loading