From 9d98cf0bc3b1606e8d12ad23cdc1a0871d878736 Mon Sep 17 00:00:00 2001 From: LordOfPhys Date: Thu, 6 Oct 2016 20:29:44 +0300 Subject: [PATCH 1/4] Added --- OWNER.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 OWNER.md diff --git a/OWNER.md b/OWNER.md new file mode 100644 index 00000000..0ed9b6f3 --- /dev/null +++ b/OWNER.md @@ -0,0 +1 @@ +Anton Gerasimenko Vyacheslavovich From 09b89b924a8c8e16b7961cecfbaa80da1d9dcae7 Mon Sep 17 00:00:00 2001 From: LordOfPhys Date: Tue, 1 Nov 2016 23:29:42 +0300 Subject: [PATCH 2/4] First Attempt --- src/main/java/track/container/Container.java | 90 +++++++++++++++---- .../track/container/JsonConfigReader.java | 49 ++++++++-- src/main/java/track/container/Main.java | 40 ++++----- 3 files changed, 131 insertions(+), 48 deletions(-) diff --git a/src/main/java/track/container/Container.java b/src/main/java/track/container/Container.java index 36c4bd9d..ae3f9580 100644 --- a/src/main/java/track/container/Container.java +++ b/src/main/java/track/container/Container.java @@ -1,34 +1,86 @@ package track.container; -import java.util.List; +import java.lang.reflect.Field; +import java.util.*; import track.container.config.Bean; +import track.container.config.InvalidConfigurationException; +import track.container.config.Property; +import track.container.config.ValueType; +import track.container.config.Bean; +import track.container.config.InvalidConfigurationException; -/** - * Основной класс контейнера - * У него определено 2 публичных метода, можете дописывать свои методы и конструкторы - */ public class Container { + private Map beanId; + private Map beanClass; + private Map objByName; + private Map objByClassName; - // Реализуйте этот конструктор, используется в тестах! public Container(List beans) { + beanId = new HashMap<>(); + beanClass = new HashMap<>(); + objByClassName = new HashMap<>(); + objByName = new HashMap<>(); + for (Bean bean : beans) { + beanId.put(bean.getId(), bean); + beanClass.put(bean.getClassName(), bean); + } + } + public Object getById(String id) throws IllegalAccessException, InstantiationException, NoSuchFieldException, InvalidConfigurationException, ClassNotFoundException { + if (objByName.containsKey(id)) { + return objByName.get(id); + } else { + if (beanId.get(id) == null) { + throw new InvalidConfigurationException("No id"); + } + Class clazz = Class.forName(beanId.get(id).getClassName()); + Object obj = clazz.newInstance(); + for (Map.Entry entry : beanClass.get(id).getProperties().entrySet()) { + Property prop = entry.getValue(); + Field field = clazz.getDeclaredField(prop.getName()); + field.setAccessible(true); + if (prop.getType().equals(ValueType.VAL)) { + Object value = Integer.parseInt(prop.getValue()); + field.set(obj, value); + } else { + if (prop.getType().equals(ValueType.REF)) { + String className = beanId.get(prop.getValue()).getClassName(); + if (!objByClassName.containsKey(className)) { + Class classN = Class.forName(className); + if (isDepend(beanId.get(id).getClassName(), classN.getDeclaredFields())) { + throw new InvalidConfigurationException("No dependency"); + } else { + Object value = getByClass(className); + field.set(obj, value); + } + } else { + field.set(obj, objByClassName.get(className)); + } + } + } + } + objByClassName.put(beanId.get(id).getClassName(), obj); + objByName.put(beanId.get(id).getId(), obj); + return obj; + } } - /** - * Вернуть объект по имени бина из конфига - * Например, Car car = (Car) container.getById("carBean") - */ - public Object getById(String id) { - return null; + public boolean isDepend (String classN, Field[] declaredF) { + for (Field field : declaredF) { + if (field.getType().getName().equals(classN)) { + return true; + } + } + return false; } - /** - * Вернуть объект по имени класса - * Например, Car car = (Car) container.getByClass("track.container.beans.Car") - */ - public Object getByClass(String className) { - return null; + public Object getByClass(String className) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, InvalidConfigurationException, IllegalAccessException { + if (objByClassName.containsKey(className)) { + return objByClassName.get(className); + } else { + return getById(beanClass.get(className).getId()); + } } -} +} \ No newline at end of file diff --git a/src/main/java/track/container/JsonConfigReader.java b/src/main/java/track/container/JsonConfigReader.java index 76eb69ff..397d5e3a 100644 --- a/src/main/java/track/container/JsonConfigReader.java +++ b/src/main/java/track/container/JsonConfigReader.java @@ -1,19 +1,50 @@ package track.container; import java.io.File; -import java.util.List; +import java.io.IOException; +import java.util.*; -import track.container.config.Bean; -import track.container.config.ConfigReader; -import track.container.config.InvalidConfigurationException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import track.container.config.*; -/** - * TODO: Реализовать - */ public class JsonConfigReader implements ConfigReader { @Override public List parseBeans(File configFile) throws InvalidConfigurationException { - return null; + ObjectMapper mapper = new ObjectMapper(); + List bean = new ArrayList<>(); + try { + JsonNode root = mapper.readTree(configFile).path("bean"); + for (JsonNode indexNode : root) { + String id = indexNode.path("id").asText(); + String className = indexNode.path("className").asText(); + Map properties = createProperty(indexNode.path("properties")); + bean.add(new Bean(id, className, properties)); + } + return bean; + } catch (IOException e) { + throw new InvalidConfigurationException("Invalid config"); + } } -} + + public Map createProperty(JsonNode jNode) throws InvalidConfigurationException { + Map properties = new HashMap<>(); + for (JsonNode indexNode : jNode) { + String name = indexNode.path("name").asText(); + Property property = null; + if (indexNode.has("ref")) { + property = new Property(name, indexNode.path("ref").asText(), ValueType.REF); + } else { + if (indexNode.has("val")) { + property = new Property(name, indexNode.path("val").asText(), ValueType.VAL); + } else { + throw new InvalidConfigurationException("Not value"); + } + } + properties.put(name, property); + } + return properties; + } +} \ No newline at end of file diff --git a/src/main/java/track/container/Main.java b/src/main/java/track/container/Main.java index 8fdc23e6..97bf27b5 100644 --- a/src/main/java/track/container/Main.java +++ b/src/main/java/track/container/Main.java @@ -1,26 +1,26 @@ package track.container; -/** - * - */ +import track.container.beans.Car; +import track.container.config.Bean; +import track.container.config.ConfigReader; +import track.container.config.InvalidConfigurationException; +import java.io.File; +import java.util.List; + public class Main { 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"); - - + ConfigReader reader = new JsonConfigReader(); + try { + File file = new File(JsonConfigReader.class.getClassLoader().getResource("config.json").getFile()); + List bean = reader.parseBeans(file); + System.out.println(bean); + Container container = new Container(bean); + Car car = (Car) container.getByClass("track.container.beans.Car"); + car = (Car) container.getById("carBean"); + System.out.println(car); + } catch (Exception e) { + e.printStackTrace(); + } } -} +} \ No newline at end of file From d8a46507ea3569d71a426e13c4aaa946b0002ace Mon Sep 17 00:00:00 2001 From: LordOfPhys Date: Tue, 1 Nov 2016 23:33:55 +0300 Subject: [PATCH 3/4] Fixed style --- src/main/java/track/container/Container.java | 2 +- src/main/java/track/container/JsonConfigReader.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/track/container/Container.java b/src/main/java/track/container/Container.java index ae3f9580..6e3f12bc 100644 --- a/src/main/java/track/container/Container.java +++ b/src/main/java/track/container/Container.java @@ -67,7 +67,7 @@ public Object getById(String id) throws IllegalAccessException, InstantiationExc } } - public boolean isDepend (String classN, Field[] declaredF) { + public boolean isDepend(String classN, Field[] declaredF) { for (Field field : declaredF) { if (field.getType().getName().equals(classN)) { return true; diff --git a/src/main/java/track/container/JsonConfigReader.java b/src/main/java/track/container/JsonConfigReader.java index 397d5e3a..2c15bec0 100644 --- a/src/main/java/track/container/JsonConfigReader.java +++ b/src/main/java/track/container/JsonConfigReader.java @@ -29,9 +29,9 @@ public List parseBeans(File configFile) throws InvalidConfigurationExcepti } } - public Map createProperty(JsonNode jNode) throws InvalidConfigurationException { + public Map createProperty(JsonNode jsonNode) throws InvalidConfigurationException { Map properties = new HashMap<>(); - for (JsonNode indexNode : jNode) { + for (JsonNode indexNode : jsonNode) { String name = indexNode.path("name").asText(); Property property = null; if (indexNode.has("ref")) { From 5e9ac43d70bc06dd0e6bd9bad5f7499063c86789 Mon Sep 17 00:00:00 2001 From: LordOfPhys Date: Tue, 1 Nov 2016 23:43:28 +0300 Subject: [PATCH 4/4] Again add ALL HOMEWORKS! --- .../java/track/lessons/lesson2/Document.java | 17 ++- .../java/track/lessons/lesson2/Parser.java | 27 ++-- .../track/lessons/lesson3/DynamicList.java | 67 ++++++++- .../track/lessons/lesson3/LinkedList.java | 139 +++++++++++++++++- src/main/java/track/lessons/lesson3/List.java | 14 +- .../java/track/lessons/lesson3/ListMain.java | 22 ++- .../java/track/lessons/lesson3/Queue.java | 7 + .../java/track/lessons/lesson3/Stack.java | 7 + 8 files changed, 258 insertions(+), 42 deletions(-) create mode 100644 src/main/java/track/lessons/lesson3/Queue.java create mode 100644 src/main/java/track/lessons/lesson3/Stack.java diff --git a/src/main/java/track/lessons/lesson2/Document.java b/src/main/java/track/lessons/lesson2/Document.java index fa2b2915..8a49e3e6 100644 --- a/src/main/java/track/lessons/lesson2/Document.java +++ b/src/main/java/track/lessons/lesson2/Document.java @@ -1,20 +1,23 @@ package track.lessons.lesson2; -/** - * - */ +import java.util.Arrays; + public class Document { String[] tokens; + public Document(String[] tokens) { + this.tokens = tokens; + } + String[] getTokens() { - return null; + return tokens; } int getTokenCount() { - return 0; + return tokens.length; } boolean hasToken(String token) { - return false; + return Arrays.asList(tokens).contains(token); } -} +} \ No newline at end of file diff --git a/src/main/java/track/lessons/lesson2/Parser.java b/src/main/java/track/lessons/lesson2/Parser.java index 7276042d..137a4979 100644 --- a/src/main/java/track/lessons/lesson2/Parser.java +++ b/src/main/java/track/lessons/lesson2/Parser.java @@ -2,26 +2,27 @@ import java.io.BufferedReader; import java.io.FileReader; +import java.util.Arrays; -/** - * - */ public class Parser { Document parse(String data) { - return null; + return new Document(data.split(" ")); } public static void main(String[] args) throws Exception { - String path = "path/to/file"; + String path = "test.txt"; BufferedReader reader = new BufferedReader(new FileReader(path)); - // reader умеет читать по строкам с помощью метода readLine() - - // Создайте объект Parser - - // Получите объект Document, реализовав метод parse() - - + StringBuilder stringBuilder = new StringBuilder(); + Parser parser = new Parser(); + String currentString = reader.readLine(); + while (currentString != null) { + stringBuilder.append(currentString); + currentString = reader.readLine(); + } + + Document document = parser.parse(stringBuilder.toString()); + System.out.println(Arrays.toString(document.getTokens())); } -} +} \ 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 index 8b30d2c7..c0ce869e 100644 --- a/src/main/java/track/lessons/lesson3/DynamicList.java +++ b/src/main/java/track/lessons/lesson3/DynamicList.java @@ -1,7 +1,64 @@ package track.lessons.lesson3; -/** - * - */ -public class DynamicList { -} +public class DynamicList extends List { + + private static int defaultsize = 10; + private int[] array; + private int size; + + public DynamicList(int size) { + if (size > 0) { + array = new int[size]; + } else { + array = new int[defaultsize]; + } + this.size = 0; + } + + public DynamicList() { + array = new int[defaultsize]; + size = 0; + } + + + @Override + void add(int item) { + if (size == array.length) { + int[] newArray = new int[array.length * 2]; + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size++] = item; + } + + @Override + int remove(int idx) { + int value; + if (!isRange(idx)) { + value = array[idx]; + System.arraycopy(array, idx + 1, array, idx, size - idx - 1); + size--; + } else { + value = -1; + } + return value; + } + + private boolean isRange(int idx) { + return idx < 0 || idx > size - 1; + } + + @Override + int get(int idx) { + if (!isRange(idx)) { + return array[idx]; + } else { + throw new IndexOutOfBoundsException("Index is incorrect"); + } + } + + @Override + int size() { + return size; + } +} \ 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 index 49a5e181..1fb5a63f 100644 --- a/src/main/java/track/lessons/lesson3/LinkedList.java +++ b/src/main/java/track/lessons/lesson3/LinkedList.java @@ -1,7 +1,136 @@ package track.lessons.lesson3; -/** - * - */ -public class LinkedList { -} +public class LinkedList extends List implements Queue, Stack { + + class Node { + private Node next; + private Node prev; + private int value; + + public Node() { + next = null; + prev = null; + value = 0; + } + + public void setNext(Node node) { + this.next = node; + } + + public void setPrev(Node node) { + this.prev = node; + } + + public void setValue(int value) { + this.value = value; + } + + public Node getNext() { + return this.next; + } + + public Node getPrev() { + return this.prev; + } + + public int getValue() { + return this.value; + } + } + + private int size; + private Node root; + + public LinkedList() { + root = null; + size = 0; + } + + @Override + public void add(int item) { + Node newnode = new Node(); + newnode.setValue(item); + if (size == 0) { + root = newnode; + } else { + Node search = root; + while (search.getNext() != null) { + search = search.getNext(); + } + search.setNext(newnode); + newnode.setPrev(search); + } + size++; + } + + @Override + public int remove(int idx) { + if (index(idx)) { + throw new IndexOutOfBoundsException("Index is incorrect"); + } else { + if (idx == 0) { + root = root.getNext(); + size--; + return root.getValue(); + } + Node delete = root; + int item = 0; + while (item != idx) { + delete = delete.getNext(); + item = item + 1; + } + if (idx != size - 1) { + delete.getPrev().setNext(delete.getNext()); + delete.getNext().setPrev(delete.getPrev()); + } else { + delete.getPrev().setNext(null); + } + size--; + return delete.getValue(); + } + } + + private boolean index(int idx) { + return idx < 0 || idx > size - 1; + } + + @Override + public int get(int idx) { + if (index(idx)) { + throw new IndexOutOfBoundsException("Index is incorrect"); + } else { + Node search = root; + int item = 0; + while (item != idx) { + search = search.getNext(); + item++; + } + return search.getValue(); + } + } + + @Override + public int size() { + return size; + } + + + public void push(int value) { + add(value); + } + + + public int pop() { + return remove(size - 1); + } + + + public void enqueue(int value) { + add(value); + } + + + public int dequeu() { + return remove(0); + } +} \ 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 index 49a182d5..fdc5e713 100644 --- a/src/main/java/track/lessons/lesson3/List.java +++ b/src/main/java/track/lessons/lesson3/List.java @@ -1,7 +1,11 @@ package track.lessons.lesson3; -/** - * - */ -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/ListMain.java b/src/main/java/track/lessons/lesson3/ListMain.java index 5a2e7a3a..45c0036f 100644 --- a/src/main/java/track/lessons/lesson3/ListMain.java +++ b/src/main/java/track/lessons/lesson3/ListMain.java @@ -6,13 +6,21 @@ public class ListMain { public static void main(String[] args) { + LinkedList test = new LinkedList(); + test.add(1); + test.add(2); + test.add(3); + System.out.println(test.size()); + test.remove(2); + System.out.println(test.get(0)); - -// List list = new DynamicList(); -// list.add(1); -// list.add(2); -// list.add(10); -// int first = list.remove(0); - + DynamicList one = new DynamicList(); + one.add(1); + one.add(2); + one.add(3); + System.out.println(one.size()); + one.remove(1); + System.out.println(one.get(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..2d3e3127 --- /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(); +} \ No newline at end of file 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..08452076 --- /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(); +} \ No newline at end of file