Skip to content

Commit

Permalink
study utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Mar 3, 2021
1 parent be2b69b commit 4cfd20f
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 3 deletions.
16 changes: 16 additions & 0 deletions core-banking/src/com/example/banking/domain/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ public Optional<Account> getAccount(String iban) {
return Optional.empty();
}

public double getTotalBalance() {
var total = 0;
for (var account : accounts) {
total += account.getBalance();
}
return total;
}

public double getTotalBalance8() {
return accounts.stream()
.parallel()
//.mapToDouble(account -> account.getBalance())
.mapToDouble(Account::getBalance)
.sum(); // terminal method
}

public int getNumberOfAccounts() {
return accounts.size();
}
Expand Down
15 changes: 14 additions & 1 deletion part1/src/com/example/exercises/Exercise2.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.example.exercises;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.example.dao.CountryDao;
import com.example.dao.InMemoryWorldDao;
import com.example.domain.Country;

/**
*
Expand All @@ -12,7 +16,16 @@ public class Exercise2 {
private static final CountryDao countryDao = InMemoryWorldDao.getInstance();

public static void main(String[] args) {
// Find the most populated city of each continent
var countries = countryDao.findAllCountries();
var stream = countries.stream();
if (countries.size()>1_000)
stream = stream.parallel();
var continents = stream
.map(Country::getContinent)
.distinct()
.sorted((c1,c2)->c2.compareTo(c1))
.collect(Collectors.toList()); // terminal
System.out.println(continents);
}

}
5 changes: 5 additions & 0 deletions part2/src/com/example/animals/domain/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public void eat() {
System.out.println(String.format("%s is eating now...", name));
}

@Override
public String toString() {
return "Cat [name=" + name + ", legs=" + getLegs() + "]";
}

}
5 changes: 5 additions & 0 deletions part2/src/com/example/animals/domain/Fish.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public void eat() {
public void walk() {
System.out.println(String.format("%s is swimming now...", name));
}

@Override
public String toString() {
return "Fish [name=" + name + ", legs= "+getLegs()+" ]";
}

}
6 changes: 6 additions & 0 deletions part2/src/com/example/animals/domain/Spider.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public void eat() {
System.out.println("Spider is eating now...");
}

@Override
public String toString() {
return "Spider [legs=" + getLegs() + "]";
}


}
7 changes: 7 additions & 0 deletions part2/src/com/example/animals/exercises/Exercise3.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.animals.exercises;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.example.animals.domain.Animal;
Expand All @@ -18,6 +19,12 @@ public static void main(String[] args) {
// Find the animal with the highest number of legs
List<Animal> animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
new Spider(), new Fish("Jaws"));
animals.stream()
.max(Comparator.comparing(Animal::getLegs))
.ifPresent(System.out::println);
animals.stream()
.min(Comparator.comparing(Animal::getLegs))
.ifPresent(System.out::println);

}
}
3 changes: 2 additions & 1 deletion part2/src/com/example/animals/exercises/Exercise5.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static void main(String[] args) {
// Find the total number of legs
List<Animal> animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
new Spider(), new Fish("Jaws"));

var totalLegs = animals.stream().mapToLong(Animal::getLegs).sum();
System.out.println("Total legs: "+totalLegs);
}
}
9 changes: 8 additions & 1 deletion part2/src/com/example/animals/exercises/Exercise6.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.example.animals.domain.Animal;
import com.example.animals.domain.Cat;
Expand All @@ -18,6 +19,12 @@ public static void main(String[] args) {
// Group the animals by their number of legs
List<Animal> animals = Arrays.asList(new Cat(), new Spider(), new Cat("Tekir"), new Fish("Free Willy"),
new Spider(), new Fish("Jaws"));

var animalsByLegs =
animals.stream()
.collect(Collectors.groupingBy(Animal::getLegs));
animalsByLegs.forEach((legs,groupedAnimals)->{
System.out.println("Group: "+legs);
groupedAnimals.forEach(System.out::println);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
encoding//src/com/example/StudyCurrency.java=UTF-8
encoding//src/com/example/StudyDateTimeFormatter.java=UTF-8
encoding//src/com/example/StudyString.java=UTF-8
encoding//src/com/example/StudyStringComparison.java=UTF-8
1 change: 1 addition & 0 deletions study-utility-classes/bin/messages_en_US.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greeting=Hi {0}!
1 change: 1 addition & 0 deletions study-utility-classes/bin/messages_tr_TR.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greeting=Merhaba, {0}!
19 changes: 19 additions & 0 deletions study-utility-classes/src/com/example/StudyCurrency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class StudyCurrency {

public static void main(String[] args) {
var money = 12_345_678.9;
Locale locale = Locale.JAPAN; // new Locale("tr", "TR");
var df = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
//var dfs = DecimalFormatSymbols.getInstance(locale);
//dfs.setCurrencySymbol("\u20BA");
//df.setDecimalFormatSymbols(dfs);
System.out.println(df.format(money));
}

}
18 changes: 18 additions & 0 deletions study-utility-classes/src/com/example/StudyDateTimeFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class StudyDateTimeFormatter {

public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now();
Locale tr = Locale.JAPAN; // new Locale("tr", "TR");
var dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL)
.withLocale(tr);
System.out.println(dtf.format(now));
}

}
17 changes: 17 additions & 0 deletions study-utility-classes/src/com/example/StudyDynamicString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

public class StudyDynamicString {

public static void main(String[] args) {
// StringBuffer: TS, StringBuilder: NOT TS
var s = new StringBuffer(488891);
var start = System.currentTimeMillis();
for (var i=0;i<2_000_000;++i) {
s.append(i); // dynamic string
}
var stop = System.currentTimeMillis();
System.out.println("s.length(): "+s.length()
+ ", duration: " + (stop-start) + " ms.");
}

}
18 changes: 18 additions & 0 deletions study-utility-classes/src/com/example/StudyI18N.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class StudyI18N {

public static void main(String[] args) {
Locale tr = Locale.US; // new Locale("tr", "TR");
var bundle = ResourceBundle.getBundle("messages", tr);
String fullname= "Jack Bauer";
var formatter = new MessageFormat(bundle.getString("greeting"),tr);
System.out.println(formatter.format(new Object[] {fullname}));

}

}
21 changes: 21 additions & 0 deletions study-utility-classes/src/com/example/StudyString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

import java.util.Locale;

public class StudyString {

public static void main(String[] args) {
// String: immutable class -> object caching/pooling
String name = "jack";
System.out.println(name);
name = name.toUpperCase();
System.out.println(name);
var city = "izmir";
// Internationalization: i18n
var tr = new Locale("tr","TR");
System.out.println(city);
city = city.toUpperCase(tr); // supports i18n
System.out.println(city);
}

}
38 changes: 38 additions & 0 deletions study-utility-classes/src/com/example/StudyStringComparison.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example;

import java.text.Collator;
import java.text.ParseException;
import java.text.RuleBasedCollator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class StudyStringComparison {

public static void main(String[] args) throws ParseException {
List<String> names = new ArrayList<>();
names.add("şule");
names.add("şima");
names.add("sema");
names.add("zehra");
names.add("ayşegül");
names.sort(String::compareTo);
System.out.println(names);
String name1 = "müller";
String name2 = "mueller";
System.out.println("name1.equals(name2): "+name1.equals(name2));
// Collation
String basicRules= "< z < b < c < ç < d < e < f < g < ğ < h "+
"< ı < i < j < k < l < m < n < o < ö < p "+
"< r < s < ş < t < u < ü < v < y < a ";
String trExpension= "& ü ; ue & s ; ş" ;
final Collator collator= new RuleBasedCollator(basicRules + trExpension);
Collator.getInstance(new Locale("tr","TR"));
collator.setStrength(Collator.PRIMARY);
System.out.println("name1.equals(name2): "+collator.equals(name1,name2));
System.out.println("sima==şima: "+collator.equals("sima","şima"));
names.sort(collator::compare);
System.out.println(names);
}

}
1 change: 1 addition & 0 deletions study-utility-classes/src/messages_en_US.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greeting=Hi {0}!
1 change: 1 addition & 0 deletions study-utility-classes/src/messages_tr_TR.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greeting=Merhaba, {0}!

0 comments on commit 4cfd20f

Please sign in to comment.