diff --git a/Owner.md b/Owner.md new file mode 100644 index 00000000..f0d27f60 --- /dev/null +++ b/Owner.md @@ -0,0 +1 @@ +Nagaiko Ivan diff --git a/pom.xml b/pom.xml index ffe9eeed..1b6a5846 100644 --- a/pom.xml +++ b/pom.xml @@ -120,6 +120,11 @@ jackson-databind LATEST + + junit + junit + RELEASE + diff --git a/src/main/java/track/container/Container.java b/src/main/java/track/container/Container.java index 36c4bd9d..feffb989 100644 --- a/src/main/java/track/container/Container.java +++ b/src/main/java/track/container/Container.java @@ -1,8 +1,21 @@ package track.container; +import java.beans.PropertyEditor; +import java.beans.PropertyEditorManager; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Field; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException; import track.container.config.Bean; +import track.container.config.InvalidConfigurationException; +import track.container.config.Property; +import track.container.config.ValueType; + +import static org.apache.log4j.config.PropertyPrinter.capitalize; /** * Основной класс контейнера @@ -10,25 +23,81 @@ */ public class Container { + private Map beanById; + private Map beanByClassName; + private Map objById; // Реализуйте этот конструктор, используется в тестах! public Container(List beans) { - + beanById = new HashMap<>(); + beanByClassName = new HashMap<>(); + objById = new HashMap<>(); + for (Bean bean : beans) { + beanById.put(bean.getId(), bean); + beanByClassName.put(bean.getClassName(), bean); + } } /** * Вернуть объект по имени бина из конфига * Например, Car car = (Car) container.getById("carBean") */ - public Object getById(String id) { - return null; + public Object getById(String id) throws InvalidConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { + Object object = objById.get(id); + if (object == null) { + Bean bean = beanById.get(id); + if (bean != null) { + object = createObject(bean); + } else { + throw new InvalidConfigurationException("Bean without id"); + } + } + + return object; } /** * Вернуть объект по имени класса * Например, Car car = (Car) container.getByClass("track.container.beans.Car") */ - public Object getByClass(String className) { - return null; + public Object getByClass(String className) throws InvalidConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { + Object object = objById.get(className); + + if (object == null) { + Bean bean = beanByClassName.get(className); + if (bean != null) { + object = createObject(bean); + } else { + throw new InvalidConfigurationException("Bean without class"); + } + } + + return object; + } + + private Object createObject(Bean bean) throws ValueException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + Class clazz = Class.forName(bean.getClassName()); + Object object = clazz.newInstance(); + objById.put(bean, object); + for (Map.Entry entry : bean.getProperties().entrySet()) { + Property property = entry.getValue(); + Field field = clazz.getDeclaredField(property.getName()); + Class type = field.getType(); + Method set = clazz.getMethod(getSetName(property.getName()), type); + if (property.getType() == ValueType.VAL) { + set.invoke(object, Integer.parseInt(property.getValue())); + } else if (property.getType() == ValueType.REF) { + if (!objById.containsKey(beanById.get(property.getValue()))) { + createObject(beanById.get(property.getValue())); + } + set.invoke(object, objById.get(beanById.get(property.getValue()))); + } + + } + return object; + } + + private String getSetName(String line) { + return "set" + Character.toUpperCase(line.charAt(0)) + line.substring(1); } } diff --git a/src/main/java/track/container/JsonConfigReader.java b/src/main/java/track/container/JsonConfigReader.java index 76eb69ff..94d9f977 100644 --- a/src/main/java/track/container/JsonConfigReader.java +++ b/src/main/java/track/container/JsonConfigReader.java @@ -1,19 +1,60 @@ package track.container; import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; -import track.container.config.Bean; -import track.container.config.ConfigReader; -import track.container.config.InvalidConfigurationException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import track.container.config.*; /** * TODO: Реализовать */ public class JsonConfigReader implements ConfigReader { + private Property parseProperty(JsonNode property) throws InvalidConfigurationException { + String name = property.get("name").asText(); + ValueType type; + String value = property.get("value").asText(); + if (property.get("type").asText().equals("REF")) { + type = ValueType.REF; + } else { + type = ValueType.VAL; + } + + return new Property(name, value, type); + } + + private Bean parseBean(JsonNode bean) throws InvalidConfigurationException { + String id = bean.get("id").asText(); + String className = bean.get("className").asText(); + Map properties = new HashMap(); + for (JsonNode property: bean.get("properties")) { + Property parsedProperty = parseProperty(property); + properties.put(parsedProperty.getName(), parsedProperty); + } + + return new Bean(id, className, properties); + } + @Override public List parseBeans(File configFile) throws InvalidConfigurationException { - return null; + ObjectMapper objectMapper = new ObjectMapper(); + List beans = new ArrayList<>(); + try { + JsonNode root = objectMapper.readTree(configFile); + for (JsonNode bean : root.get("beans")) { + beans.add(parseBean(bean)); + } + } catch (InvalidConfigurationException | IOException e) { + e.printStackTrace(); + } + return beans; } } + + diff --git a/src/main/java/track/container/Main.java b/src/main/java/track/container/Main.java index 8fdc23e6..8f6abf02 100644 --- a/src/main/java/track/container/Main.java +++ b/src/main/java/track/container/Main.java @@ -1,11 +1,28 @@ package track.container; +import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException; +import org.junit.Assert; +import track.container.beans.Car; +import track.container.beans.Engine; +import track.container.beans.Gear; +import track.container.config.Bean; +import track.container.config.ConfigReader; +import track.container.config.InvalidConfigurationException; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.lang.String; + +import java.io.File; + +import static org.apache.log4j.helpers.Loader.getResource; + /** * */ public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws ValueException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, InvalidConfigurationException { /* @@ -14,12 +31,16 @@ public static void main(String[] args) { */ // // При чтении нужно обработать исключение -// ConfigReader reader = new JsonReader(); -// List beans = reader.parseBeans("config.json"); -// Container container = new Container(beans); -// -// Car car = (Car) container.getByClass("track.container.beans.Car"); -// car = (Car) container.getById("carBean"); + File file = new File(getResource("config.json").getFile()); + ConfigReader reader = new JsonConfigReader(); + try { + List beans = reader.parseBeans(file); + System.out.print(beans.toString()); + + } catch (InvalidConfigurationException exception) { + System.out.println(exception.getMessage()); + } + } diff --git a/src/main/java/track/lessons/lesson2/Document.java b/src/main/java/track/lessons/lesson2/Document.java index fa2b2915..657ebfbf 100644 --- a/src/main/java/track/lessons/lesson2/Document.java +++ b/src/main/java/track/lessons/lesson2/Document.java @@ -1,20 +1,26 @@ package track.lessons.lesson2; -/** - * - */ public class Document { String[] tokens; + Document(String[] token) { + this.tokens = token; + } + String[] getTokens() { - return null; + return tokens; } int getTokenCount() { - return 0; + return tokens.length; } boolean hasToken(String token) { + for (int i = 0; i < tokens.length; i++) { + if (token.equals(tokens[i])) { + return true; + } + } return false; } } diff --git a/src/main/java/track/lessons/lesson2/Document.java~ b/src/main/java/track/lessons/lesson2/Document.java~ new file mode 100644 index 00000000..3e687db5 --- /dev/null +++ b/src/main/java/track/lessons/lesson2/Document.java~ @@ -0,0 +1,26 @@ +package track.lessons.lesson2; + +public class Document { + String[] tokens; + + Document(String[] token) { + this.tokens = token; + } + + String[] getTokens() { + return tokens; + } + + int getTokenCount() { + return tokens.length; + } + + boolean hasToken(String token) { + for(int i = 0; i < tokens.length; i++) { + if (token.equals(tokens[i])) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/track/lessons/lesson2/Parser.java b/src/main/java/track/lessons/lesson2/Parser.java index 7276042d..8c8d32cf 100644 --- a/src/main/java/track/lessons/lesson2/Parser.java +++ b/src/main/java/track/lessons/lesson2/Parser.java @@ -3,25 +3,27 @@ import java.io.BufferedReader; import java.io.FileReader; -/** - * - */ public class Parser { - Document parse(String data) { - return null; + if (data == null) { + return null; + } + String[] tokens = data.split("[ ]"); + return new Document(tokens); } public static void main(String[] args) throws Exception { String path = "path/to/file"; BufferedReader reader = new BufferedReader(new FileReader(path)); - // reader умеет читать по строкам с помощью метода readLine() - - // Создайте объект Parser - - // Получите объект Document, реализовав метод parse() - - + StringBuilder builder = new StringBuilder(); + Parser pars = new Parser(); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line); + builder.append(" "); + } + String res = builder.toString(); + Document doc = pars.parse(res); } } diff --git a/src/main/java/track/lessons/lesson2/Parser.java~ b/src/main/java/track/lessons/lesson2/Parser.java~ new file mode 100644 index 00000000..e2f84799 --- /dev/null +++ b/src/main/java/track/lessons/lesson2/Parser.java~ @@ -0,0 +1,28 @@ +package track.lessons.lesson2; + +import java.io.BufferedReader; +import java.io.FileReader; + +public class Parser { + Document parse(String data) { + String[] tokens = data.split("[ ]"); + if (tokens == null) { + return null; + } + return new Document(tokens); + } + + public static void main(String[] args) throws Exception { + + String path = "path/to/file"; + BufferedReader reader = new BufferedReader(new FileReader(path)); + StringBuilder builder = new StringBuilder(); + Parser pars = new Parser(); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line); + } + String res = builder.toString(); + Document doc = pars.parse(res); + } +} diff --git a/src/main/java/track/lessons/lesson3/DynamicList.java b/src/main/java/track/lessons/lesson3/DynamicList.java index 8b30d2c7..5a6cabc3 100644 --- a/src/main/java/track/lessons/lesson3/DynamicList.java +++ b/src/main/java/track/lessons/lesson3/DynamicList.java @@ -1,7 +1,51 @@ package track.lessons.lesson3; -/** - * - */ -public class DynamicList { -} + +public class DynamicList extends List { + private static final int DEFAULT_SIZE = 1000; + private int[] array; + private int size; + + public DynamicList() { + array = new int[DEFAULT_SIZE]; + } + + @Override + public void add(int value) { + if (size == array.length) { + allocate(); + } + array[size++] = value; + } + + @Override + public int remove(int idx) { + valid_index(idx); + int value = array[idx]; + System.arraycopy(array, idx + 1, array, idx, size-- - idx); + return value; + } + + @Override + public int get(int idx) { + valid_index(idx); + return array[idx]; + } + + @Override + public int size() { + return size; + } + + private void allocate() { + int[] newarray = new int[2 * size]; + System.arraycopy(array, 0, newarray, 0, size); + array = newarray; + } + + private void valid_index(int idx) { + if (idx < 0 || idx >= size) { + throw new IndexOutOfBoundsException("Invalid index"); + } + } +} \ No newline at end of file diff --git a/src/main/java/track/lessons/lesson3/DynamicList.java~ b/src/main/java/track/lessons/lesson3/DynamicList.java~ new file mode 100644 index 00000000..c6c63a94 --- /dev/null +++ b/src/main/java/track/lessons/lesson3/DynamicList.java~ @@ -0,0 +1,52 @@ +package track.lessons.lesson3; + +/** + * + */ +public class DynamicList extends List{ + private static final int DEFAULT_SIZE = 1000; + private int[] array; + private int size; + + public DynamicList() { + array = new int[DEFAULT_SIZE]; + } + + @Override + public void add(int value) { + if(size == array.length) { + allocate(); + } + array[size++] = value; + } + + @Override + public int remove(int idx) { + valid_index(idx); + int value = array[idx]; + System.arraycopy(array, idx + 1, array, idx, size-- - idx); + return value; + } + + @Override + public int get(int idx) { + valid_index(idx); + return array[idx]; + } + @Override + public int size() { + return size; + } + + private void allocate() { + int[] newarray = new int[2 * size]; + System.arraycopy(array, 0, newarray, 0, size); + array = newarray; + } + + private void valid_index(int idx) { + if (idx < 0 || idx >= size) { + throw new IndexOutOfBoundsException("Invalid index"); + } + } +} diff --git a/src/main/java/track/lessons/lesson3/LinkedList.java b/src/main/java/track/lessons/lesson3/LinkedList.java index 49a5e181..fa1ff1e8 100644 --- a/src/main/java/track/lessons/lesson3/LinkedList.java +++ b/src/main/java/track/lessons/lesson3/LinkedList.java @@ -1,7 +1,139 @@ package track.lessons.lesson3; -/** - * - */ -public class LinkedList { -} + +public class LinkedList extends List implements Stack, Queue { + + private class Node { + public Node next; + public Node prev; + public int value; + } + + private Node first; + private Node last; + private int size; + + public LinkedList() { + first = null; + last = null; + size = 0; + } + + private boolean isEmpty() { + if (size > 0) { + return false; + } + return true; + } + + @Override + public void add(int item) { + Node newNode = new Node(); + if (isEmpty()) { + newNode.prev = newNode; + newNode.next = newNode; + first = newNode; + } else { + newNode.prev = last; + newNode.next = first; + } + last = newNode; + first.prev = last; + newNode.value = item; + size++; + } + + private void valid_index(int idx) { + if (idx < 0 || idx >= size) { + throw new IndexOutOfBoundsException("Invalid index"); + } + } + + @Override + public int remove(int idx) { + valid_index(idx); + size--; + if (idx == 0) { + return removeFirst(); + } + if (idx == size) { + return removeLast(); + } + Node node = new Node(); + node = first; + for (int i = 0; i <= idx; i++) { + node = node.next; + } + + Node next = new Node(); + next = node.next; + node.prev.next = node.next; + next.prev = node.prev; + return node.value; + } + + private int removeLast() { + int value = last.value; + if (last.prev != null) { + last = last.prev; + last.prev.next = null; + } else { + last = null; + } + return value; + } + + private int removeFirst() { + int value = first.value; + if (first.next != null) { + first = first.next; + first.next.prev = null; + } else { + first = null; + } + return value; + } + + @Override + int get(int idx) { + valid_index(idx); + if (idx == 0) { + return first.value; + } + if (idx == size - 1) { + return last.value; + } + Node node = new Node(); + node = first; + for (int i = 0; i <= idx; i++) { + node = node.next; + } + return node.value; + } + + @Override + int size() { + return size; + } + + @Override + public void enqueue(int value) { + add(value); + } + + @Override + public int dequeu() { + return remove(0); + } + + @Override + public void push(int value) { + add(value); + } + + @Override + public int pop() { + return remove(size - 1); + } + +} \ No newline at end of file diff --git a/src/main/java/track/lessons/lesson3/LinkedList.java~ b/src/main/java/track/lessons/lesson3/LinkedList.java~ new file mode 100644 index 00000000..42297803 --- /dev/null +++ b/src/main/java/track/lessons/lesson3/LinkedList.java~ @@ -0,0 +1,141 @@ +package track.lessons.lesson3; + +/** + * + */ +public class LinkedList extends List implements Stack, Queue { + + private class Node { + public Node next; + public Node prev; + public int value; + } + + private Node first; + private Node last; + private int size; + + public LinkedList() { + first = null; + last = null; + size = 0; + } + + private boolean isEmpty() { + if (size > 0) { + return true; + } + return false; + } + + @Override + public void add(int item) { + Node newNode = new Node(); + if (isEmpty() == true) { + newNode.prev = newNode; + newNode.next = newNode; + first = newNode; + } + else { + newNode.prev = last; + newNode.next = first; + } + last = newNode; + newNode.value = item; + size++; + } + + private void valid_index(int idx) { + if (idx < 0 || idx >= size) { + throw new IndexOutOfBoundsException("Invalid index"); + } + } + + @Override + public int remove(int idx) { + valid_index(idx); + size--; + if (idx == 0) { + return removeFirst(); + } + if (idx == size) { + return removeLast(); + } + Node node = new Node(); + node = first; + for (int i = 0; i <= idx; i++) { + node = node.next; + } + int cur_value = node.value; + Node next = new Node(); + next = node.next; + node.prev.next = node.next; + next.prev = node.prev; + return cur_value; + } + + private int removeLast() { + int value = last.value; + if (last.prev != null) { + last = last.prev; + last.prev.next = null; + } + else { + last = null; + } + return value; + } + + private int removeFirst() { + int value = first.value; + if(first.next != null) { + first = first.next; + first.next.prev = null; + } + else { + first = null; + } + return value; + } + + @Override + int get(int idx){ + valid_index(idx); + if (idx == 0) { + return first.value; + } + if (idx == size - 1) { + return last.value; + } + Node node = new Node(); + node = first; + for (int i = 0; i <= idx; i++) { + node = node.next; + } + return next.value; + } + @Override + int size(){ + return size; + } + + @Override + public void enqueue(int value) { + add(value); + } + + @Override + public int dequeu() { + return remove(0); + } + + @Override + public void push(int value) { + add(value); + } + + @Override + public int pop() { + return remove(size - 1); + } +} diff --git a/src/main/java/track/lessons/lesson3/List.java b/src/main/java/track/lessons/lesson3/List.java index 49a182d5..3f47e2ee 100644 --- a/src/main/java/track/lessons/lesson3/List.java +++ b/src/main/java/track/lessons/lesson3/List.java @@ -3,5 +3,12 @@ /** * */ -public class List { -} +public abstract class List { + abstract void add(int item); + + abstract int remove(int idx); + + abstract int get(int idx); + + abstract int size(); +} \ No newline at end of file diff --git a/src/main/java/track/lessons/lesson3/List.java~ b/src/main/java/track/lessons/lesson3/List.java~ new file mode 100644 index 00000000..e5f83d5c --- /dev/null +++ b/src/main/java/track/lessons/lesson3/List.java~ @@ -0,0 +1,11 @@ +package track.lessons.lesson3; + +/** + * + */ +public abstract class List { + abstract void add(int item); + abstract void remove(int idx); + abstract int get(int idx); + abstract int size(); +} diff --git a/src/main/java/track/lessons/lesson3/ListMain.java b/src/main/java/track/lessons/lesson3/ListMain.java index 5a2e7a3a..0a9c8b5d 100644 --- a/src/main/java/track/lessons/lesson3/ListMain.java +++ b/src/main/java/track/lessons/lesson3/ListMain.java @@ -6,13 +6,18 @@ public class ListMain { public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + for (int i = 0; i < 1000; i++) { + linkedList.add(i); + } + for (int i = 0; i < 1000; i++) { + linkedList.push(i); + } + DynamicList dynamicList = new DynamicList(); -// List list = new DynamicList(); -// list.add(1); -// list.add(2); -// list.add(10); -// int first = list.remove(0); - + for (int i = 0; i < 1000; i++) { + dynamicList.add(i); + } } -} +} \ No newline at end of file diff --git a/src/main/java/track/lessons/lesson3/ListMain.java~ b/src/main/java/track/lessons/lesson3/ListMain.java~ new file mode 100644 index 00000000..26d7438c --- /dev/null +++ b/src/main/java/track/lessons/lesson3/ListMain.java~ @@ -0,0 +1,72 @@ +package track.lessons.lesson3; + +/** + * + */ +public class ListMain { + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + + for (int i = 0; i < 1000; i++) { + linkedList.add(i); + } + + System.out.println(linkedList); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(3)); + System.out.println(linkedList.remove(1)); + System.out.println(linkedList); + System.out.println(linkedList.remove(linkedList.size() - 1)); + System.out.println(linkedList); + System.out.println(linkedList.remove(0)); + System.out.println(linkedList); + System.out.println(linkedList.remove(0)); + System.out.println(linkedList.remove(0)); + System.out.println(linkedList); + + for (int i = 0; i < 5; i++) { + linkedList.push(i); + } + System.out.println(linkedList); + System.out.println(linkedList.pop()); + + DynamicList dynamicList = new DynamicList(); + + for (int i = 0; i < 12; i++) { + dynamicList.add(i); + } + + System.out.println(dynamicList); + System.out.println(dynamicList.get(0)); + System.out.println(dynamicList.get(3)); + System.out.println(dynamicList.remove(1)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(dynamicList.size() - 1)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList); + System.out.println(dynamicList.remove(0)); + System.out.println(dynamicList); + +// List list = new DynamicList(); +// list.add(1); +// list.add(2); +// list.add(10); +// int first = list.remove(0); + + } +} diff --git a/src/main/java/track/lessons/lesson3/Queue.java b/src/main/java/track/lessons/lesson3/Queue.java new file mode 100644 index 00000000..4356ad72 --- /dev/null +++ b/src/main/java/track/lessons/lesson3/Queue.java @@ -0,0 +1,7 @@ +package track.lessons.lesson3; + +public interface Queue { + void enqueue(int value); + + int dequeu(); +} diff --git a/src/main/java/track/lessons/lesson3/Queue.java~ b/src/main/java/track/lessons/lesson3/Queue.java~ new file mode 100644 index 00000000..5df61cb7 --- /dev/null +++ b/src/main/java/track/lessons/lesson3/Queue.java~ @@ -0,0 +1,2 @@ +package track.lessons.lesson3; + diff --git a/src/main/java/track/lessons/lesson3/Stack.java b/src/main/java/track/lessons/lesson3/Stack.java new file mode 100644 index 00000000..98f8b1ba --- /dev/null +++ b/src/main/java/track/lessons/lesson3/Stack.java @@ -0,0 +1,7 @@ +package track.lessons.lesson3; + +public interface Stack { + void push(int value); + + int pop(); +} diff --git a/src/main/java/track/lessons/lesson3/Stack.java~ b/src/main/java/track/lessons/lesson3/Stack.java~ new file mode 100644 index 00000000..17e08092 --- /dev/null +++ b/src/main/java/track/lessons/lesson3/Stack.java~ @@ -0,0 +1,4 @@ +interface Stack { + void push(int value); + int pop(); +} diff --git a/src/main/java/track/lessons/lesson8/IoUtil.java b/src/main/java/track/lessons/lesson8/IoUtil.java index 075ddcaa..41041df4 100644 --- a/src/main/java/track/lessons/lesson8/IoUtil.java +++ b/src/main/java/track/lessons/lesson8/IoUtil.java @@ -1,4 +1,5 @@ package track.lessons.lesson8; + import java.io.Closeable; public class IoUtil {