Skip to content

Commit a0a9ae8

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents a1120ec + 0cb1447 commit a0a9ae8

12 files changed

Lines changed: 658 additions & 64 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.casting;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Animal {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(Animal.class);
8+
9+
public void eat() {
10+
LOGGER.info("animal is eating");
11+
}
12+
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.casting;
2+
3+
import java.util.List;
4+
5+
public class AnimalFeeder {
6+
7+
public void feed(List<Animal> animals) {
8+
animals.forEach(animal -> {
9+
animal.eat();
10+
if (animal instanceof Cat) {
11+
((Cat) animal).meow();
12+
}
13+
});
14+
}
15+
16+
public void uncheckedFeed(List<Animal> animals) {
17+
animals.forEach(animal -> {
18+
animal.eat();
19+
((Cat) animal).meow();
20+
});
21+
}
22+
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.casting;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Cat extends Animal implements Mew {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(Cat.class);
8+
9+
public void eat() {
10+
LOGGER.info("cat is eating");
11+
}
12+
13+
public void meow() {
14+
LOGGER.info("meow");
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.casting;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Dog extends Animal {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(Dog.class);
8+
9+
public void eat() {
10+
LOGGER.info("dog is eating");
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.casting;
2+
3+
public interface Mew {
4+
public void meow();
5+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
package com.baeldung.java.list;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import java.util.ListIterator;
8+
9+
public class CustomList<E> implements List<E> {
10+
private Object[] internal = {};
11+
12+
@Override
13+
public void add(int index, E element) {
14+
throw new UnsupportedOperationException();
15+
}
16+
17+
@Override
18+
public boolean addAll(Collection<? extends E> collection) {
19+
throw new UnsupportedOperationException();
20+
}
21+
22+
@Override
23+
public boolean addAll(int index, Collection<? extends E> collection) {
24+
throw new UnsupportedOperationException();
25+
}
26+
27+
@Override
28+
public E remove(int index) {
29+
throw new UnsupportedOperationException();
30+
}
31+
32+
@Override
33+
public boolean remove(Object object) {
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
@Override
38+
public boolean removeAll(Collection<?> collection) {
39+
throw new UnsupportedOperationException();
40+
}
41+
42+
@Override
43+
public boolean retainAll(Collection<?> collection) {
44+
throw new UnsupportedOperationException();
45+
}
46+
47+
@Override
48+
public int size() {
49+
return internal.length;
50+
}
51+
52+
@Override
53+
public boolean isEmpty() {
54+
return internal.length == 0;
55+
}
56+
57+
@Override
58+
public boolean add(E element) {
59+
// the first cycle
60+
// internal = new Object[1];
61+
// internal[0] = element;
62+
// return true;
63+
64+
Object[] temp = new Object[internal.length + 1];
65+
System.arraycopy(internal, 0, temp, 0, internal.length);
66+
temp[internal.length] = element;
67+
internal = temp;
68+
return true;
69+
}
70+
71+
@SuppressWarnings("unchecked")
72+
@Override
73+
public E get(int index) {
74+
return (E) internal[index];
75+
}
76+
77+
@Override
78+
public boolean contains(Object object) {
79+
// return false
80+
81+
for (Object element : internal) {
82+
if (object.equals(element)) {
83+
return true;
84+
}
85+
}
86+
return false;
87+
}
88+
89+
@Override
90+
public boolean containsAll(Collection<?> collection) {
91+
// the first cycle
92+
// for (Object element : collection) {
93+
// if (element.equals(internal[0])) {
94+
// return true;
95+
// }
96+
// }
97+
// return false;
98+
99+
for (Object element : collection)
100+
if (!contains(element)) {
101+
return false;
102+
}
103+
return true;
104+
}
105+
106+
@SuppressWarnings("unchecked")
107+
@Override
108+
public E set(int index, E element) {
109+
E oldElement = (E) internal[index];
110+
internal[index] = element;
111+
return oldElement;
112+
}
113+
114+
@Override
115+
public void clear() {
116+
internal = new Object[0];
117+
}
118+
119+
@Override
120+
public int indexOf(Object object) {
121+
// the first cycle
122+
// if (object.equals(internal[0])) {
123+
// return 0;
124+
// }
125+
// return -1;
126+
127+
for (int i = 0; i < internal.length; i++) {
128+
if (object.equals(internal[i])) {
129+
return i;
130+
}
131+
}
132+
return -1;
133+
}
134+
135+
@Override
136+
public int lastIndexOf(Object object) {
137+
// the first cycle
138+
// if (object.equals(internal[0])) {
139+
// return 0;
140+
// }
141+
// return -1;
142+
143+
for (int i = internal.length - 1; i >= 0; i--) {
144+
if (object.equals(internal[i])) {
145+
return i;
146+
}
147+
}
148+
return -1;
149+
}
150+
151+
@SuppressWarnings("unchecked")
152+
@Override
153+
public List<E> subList(int fromIndex, int toIndex) {
154+
// the first cycle
155+
// return (List<E>) Arrays.asList(internal);
156+
157+
Object[] temp = new Object[toIndex - fromIndex];
158+
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
159+
return (List<E>) Arrays.asList(temp);
160+
}
161+
162+
@Override
163+
public Object[] toArray() {
164+
return Arrays.copyOf(internal, internal.length);
165+
}
166+
167+
@SuppressWarnings("unchecked")
168+
@Override
169+
public <T> T[] toArray(T[] array) {
170+
// the first cycle
171+
// array[0] = (T) internal[0];
172+
// return array;
173+
174+
// the second cycle
175+
// if (array.length < internal.length) {
176+
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
177+
// }
178+
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
179+
180+
if (array.length < internal.length) {
181+
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
182+
}
183+
184+
System.arraycopy(internal, 0, array, 0, internal.length);
185+
if (array.length > internal.length) {
186+
array[internal.length] = null;
187+
}
188+
return array;
189+
}
190+
191+
@Override
192+
public Iterator<E> iterator() {
193+
return new CustomIterator();
194+
}
195+
196+
@Override
197+
public ListIterator<E> listIterator() {
198+
return null;
199+
}
200+
201+
@Override
202+
public ListIterator<E> listIterator(int index) {
203+
// ignored for brevity
204+
return null;
205+
}
206+
207+
private class CustomIterator implements Iterator<E> {
208+
int index;
209+
210+
@Override
211+
public boolean hasNext() {
212+
// the first cycle
213+
// return true;
214+
215+
return index != internal.length;
216+
}
217+
218+
@SuppressWarnings("unchecked")
219+
@Override
220+
public E next() {
221+
// the first cycle
222+
// return (E) CustomList.this.internal[0];
223+
224+
E element = (E) CustomList.this.internal[index];
225+
index++;
226+
return element;
227+
}
228+
}
229+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.casting;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.*;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class CastingTest {
9+
10+
@Test
11+
public void whenPrimitiveConverted_thenValueChanged() {
12+
double myDouble = 1.1;
13+
int myInt = (int) myDouble;
14+
assertNotEquals(myDouble, myInt);
15+
}
16+
17+
@Test
18+
public void whenUpcast_thenInstanceUnchanged() {
19+
Cat cat = new Cat();
20+
Animal animal = cat;
21+
animal = (Animal) cat;
22+
assertTrue(animal instanceof Cat);
23+
}
24+
25+
@Test
26+
public void whenUpcastToObject_thenInstanceUnchanged() {
27+
Object object = new Animal();
28+
assertTrue(object instanceof Animal);
29+
}
30+
31+
@Test
32+
public void whenUpcastToInterface_thenInstanceUnchanged() {
33+
Mew mew = new Cat();
34+
assertTrue(mew instanceof Cat);
35+
}
36+
37+
@Test
38+
public void whenUpcastToAnimal_thenOverridenMethodsCalled() {
39+
List<Animal> animals = new ArrayList<>();
40+
animals.add(new Cat());
41+
animals.add(new Dog());
42+
new AnimalFeeder().feed(animals);
43+
}
44+
45+
@Test
46+
public void whenDowncastToCat_thenMeowIsCalled() {
47+
Animal animal = new Cat();
48+
((Cat) animal).meow();
49+
}
50+
51+
@Test(expected = ClassCastException.class)
52+
public void whenDownCastWithoutCheck_thenExceptionThrown() {
53+
List<Animal> animals = new ArrayList<>();
54+
animals.add(new Cat());
55+
animals.add(new Dog());
56+
new AnimalFeeder().uncheckedFeed(animals);
57+
}
58+
}

0 commit comments

Comments
 (0)