Skip to content
Snippets Groups Projects
Commit 4564a98e authored by jez04's avatar jez04
Browse files

style: tune code style

parent 8888479b
No related merge requests found
package cz.vsb.fei.efrei.lab04; package cz.vsb.fei.efrei.lab04;
import java.util.Objects;
import java.util.Random; import java.util.Random;
public class Hero implements Fighter{ public class Hero implements Fighter {
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
...@@ -12,10 +11,8 @@ public class Hero implements Fighter{ ...@@ -12,10 +11,8 @@ public class Hero implements Fighter{
private int lives; private int lives;
public Hero(String name, int strenght) { public Hero(String name, int strenght) {
if(name == null || name.length() < 2 || if (name == null || name.length() < 2 || strenght < 30) {
strenght < 30) { throw new IllegalArgumentException("Sorry wrong arguments!!");
throw new IllegalArgumentException(
"Sorry wrong arguments!!");
} }
this.name = name; this.name = name;
this.strenght = strenght; this.strenght = strenght;
...@@ -27,9 +24,8 @@ public class Hero implements Fighter{ ...@@ -27,9 +24,8 @@ public class Hero implements Fighter{
} }
@Override @Override
public void attackedBy(Fighter fighter) public void attackedBy(Fighter fighter) throws AlreadyDeadException {
throws AlreadyDeadException { if (!isAlive()) {
if(!isAlive()) {
throw new AlreadyDeadException(this); throw new AlreadyDeadException(this);
} }
System.out.println(fighter.getName() + " attack " + getName() + "!"); System.out.println(fighter.getName() + " attack " + getName() + "!");
......
...@@ -5,27 +5,26 @@ import java.util.Collection; ...@@ -5,27 +5,26 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.Set; import java.util.Set;
public class WorldOfCollections { public class WorldOfCollections {
private static final Random RANDOM = new Random();
public static void main(String[] args) { public static void main(String[] args) {
useSetToStoreObjectWithoutHashCodeAndEquals(); useSetToStoreObjectWithoutHashCodeAndEquals();
useSetToStoreObjectWithEqualsOnlyForName(); useSetToStoreObjectWithEqualsOnlyForName();
useSetToStoreObjectWithEqualsAndHashCode(); useSetToStoreObjectWithEqualsAndHashCode();
useSetToStoreObjectWithEqualsBasedOnNameAndBadHashCode(); useSetToStoreObjectWithEqualsBasedOnNameAndBadHashCode();
useSetToStoreObjectWithEqualsAndConstantHashCode(); useSetToStoreObjectWithEqualsAndConstantHashCode();
System.out.println("=========================================================================================="); System.out
.println("==========================================================================================");
performanceForUseSetToStoreObjectWithEqualsAndHashCode(); performanceForUseSetToStoreObjectWithEqualsAndHashCode();
performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode(); performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode();
System.out.println("=========================================================================================="); System.out
.println("==========================================================================================");
performanceForAddIntoArrayList(); performanceForAddIntoArrayList();
performanceForAddIntoLinkedList(); performanceForAddIntoLinkedList();
performanceForInsertIntoArrayList(); performanceForInsertIntoArrayList();
performanceForInsertIntoLinkedList(); performanceForInsertIntoLinkedList();
} }
private static void testObjectAnHashSet(Set<Fighter> setOfFighters, Hero king1, Hero king2) { private static void testObjectAnHashSet(Set<Fighter> setOfFighters, Hero king1, Hero king2) {
...@@ -42,12 +41,12 @@ public class WorldOfCollections { ...@@ -42,12 +41,12 @@ public class WorldOfCollections {
Set<Fighter> setOfFighters = new HashSet<>(); Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = new Hero("King", 66); Hero king1 = new Hero("King", 66);
Hero king2 = new Hero("King", 66); Hero king2 = new Hero("King", 66);
System.out.println("=====Use HashSet to Store Object Without HashCode And Equals================================"); System.out.println(
"=====Use HashSet to Store Object Without HashCode And Equals================================");
testObjectAnHashSet(setOfFighters, king1, king2); testObjectAnHashSet(setOfFighters, king1, king2);
System.out.println("Each object is different (different palces in memory) even look same!"); System.out.println("Each object is different (different palces in memory) even look same!");
} }
public static void useSetToStoreObjectWithEqualsOnlyForName() { public static void useSetToStoreObjectWithEqualsOnlyForName() {
Set<Fighter> setOfFighters = new HashSet<>(); Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = new HeroWithEquals("King", 66); Hero king1 = new HeroWithEquals("King", 66);
...@@ -70,21 +69,24 @@ public class WorldOfCollections { ...@@ -70,21 +69,24 @@ public class WorldOfCollections {
Set<Fighter> setOfFighters = new HashSet<>(); Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = new HeroWithEqualsAndBadHashcode("King", 66); Hero king1 = new HeroWithEqualsAndBadHashcode("King", 66);
Hero king2 = new HeroWithEqualsAndBadHashcode("King", 42); Hero king2 = new HeroWithEqualsAndBadHashcode("King", 42);
System.out.println("=====Use HashSet to Store Object With Equals and BAD hashcode================================"); System.out.println(
"=====Use HashSet to Store Object With Equals and BAD hashcode================================");
testObjectAnHashSet(setOfFighters, king1, king2); testObjectAnHashSet(setOfFighters, king1, king2);
System.out.println("It does'n work!"); System.out.println("It does'n work!");
System.out.println("Object are equals because of same name, but not found in set because of worng hashcode calculated from strenght!"); System.out.println(
"Object are equals because of same name, but not found in set because of worng hashcode calculated from strenght!");
} }
public static void useSetToStoreObjectWithEqualsAndConstantHashCode() { public static void useSetToStoreObjectWithEqualsAndConstantHashCode() {
Set<Fighter> setOfFighters = new HashSet<>(); Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = new HeroWithEqualsAndConstantHashcode("King", 66); Hero king1 = new HeroWithEqualsAndConstantHashcode("King", 66);
Hero king2 = new HeroWithEqualsAndConstantHashcode("King", 66); Hero king2 = new HeroWithEqualsAndConstantHashcode("King", 66);
System.out.println("=====Use HashSet to Store Object With Equals and constant hashcode================================"); System.out.println(
"=====Use HashSet to Store Object With Equals and constant hashcode================================");
testObjectAnHashSet(setOfFighters, king1, king2); testObjectAnHashSet(setOfFighters, king1, king2);
System.out.println("It's work!"); System.out.println("It's work!");
} }
private static String mesureAndAdd(Collection<Hero> sourceOfHeros, Collection<Hero> tragetOfHeros) { private static String mesureAndAdd(Collection<Hero> sourceOfHeros, Collection<Hero> tragetOfHeros) {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.start(); stopwatch.start();
...@@ -115,10 +117,10 @@ public class WorldOfCollections { ...@@ -115,10 +117,10 @@ public class WorldOfCollections {
} }
Set<Hero> setOfHeros = new HashSet<>(); Set<Hero> setOfHeros = new HashSet<>();
String duration = mesureAndAdd(heros, setOfHeros); String duration = mesureAndAdd(heros, setOfHeros);
System.out.println("Adding " + countOfHeros + " heros with correct hashcode and equals into set take " + duration); System.out.println(
"Adding " + countOfHeros + " heros with correct hashcode and equals into set take " + duration);
} }
public static void performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode() { public static void performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode() {
int countOfHeros = 20000; int countOfHeros = 20000;
List<Hero> heros = new ArrayList<>(countOfHeros); List<Hero> heros = new ArrayList<>(countOfHeros);
...@@ -130,7 +132,6 @@ public class WorldOfCollections { ...@@ -130,7 +132,6 @@ public class WorldOfCollections {
System.out.println("Adding " + countOfHeros + " heros with constant hashcode into set take " + duration); System.out.println("Adding " + countOfHeros + " heros with constant hashcode into set take " + duration);
} }
public static void performanceForAddIntoArrayList() { public static void performanceForAddIntoArrayList() {
int countOfHeros = 500000; int countOfHeros = 500000;
List<Hero> heros = new ArrayList<>(countOfHeros); List<Hero> heros = new ArrayList<>(countOfHeros);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment