Skip to content

Commit 3db1747

Browse files
committed
Removing compile warnings
1 parent 3e0098a commit 3db1747

File tree

19 files changed

+146
-129
lines changed

19 files changed

+146
-129
lines changed

generics/GenericVarargs.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import java.util.*;
33

44
public class GenericVarargs {
5+
@SuppressWarnings("unchecked")
56
public static <T> List<T> makeList(T... args) {
67
List<T> result = new ArrayList<>();
78
for(T item : args)

logging/CustomHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
public class CustomHandler {
77
private static Logger logger =
88
Logger.getLogger("CustomHandler");
9-
private static List strHolder = new ArrayList();
9+
private static List<String> trace = new ArrayList<>();
1010
public static void main(String[] args) {
1111
logger.addHandler(new Handler() {
1212
@Override
1313
public void publish(LogRecord logRecord) {
14-
strHolder.add(logRecord.getLevel() + ":");
15-
strHolder.add(logRecord.getSourceClassName()+":");
16-
strHolder.add(logRecord.getSourceMethodName()+":");
17-
strHolder.add("<" + logRecord.getMessage() + ">");
18-
strHolder.add("\n");
14+
trace.add(logRecord.getLevel() + ":");
15+
trace.add(logRecord.getSourceClassName()+":");
16+
trace.add(logRecord.getSourceMethodName()+":");
17+
trace.add("<" + logRecord.getMessage() + ">");
18+
trace.add("\n");
1919
}
2020
@Override
2121
public void flush() {}
@@ -24,6 +24,6 @@ public void close() {}
2424
});
2525
logger.warning("Logging Warning");
2626
logger.info("Logging Info");
27-
System.out.print(strHolder);
27+
System.out.print(trace);
2828
}
2929
} ///:~

patterns/CommandPattern.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ public void execute() {
2828

2929
// A Command object that holds commands:
3030
class Macro implements Command {
31-
private ArrayList commands = new ArrayList();
31+
private ArrayList<Command> commands =
32+
new ArrayList<>();
3233
public void add(Command c) { commands.add(c); }
3334
@Override
3435
public void execute() {
35-
Iterator it = commands.iterator();
36+
Iterator<Command> it = commands.iterator();
3637
while(it.hasNext())
37-
((Command)it.next()).execute();
38+
it.next().execute();
3839
}
3940
}
4041

patterns/PaperScissorsRock.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class Outcome {
77
private int value;
88
private Outcome(int val) { value = val; }
99
public final static Outcome
10-
WIN = new Outcome(0),
11-
LOSE = new Outcome(1),
10+
WIN = new Outcome(0),
11+
LOSE = new Outcome(1),
1212
DRAW = new Outcome(2);
1313
@Override
1414
public String toString() {
@@ -119,13 +119,12 @@ public static Outcome match(Item a, Item b) {
119119

120120
public class PaperScissorsRock {
121121
public static void main(String args[]) {
122-
ArrayList items = new ArrayList();
122+
ArrayList<Item> items = new ArrayList<>();
123123
for(int i = 0; i < 40; i++)
124124
items.add(ItemFactory.newItem());
125125
for(int i = 0; i < items.size()/2; i++)
126126
System.out.println(
127127
Compete.match(
128-
(Item)items.get(i),
129-
(Item)items.get(i*2)));
128+
items.get(i), items.get(i*2)));
130129
}
131130
} ///:~

patterns/ShapeFactory2.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ interface Shape {
1515

1616
abstract class ShapeFactory {
1717
protected abstract Shape create();
18-
static Map factories = new HashMap();
19-
static Shape createShape(String id)
18+
static Map<String, ShapeFactory> factories =
19+
new HashMap<>();
20+
static Shape createShape(String id)
2021
throws BadShapeCreation {
2122
if(!factories.containsKey(id)) {
2223
try {
@@ -28,23 +29,22 @@ static Shape createShape(String id)
2829
if(!factories.containsKey(id))
2930
throw new BadShapeCreation(id);
3031
}
31-
return
32-
((ShapeFactory)factories.get(id)).create();
32+
return factories.get(id).create();
3333
}
3434
}
3535

3636
class Circle implements Shape {
3737
private Circle() {}
38-
public void draw() {
39-
System.out.println("Circle.draw");
38+
public void draw() {
39+
System.out.println("Circle.draw");
4040
}
41-
public void erase() {
41+
public void erase() {
4242
System.out.println("Circle.erase");
4343
}
4444
static class Factory extends ShapeFactory {
4545
@Override
46-
protected Shape create() {
47-
return new Circle();
46+
protected Shape create() {
47+
return new Circle();
4848
}
4949
}
5050
static {
@@ -54,17 +54,17 @@ protected Shape create() {
5454
}
5555

5656
class Square implements Shape {
57-
private Square() {}
58-
public void draw() {
59-
System.out.println("Square.draw");
57+
private Square() {}
58+
public void draw() {
59+
System.out.println("Square.draw");
6060
}
61-
public void erase() {
62-
System.out.println("Square.erase");
61+
public void erase() {
62+
System.out.println("Square.erase");
6363
}
6464
static class Factory extends ShapeFactory {
6565
@Override
66-
protected Shape create() {
67-
return new Square();
66+
protected Shape create() {
67+
return new Square();
6868
}
6969
}
7070
static {
@@ -75,9 +75,9 @@ protected Shape create() {
7575

7676
public class ShapeFactory2 {
7777
public static void main(String args[]) {
78-
String shlist[] = { "Circle", "Square",
78+
String shlist[] = { "Circle", "Square",
7979
"Square", "Circle", "Circle", "Square" };
80-
ArrayList shapes = new ArrayList();
80+
ArrayList<Shape> shapes = new ArrayList<>();
8181
try {
8282
for (String shlist1 : shlist) {
8383
shapes.add(ShapeFactory.createShape(shlist1));
@@ -86,11 +86,11 @@ public static void main(String args[]) {
8686
e.printStackTrace();
8787
return;
8888
}
89-
Iterator i = shapes.iterator();
89+
Iterator<Shape> i = shapes.iterator();
9090
while(i.hasNext()) {
91-
Shape s = (Shape)i.next();
91+
Shape s = i.next();
9292
s.draw();
9393
s.erase();
9494
}
95-
}
95+
}
9696
} ///:~

patterns/doubledispatch/TypedBin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import java.util.*;
66

77
public abstract class TypedBin {
8-
ArrayList v = new ArrayList();
8+
ArrayList<Trash> v = new ArrayList<>();
99
protected boolean addIt(Trash t) {
1010
v.add(t);
1111
return true;
1212
}
13-
public Iterator elements() {
13+
public Iterator<Trash> elements() {
1414
return v.iterator();
1515
}
1616
public boolean add(DDAluminum a) {

patterns/dynatrash/DynaTrash.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//: patterns/dynatrash/DynaTrash.java
1+
//: patterns/dynatrash/DynaTrash.java
22
// Using a HashMap of ArrayLists and RTTI
33
// to automatically sort trash into
44
// vectors. This solution, despite the
@@ -8,43 +8,44 @@
88
import java.util.*;
99

1010
// Generic TypeMap works in any situation:
11-
class TypeMap {
12-
private HashMap t = new HashMap();
13-
public void add(Object o) {
11+
class TypeMap<T> {
12+
private Map<Class,List<T>> t = new HashMap<>();
13+
public void add(T o) {
1414
Class type = o.getClass();
1515
if(t.containsKey(type))
16-
((ArrayList)t.get(type)).add(o);
16+
t.get(type).add(o);
1717
else {
18-
ArrayList v = new ArrayList();
18+
List<T> v = new ArrayList<>();
1919
v.add(o);
2020
t.put(type,v);
2121
}
2222
}
23-
public ArrayList get(Class type) {
24-
return (ArrayList)t.get(type);
23+
public List<T> get(Class type) {
24+
return t.get(type);
2525
}
26-
public Iterator keys() {
27-
return t.keySet().iterator();
26+
public Iterator<Class> keys() {
27+
return t.keySet().iterator();
2828
}
2929
}
3030

3131
// Adapter class to allow
3232
// callbacks from ParseTrash.fillBin():
3333
class TypeMapAdapter implements Fillable {
34-
TypeMap map;
35-
public TypeMapAdapter(TypeMap tm) { map = tm; }
34+
TypeMap<Trash> map;
35+
public TypeMapAdapter(TypeMap<Trash> tm) {
36+
map = tm;
37+
}
3638
@Override
3739
public void addTrash(Trash t) { map.add(t); }
3840
}
3941

4042
public class DynaTrash {
4143
public static void main(String[] args) {
42-
TypeMap bin = new TypeMap();
43-
ParseTrash.fillBin("Trash.dat",
44+
TypeMap<Trash> bin = new TypeMap<>();
45+
ParseTrash.fillBin("Trash.dat",
4446
new TypeMapAdapter(bin));
45-
Iterator keys = bin.keys();
47+
Iterator<Class> keys = bin.keys();
4648
while(keys.hasNext())
47-
Trash.sumValue(
48-
bin.get((Class)keys.next()));
49+
Trash.sumValue(bin.get(keys.next()));
4950
}
5051
} ///:~

patterns/factory/ShapeFactory1.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BadShapeCreation extends Exception {
1212
abstract class Shape {
1313
public abstract void draw();
1414
public abstract void erase();
15-
static Shape factory(String type)
15+
static Shape factory(String type)
1616
throws BadShapeCreation {
1717
if("Circle".equals(type)) return new Circle();
1818
if("Square".equals(type)) return new Square();
@@ -23,32 +23,32 @@ static Shape factory(String type)
2323
class Circle extends Shape {
2424
Circle() {} // Friendly constructor
2525
@Override
26-
public void draw() {
27-
System.out.println("Circle.draw");
26+
public void draw() {
27+
System.out.println("Circle.draw");
2828
}
2929
@Override
30-
public void erase() {
31-
System.out.println("Circle.erase");
30+
public void erase() {
31+
System.out.println("Circle.erase");
3232
}
3333
}
3434

3535
class Square extends Shape {
3636
Square() {} // Friendly constructor
3737
@Override
38-
public void draw() {
39-
System.out.println("Square.draw");
38+
public void draw() {
39+
System.out.println("Square.draw");
4040
}
4141
@Override
42-
public void erase() {
43-
System.out.println("Square.erase");
42+
public void erase() {
43+
System.out.println("Square.erase");
4444
}
4545
}
4646

4747
public class ShapeFactory1 {
4848
public static void main(String args[]) {
49-
String shlist[] = { "Circle", "Square",
49+
String shlist[] = { "Circle", "Square",
5050
"Square", "Circle", "Circle", "Square" };
51-
ArrayList shapes = new ArrayList();
51+
List<Shape> shapes = new ArrayList<>();
5252
try {
5353
for (String shlist1 : shlist) {
5454
shapes.add(Shape.factory(shlist1));
@@ -57,11 +57,11 @@ public static void main(String args[]) {
5757
e.printStackTrace();
5858
return;
5959
}
60-
Iterator i = shapes.iterator();
60+
Iterator<Shape> i = shapes.iterator();
6161
while(i.hasNext()) {
62-
Shape s = (Shape)i.next();
62+
Shape s = i.next();
6363
s.draw();
6464
s.erase();
6565
}
66-
}
66+
}
6767
} ///:~

patterns/recyclea/RecycleA.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//: patterns/recyclea/RecycleA.java
1+
//: patterns/recyclea/RecycleA.java
22
// Recycling with RTTI.
33
package patterns.recyclea;
44
import java.util.*;
@@ -10,13 +10,13 @@ abstract class Trash {
1010
abstract double value();
1111
double weight() { return weight; }
1212
// Sums the value of Trash in a bin:
13-
static void sumValue(ArrayList bin) {
14-
Iterator e = bin.iterator();
13+
static void sumValue(List<Trash> bin) {
14+
Iterator<Trash> e = bin.iterator();
1515
double val = 0.0f;
1616
while(e.hasNext()) {
1717
// One kind of RTTI:
1818
// A dynamically-checked cast
19-
Trash t = (Trash)e.next();
19+
Trash t = e.next();
2020
// Polymorphism in action:
2121
val += t.weight() * t.value();
2222
System.out.println(
@@ -62,7 +62,7 @@ static void value(double newval) {
6262

6363
public class RecycleA {
6464
public static void main(String[] args) {
65-
ArrayList bin = new ArrayList();
65+
List<Trash> bin = new ArrayList<>();
6666
// Fill up the Trash bin:
6767
for(int i = 0; i < 30; i++)
6868
switch((int)(Math.random() * 3)) {
@@ -78,14 +78,14 @@ public static void main(String[] args) {
7878
bin.add(new
7979
Glass(Math.random() * 100));
8080
}
81-
ArrayList
82-
glassBin = new ArrayList(),
83-
paperBin = new ArrayList(),
84-
alBin = new ArrayList();
85-
Iterator sorter = bin.iterator();
81+
List<Trash>
82+
glassBin = new ArrayList<>(),
83+
paperBin = new ArrayList<>(),
84+
alBin = new ArrayList<>();
85+
Iterator<Trash> sorter = bin.iterator();
8686
// Sort the Trash:
8787
while(sorter.hasNext()) {
88-
Object t = sorter.next();
88+
Trash t = sorter.next();
8989
// RTTI to show class membership:
9090
if(t instanceof Aluminum)
9191
alBin.add(t);

patterns/trash/FillableList.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//: patterns/trash/FillableList.java
2+
// Adapter that makes an ArrayList Fillable.
3+
package patterns.trash;
4+
import java.util.*;
5+
6+
public class FillableList
7+
implements Fillable {
8+
private ArrayList v;
9+
public FillableList(ArrayList vv) { v = vv; }
10+
@Override
11+
public void addTrash(Trash t) { v.add(t); }
12+
} ///:~

0 commit comments

Comments
 (0)