Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void createNodeClass() {

@Test
@Order(2)
void chekFieldsNameInNodeClass() {
void checkFieldsNameInNodeClass() {
Class<?> innerClass = getInnerStaticNodeClass();
boolean hasElementField = hasField(innerClass, ELEMENT_FIELD);
boolean hasNodeField = hasField(innerClass, NODE_FIELD);
Expand Down Expand Up @@ -104,16 +104,19 @@ void addFillsQueueWhenItIsEmpty() {
@Order(5)
void addFillsQueueWhenItIsNotEmpty() {
addIntElementToQueue(12);
addIntElementToQueue(13);
integerQueue.add(111);
int size = getInternalSize();
boolean isEmpty = isEmptyQueue();
Integer firstElement = (Integer) pollElementFromQueue();
Integer secondElement = (Integer) pollElementFromQueue();
Integer tailValue = (Integer) getNodeValue(TAIL_FIELD);

assertThat(size).isEqualTo(2);
assertThat(size).isEqualTo(3);
assertThat(isEmpty).isEqualTo(false);
assertThat(firstElement).isEqualTo(12);
assertThat(secondElement).isEqualTo(111);
assertThat(secondElement).isEqualTo(13);
assertThat(tailValue).isEqualTo(111);
}

@Test
Expand Down Expand Up @@ -363,4 +366,12 @@ private Field getAccessibleFieldByPredicate(Object object, Predicate<Field> pred
field.setAccessible(true);
return field;
}

@SneakyThrows
private Object getNodeValue(Predicate<Field> predicate) {
Object field = getAccessibleFieldByPredicate(integerQueue, predicate).get(integerQueue);
final Field value = getAccessibleFieldByPredicate(field, ELEMENT_FIELD);
value.setAccessible(true);
return value.get(field);
}
}
6 changes: 6 additions & 0 deletions lesson-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<artifactId>java-fundamentals-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.22</version>
</dependency>
</dependencies>

</project>
12 changes: 12 additions & 0 deletions lesson-demo/src/main/java/com/bobocode/DemoApp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package com.bobocode;

import java.util.List;

public class DemoApp {
public static void main(String[] args) {
List<String> wordList = List.of("Hello", "Hey");
System.out.println(wordList.getClass().getComponentType()); // 👈

String[] wordArray = wordList.toArray(new String[1]);
System.out.println(wordArray.getClass().getComponentType());
}

// public static <T> T[] createGenericArray(int size) {
// return new T[size]; // it's not possible to create a generic array
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bobocode;

import java.util.Stack;
import java.util.stream.Collector;
import java.util.stream.Stream;

public class FunctionalProgrammingDemoApp {
public static void main(String[] args) {
Stack<String> wordsStack = Stream.of("Hello", "Hey")
.collect(toStack());

while (!wordsStack.isEmpty()) {
System.out.println(wordsStack.pop());
}

}

private static <T> Collector<T, ?, Stack<T>> toStack() {
return Collector.of(
Stack::new,
Stack::push,
(stack1, stack2) -> {
stack2.forEach(stack1::push);
return stack1;
}
);
}

}
32 changes: 32 additions & 0 deletions lesson-demo/src/main/java/com/bobocode/SpringDemoApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bobocode;

import com.bobocode.service.ConferenceService;
import com.bobocode.service.TalkingService;
import lombok.SneakyThrows;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

public class SpringDemoApp {
@SneakyThrows
public static void main(String[] args) {

var context = new AnnotationConfigApplicationContext("com.bobocode.service");
var conferenceService = context.getBean(ConferenceService.class);
// how spring works
var field = conferenceService.getClass().getDeclaredField("talkingServiceList");
ParameterizedType genericType = (ParameterizedType) field.getGenericType();
Type actualTypeArgument = genericType.getActualTypeArguments()[0];
Class<?> talkingServiceClass = Class.forName(actualTypeArgument.getTypeName());
System.out.println(talkingServiceClass);
Map<String, ?> beanMaps = context.getBeansOfType(talkingServiceClass);
beanMaps.values().stream()
.map(bean -> (TalkingService)bean)
.forEach(TalkingService::talk);


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.bobocode.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ConferenceService {
private final List<TalkingService> talkingServiceList;

public void runConference() {
talkingServiceList.forEach(TalkingService::talk);
}
}
11 changes: 11 additions & 0 deletions lesson-demo/src/main/java/com/bobocode/service/MadService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bobocode.service;

import org.springframework.stereotype.Service;

@Service
public class MadService {

public void talk() {
System.out.println("I am mad!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bobocode.service;

public interface TalkingService {
void talk();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bobocode.service.impl;

import com.bobocode.service.TalkingService;
import org.springframework.stereotype.Service;

@Service
public class HelloTalkingService implements TalkingService {
@Override
public void talk() {
System.out.println("Hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bobocode.service.impl;

import com.bobocode.service.TalkingService;
import org.springframework.stereotype.Service;

@Service
public class HeyTalkingService implements TalkingService {
@Override
public void talk() {
System.out.println("Hey");
}
}