-
Notifications
You must be signed in to change notification settings - Fork 31
Added business logic and tests #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vladikigs
wants to merge
1
commit into
java-online-course:master
Choose a base branch
from
vladikigs:feature/spring_logic_template
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 19 additions & 1 deletion
20
src/main/java/com/epam/edu/spring/core/template/SpringCoreTemplate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
package com.epam.edu.spring.core.template; | ||
|
||
import com.epam.edu.spring.core.template.configuration.ColorFactory; | ||
import com.epam.edu.spring.core.template.configuration.MainConfiguration; | ||
import com.epam.edu.spring.core.template.repository.ArrayListItemRepository; | ||
import com.epam.edu.spring.core.template.repository.LinkedListItemRepository; | ||
import com.epam.edu.spring.core.template.service.SimpleItemService; | ||
import com.epam.edu.spring.core.template.validator.ItemValidator; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
public class SpringCoreTemplate { | ||
|
||
public static void main(String[] args) { | ||
public static void main(String[] args) throws Exception { | ||
ApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfiguration.class); | ||
|
||
ItemValidator itemValidator = ctx.getBean(ItemValidator.class); | ||
SimpleItemService itemServiceConstructor = (SimpleItemService) ctx.getBean("itemServiceConstructor"); | ||
SimpleItemService itemServiceSetter = (SimpleItemService) ctx.getBean("itemServiceSetter"); | ||
|
||
LinkedListItemRepository linkedListItemRepository = ctx.getBean(LinkedListItemRepository.class); | ||
ArrayListItemRepository arrayListItemRepository = ctx.getBean(ArrayListItemRepository.class); | ||
|
||
ColorFactory colorFactory = ctx.getBean(ColorFactory.class); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/epam/edu/spring/core/template/configuration/ColorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.epam.edu.spring.core.template.configuration; | ||
|
||
import com.epam.edu.spring.core.template.entity.Color; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Random; | ||
|
||
@Component | ||
@Lazy | ||
public class ColorFactory { | ||
|
||
@Scope("prototype") | ||
public Color getColor() { | ||
return Color.values()[new Random().nextInt(Color.values().length)]; | ||
} | ||
|
||
} |
24 changes: 23 additions & 1 deletion
24
src/main/java/com/epam/edu/spring/core/template/configuration/InitializerConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
package com.epam.edu.spring.core.template.configuration; | ||
|
||
public class InitializerConfiguration { | ||
import com.epam.edu.spring.core.template.entity.Color; | ||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.context.annotation.Configuration; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
public class InitializerConfiguration implements FactoryBean<ColorFactory> { | ||
|
||
@Override | ||
public ColorFactory getObject() { | ||
return new ColorFactory(); | ||
} | ||
|
||
@Override | ||
public Class<?> getObjectType() { | ||
return ColorFactory.class; | ||
} | ||
|
||
public Color getColor() throws Exception { | ||
return Objects.requireNonNull(getObject()).getColor(); | ||
} | ||
|
||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/main/java/com/epam/edu/spring/core/template/configuration/MainConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,38 @@ | ||
package com.epam.edu.spring.core.template.configuration; | ||
|
||
import com.epam.edu.spring.core.template.repository.InjectRandomIntAnnotationBeanPostProcessor; | ||
import com.epam.edu.spring.core.template.service.SimpleItemService; | ||
import com.epam.edu.spring.core.template.validator.ItemValidator; | ||
import com.epam.edu.spring.core.template.validator.SimpleItemValidator; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.annotation.PropertySource; | ||
|
||
@Configuration | ||
@PropertySource("classpath:application.properties") | ||
@Import({ InitializerConfiguration.class, RepositoryConfiguration.class, InjectRandomIntAnnotationBeanPostProcessor.class}) | ||
public class MainConfiguration { | ||
|
||
@Bean | ||
public ItemValidator itemValidator() { | ||
return new SimpleItemValidator(); | ||
} | ||
|
||
@Bean | ||
@Qualifier("itemServiceConstructor") | ||
public SimpleItemService itemServiceConstructor(RepositoryConfiguration repositoryConfiguration) { | ||
return new SimpleItemService(repositoryConfiguration.getItemRepository(),itemValidator()); | ||
} | ||
|
||
@Bean | ||
public SimpleItemService itemServiceSetter(RepositoryConfiguration repositoryConfiguration) { | ||
SimpleItemService simpleItemService = new SimpleItemService(); | ||
simpleItemService.setItemRepository(repositoryConfiguration.getItemRepository()); | ||
simpleItemService.setItemValidator(itemValidator()); | ||
return simpleItemService; | ||
} | ||
|
||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/main/java/com/epam/edu/spring/core/template/configuration/RepositoryConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,38 @@ | ||
package com.epam.edu.spring.core.template.configuration; | ||
|
||
import com.epam.edu.spring.core.template.repository.ArrayListItemRepository; | ||
import com.epam.edu.spring.core.template.repository.ItemRepository; | ||
import com.epam.edu.spring.core.template.repository.LinkedListItemRepository; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RepositoryConfiguration { | ||
|
||
private ItemRepository itemRepository; | ||
|
||
@Bean | ||
public ArrayListItemRepository arrayListItemRepository() { | ||
return new ArrayListItemRepository(); | ||
} | ||
|
||
@Bean | ||
public LinkedListItemRepository linkedListItemRepository() { | ||
return new LinkedListItemRepository(); | ||
} | ||
|
||
@Value("${item.repository.implementation}") | ||
private void initConfiguration (String implementation) { | ||
if (implementation.equals("linked")) { | ||
itemRepository = linkedListItemRepository(); | ||
} else { | ||
itemRepository = arrayListItemRepository(); | ||
} | ||
} | ||
|
||
public ItemRepository getItemRepository() { | ||
return itemRepository; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,5 @@ public Color getColor() { | |
public void setColor(Color color) { | ||
this.color = color; | ||
} | ||
|
||
} |
22 changes: 18 additions & 4 deletions
22
src/main/java/com/epam/edu/spring/core/template/repository/ArrayListItemRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
package com.epam.edu.spring.core.template.repository; | ||
|
||
import com.epam.edu.spring.core.template.entity.Item; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.PropertySource; | ||
import javax.annotation.PostConstruct; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Репозиторий, основанный на классе ArrayList. | ||
* initialSequence должен браться из application.properties | ||
*/ | ||
|
||
@PropertySource("classpath:application.properties") | ||
public class ArrayListItemRepository extends AbstractRepository<Item> implements ItemRepository { | ||
|
||
|
||
@Override | ||
public Item getById(long id) { | ||
return null; | ||
return holder.stream().filter(o -> (o.getId() == id)).findFirst().orElse(null); | ||
} | ||
|
||
@Override | ||
public boolean createItem(Item item) { | ||
return false; | ||
item.setId(initialSequence); | ||
holder.add(item); | ||
initialSequence++; | ||
return true; | ||
} | ||
|
||
@Value("${initial.sequence}") | ||
@Override | ||
void setInitialSequence(int val) { | ||
//TODO | ||
initialSequence = val; | ||
} | ||
|
||
@PostConstruct | ||
void setHolder() { | ||
//TODO | ||
holder = new ArrayList<>(); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/epam/edu/spring/core/template/repository/InjectRandomInt.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.epam.edu.spring.core.template.repository; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface InjectRandomInt { | ||
int min(); | ||
int max(); | ||
} |
39 changes: 39 additions & 0 deletions
39
.../epam/edu/spring/core/template/repository/InjectRandomIntAnnotationBeanPostProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.epam.edu.spring.core.template.repository; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.annotation.Configuration; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Random; | ||
|
||
@Configuration | ||
public class InjectRandomIntAnnotationBeanPostProcessor implements BeanPostProcessor { | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
Method[] declaredMethods = bean.getClass().getDeclaredMethods(); | ||
for (Method item : declaredMethods) { | ||
InjectRandomInt annotation = item.getAnnotation(InjectRandomInt.class); | ||
if (annotation != null) { | ||
int min = annotation.min(); | ||
int max = annotation.max(); | ||
Random random = new Random(); | ||
|
||
try { | ||
item.invoke(bean, random.nextInt(max - min) + min); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
return bean; | ||
} | ||
|
||
} |
19 changes: 15 additions & 4 deletions
19
src/main/java/com/epam/edu/spring/core/template/repository/LinkedListItemRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,39 @@ | ||
package com.epam.edu.spring.core.template.repository; | ||
|
||
import com.epam.edu.spring.core.template.entity.Item; | ||
import org.springframework.stereotype.Component; | ||
import javax.annotation.PostConstruct; | ||
import java.util.LinkedList; | ||
|
||
/** | ||
* Репозиторий, основанный на классе LinkedList. | ||
* initialSequence должен случайно генерироваться из диапазона от 1 до 100 | ||
*/ | ||
@Component | ||
public class LinkedListItemRepository extends AbstractRepository<Item> implements ItemRepository { | ||
|
||
|
||
@Override | ||
public Item getById(long id) { | ||
return null; | ||
return holder.stream().filter(o -> (o.getId() == id)).findFirst().orElse(null); | ||
} | ||
|
||
@Override | ||
public boolean createItem(Item item) { | ||
return false; | ||
item.setId(initialSequence); | ||
holder.add(item); | ||
initialSequence++; | ||
return true; | ||
} | ||
|
||
@InjectRandomInt(min = 1, max = 100) | ||
void setInitialSequence(int val) { | ||
//TODO | ||
initialSequence = val; | ||
} | ||
|
||
@PostConstruct | ||
void setHolder() { | ||
//TODO | ||
holder = new LinkedList<>(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В задании имелось ввиду немного другое, нужно было при помощи аннотации
@Autowired
внедрить зависимости