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

style: tune code style

parent 8888479b
Branches
No related merge requests found
package cz.vsb.fei.efrei.lab04;
import java.util.Objects;
import java.util.Random;
public class Hero implements Fighter{
public class Hero implements Fighter {
private static final Random RANDOM = new Random();
......@@ -12,10 +11,8 @@ public class Hero implements Fighter{
private int lives;
public Hero(String name, int strenght) {
if(name == null || name.length() < 2 ||
strenght < 30) {
throw new IllegalArgumentException(
"Sorry wrong arguments!!");
if (name == null || name.length() < 2 || strenght < 30) {
throw new IllegalArgumentException("Sorry wrong arguments!!");
}
this.name = name;
this.strenght = strenght;
......@@ -27,9 +24,8 @@ public class Hero implements Fighter{
}
@Override
public void attackedBy(Fighter fighter)
throws AlreadyDeadException {
if(!isAlive()) {
public void attackedBy(Fighter fighter) throws AlreadyDeadException {
if (!isAlive()) {
throw new AlreadyDeadException(this);
}
System.out.println(fighter.getName() + " attack " + getName() + "!");
......
......@@ -5,27 +5,26 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class WorldOfCollections {
private static final Random RANDOM = new Random();
public static void main(String[] args) {
useSetToStoreObjectWithoutHashCodeAndEquals();
useSetToStoreObjectWithEqualsOnlyForName();
useSetToStoreObjectWithEqualsAndHashCode();
useSetToStoreObjectWithEqualsBasedOnNameAndBadHashCode();
useSetToStoreObjectWithEqualsAndConstantHashCode();
System.out.println("==========================================================================================");
System.out
.println("==========================================================================================");
performanceForUseSetToStoreObjectWithEqualsAndHashCode();
performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode();
System.out.println("==========================================================================================");
System.out
.println("==========================================================================================");
performanceForAddIntoArrayList();
performanceForAddIntoLinkedList();
performanceForInsertIntoArrayList();
performanceForInsertIntoLinkedList();
}
private static void testObjectAnHashSet(Set<Fighter> setOfFighters, Hero king1, Hero king2) {
......@@ -42,12 +41,12 @@ public class WorldOfCollections {
Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = 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);
System.out.println("Each object is different (different palces in memory) even look same!");
}
public static void useSetToStoreObjectWithEqualsOnlyForName() {
Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = new HeroWithEquals("King", 66);
......@@ -70,21 +69,24 @@ public class WorldOfCollections {
Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = new HeroWithEqualsAndBadHashcode("King", 66);
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);
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() {
Set<Fighter> setOfFighters = new HashSet<>();
Hero king1 = 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);
System.out.println("It's work!");
}
private static String mesureAndAdd(Collection<Hero> sourceOfHeros, Collection<Hero> tragetOfHeros) {
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
......@@ -115,10 +117,10 @@ public class WorldOfCollections {
}
Set<Hero> setOfHeros = new HashSet<>();
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() {
int countOfHeros = 20000;
List<Hero> heros = new ArrayList<>(countOfHeros);
......@@ -130,7 +132,6 @@ public class WorldOfCollections {
System.out.println("Adding " + countOfHeros + " heros with constant hashcode into set take " + duration);
}
public static void performanceForAddIntoArrayList() {
int countOfHeros = 500000;
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