Skip to content

Final Commit! #10

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,34 @@
<groupId>com.epam.edu</groupId>
<artifactId>spring-core-template</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>


</project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.epam.edu.spring.core.template;

import com.epam.edu.spring.core.template.configuration.MainConfiguration;


import com.epam.edu.spring.core.template.validator.SimpleItemValidator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class SpringCoreTemplate {



public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfiguration.class);
ApplicationContext applicationContext = annotationConfigApplicationContext;
applicationContext.getBean(SimpleItemValidator.class);


}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package com.epam.edu.spring.core.template.configuration;


import com.epam.edu.spring.core.template.entity.FactoryColor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

@Configuration
public class InitializerConfiguration {
@Lazy
@Bean
@Scope("prototype")
public FactoryColor factoryColor() {
return new FactoryColor();
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.epam.edu.spring.core.template.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ComponentScan("com.epam.edu.spring.core.template")
@Import({RepositoryConfiguration.class, InitializerConfiguration.class})
public class MainConfiguration {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
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;
import org.springframework.context.annotation.PropertySource;


import java.util.ArrayList;
import java.util.LinkedList;

@Configuration
@PropertySource(value = "classpath:application.properties")
public class RepositoryConfiguration {
@Value(value = "${item.repository.implementation}")
String implementationType;

@Bean
public ItemRepository itemRepository() {
if (implementationType.equals("linked")) {
return linkedListItemRepository();
} else {
return arrayListItemRepository();
}
}

@Bean
public LinkedListItemRepository linkedListItemRepository() {
return new LinkedListItemRepository(new LinkedList<>());
}

@Bean
public ArrayListItemRepository arrayListItemRepository() {
return new ArrayListItemRepository(new ArrayList<>());
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.epam.edu.spring.core.template.entity;

public enum Color {

YELLOW,
GREEN,
BLACK,
WHITE,
RED,
ORANGE,
BLUE,
BROWN,
PINK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.epam.edu.spring.core.template.entity;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class FactoryColor implements FactoryBean {
public Color getColor() throws Exception {
return getObject();
}


@Override
public Color getObject() {
return Color.values()[new Random().nextInt(Color.values().length)];
}

@Override
public Class<Color> getObjectType() {
return Color.class;
}

@Override
public boolean isSingleton() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public abstract class AbstractRepository<T> {

abstract void setHolder();


}
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
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 java.util.ArrayList;

/**
* Репозиторий, основанный на классе ArrayList.
* initialSequence должен браться из application.properties
*/

@PropertySource(value = "classpath:application.properties")
public class ArrayListItemRepository extends AbstractRepository<Item> implements ItemRepository {
public ArrayListItemRepository(ArrayList<Item> itemArrayList) {
this.itemArrayList = itemArrayList;
}

private ArrayList<Item> itemArrayList;


private int initialSequence;

@Override
public Item getById(long id) {
for (Item i : itemArrayList) {
if (i.getId() == id) {
return i;
}
}
return null;
}

@Override
public boolean createItem(Item item) {
return false;
return itemArrayList.add(item);
}

@Override
@Value(value = "${initial.sequence}")
void setInitialSequence(int val) {
//TODO
this.initialSequence = val;
}


@Override
void setHolder() {
//TODO
holder = this.itemArrayList;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@

import com.epam.edu.spring.core.template.entity.Item;


import java.util.LinkedList;
import java.util.Random;

/**
* Репозиторий, основанный на классе LinkedList.
* initialSequence должен случайно генерироваться из диапазона от 1 до 100
*/
public class LinkedListItemRepository extends AbstractRepository<Item> implements ItemRepository {

public LinkedListItemRepository(LinkedList<Item> itemLinkedList) {
this.itemLinkedList = itemLinkedList;
}

private LinkedList<Item> itemLinkedList;
int initialSequence;

@Override
public Item getById(long id) {
for (Item i : itemLinkedList) {
if (i.getId() == id) {
return i;
}
}
return null;
}

@Override
public boolean createItem(Item item) {
return false;
return itemLinkedList.add(item);
}

void setInitialSequence(int val) {
//TODO

public void setInitialSequence(int val) {
Random random = new Random(val);
this.initialSequence = random.nextInt(100);
}


void setHolder() {
//TODO
holder = this.itemLinkedList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,46 @@
import com.epam.edu.spring.core.template.entity.Item;
import com.epam.edu.spring.core.template.repository.ItemRepository;
import com.epam.edu.spring.core.template.validator.ItemValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SimpleItemService implements ItemService {

private ItemRepository itemRepository;

private ItemValidator itemValidator;

@Autowired
public SimpleItemService(ItemRepository itemRepository, ItemValidator itemValidator) {
this.itemRepository = itemRepository;
this.itemValidator = itemValidator;
}

@Autowired
public void setItemRepository(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}

@Autowired
public void setItemValidator(ItemValidator itemValidator) {
this.itemValidator = itemValidator;
}


@Override
public Item getById(long id) {
return null;
public Item getById(long id) {return itemRepository.getById(id);
}

public ItemRepository getItemRepository() {
return itemRepository;
}

@Override
public boolean createItem(Item item) {
return false;
return itemRepository.createItem(item);
}


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.epam.edu.spring.core.template.validator;

import com.epam.edu.spring.core.template.entity.Item;
import org.springframework.stereotype.Component;

@Component
public class SimpleItemValidator implements ItemValidator {

@Override
public boolean isItemValid(Item item) {
return false;
return item != null;
}
}
Loading