diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java deleted file mode 100644 index 23ed3f6bc2..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java +++ /dev/null @@ -1,75 +0,0 @@ -package week01.BasicDataStructure; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - ensureCapacity(size + 1); //size increase,in order to have enough capacity. - elementData[size++] = o; //similar to: elementData[size]=o; size++; - } - - private void ensureCapacity(int minCapacity){ - if(minCapacity > elementData.length){ - grow(minCapacity); - } - } - - private void grow(int minCapacity){ - int oldCapacity = elementData.length; - int newCapacity = oldCapacity + ( oldCapacity >> 1 ); - if(newCapacity < minCapacity){ - newCapacity = minCapacity; - } - elementData = Arrays.copyOf(elementData, newCapacity); - - } - - public void add(int index, Object o){ - if(index < 0 || index > size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); - ensureCapacity(size+1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index){ - if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); - return elementData[index]; - } - - public Object remove(int index){ - if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); - Object data_index = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[size - 1] = null; - size--; - return data_index; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return new ArrayListIterator(); - } - - private class ArrayListIterator implements Iterator{ - - private int pos = 0; - - public boolean hasNext() { - return pos < size; - } - - public Object next() { - return elementData[pos++]; - } - } - -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java deleted file mode 100644 index a4fb2cf8b9..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java +++ /dev/null @@ -1,56 +0,0 @@ -package week01.BasicDataStructure; - -public class BinaryTreeNode{ - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(Object data){ - this.data = data; - left = null; - right = null; - } - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - if((Integer)o < (Integer)this.data) - { - if(this.left == null){ - BinaryTreeNode node = new BinaryTreeNode(o); - this.setLeft(node); - return node; - }else{ - return this.left.insert(o); - } - }else{ - if(this.right == null){ - BinaryTreeNode node = new BinaryTreeNode(o); - this.setRight(node); - return node; - }else{ - return this.right.insert(o); - } - } - } - } - - diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java deleted file mode 100644 index 0ad3fff8f3..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package week01.BasicDataStructure; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java deleted file mode 100644 index 35b1158cd1..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java +++ /dev/null @@ -1,113 +0,0 @@ -package week01.BasicDataStructure; - -public class LinkedList implements List { - - private Node head; - private int size = 0; - - public void add(Object o){ - if(head == null){ - head = new Node(o); - }else{ - Node pos = head; - while(pos.next != null){ - pos = pos.next; - } - pos.next = new Node(o); - } - size++; - } - - public void add(int index , Object o){ - if(index < 0 || index >size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); - if(index == 0) { - Node node = new Node(o); - node.next = head; - head = node; - } - else{ - Node pos = head; - for(int i = 0;i < index-1;i++){ - pos = pos.next; - } - Node node = new Node(o); - node.next = pos.next; - pos.next = node; - } - size++; - } - - public Object get(int index){ - if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); - Node pos = head; - for(int i = 0;i < index;i++){ - pos = pos.next; - } - return pos.data; - } - - public Object remove(int index){ - if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); - Node element = head; - if(index == 0){ - head = head.next; - }else{ - Node pos = head; - for(int i = 0;i < index - 1;i++){ - pos = pos.next; - } - element = pos.next; - pos.next = pos.next.next; - } - size--; - return element.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - add(0,o); - } - public void addLast(Object o){ - add(size,o); - } - public Object removeFirst(){ - return remove(0); - } - public Object removeLast(){ - return remove(size-1); - } - public Iterator iterator(){ - return new LinkedListIterator(); - } - - class LinkedListIterator implements Iterator{ - - private Node node = head; - private int pos = 0; - @Override - public boolean hasNext() { - return pos < size; - } - - @Override - public Object next() { - pos++; - if(pos != 1){ - node = node.next; - } - return node.data; - } - } - - private static class Node{ - Object data; - Node next; - public Node(Object data){ - this.data = data; - next = null; - } - } -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java deleted file mode 100644 index 7806b75ed3..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package week01.BasicDataStructure; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java deleted file mode 100644 index e0ab6bbb9c..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java +++ /dev/null @@ -1,25 +0,0 @@ -package week01.BasicDataStructure; - -public class Queue { - - private LinkedList linkedList = new LinkedList(); - private int size = 0; - - public void enQueue(Object o){ - linkedList.add(o); - size++; - } - - public Object deQueue(){ - size--; - return linkedList.removeFirst(); - } - - public boolean isEmpty(){ - return linkedList.size() == 0; - } - - public int size(){ - return size; - } -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java deleted file mode 100644 index 53f99b37c7..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java +++ /dev/null @@ -1,25 +0,0 @@ -package week01.BasicDataStructure; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size = 0; - - public void push(Object o){ - elementData.add(o); - size++; - } - - public Object pop(){ - return elementData.remove(--size); - } - - public Object peek(){ - return elementData.get(size - 1); - } - public boolean isEmpty(){ - return elementData.size() == 0; - } - public int size(){ - return elementData.size(); - } -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java deleted file mode 100644 index 5d5f07d815..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package week01.BasicDataStructureTest; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({ - ArrayListTest.class, - BinaryTreeNodeTest.class, - LinkedListTest.class, - QueueTest.class, - StackTest.class -}) - -public class AllTest { - -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java deleted file mode 100644 index c5513acfda..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package week01.BasicDataStructureTest; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import week01.BasicDataStructure.ArrayList; -import week01.BasicDataStructure.Iterator; - -public class ArrayListTest { - - private ArrayList arrayList = new ArrayList(); - - @Before - public void setUp() throws Exception { - for(int i = 0;i < 100 ; i++){ - arrayList.add(i); - } - } - - @Test - public void testAddObject() { - for(int i = 0;i < 100;i++){ - Assert.assertEquals(arrayList.get(i), i); - } - } - - @Test - public void testAddIntObject() { - arrayList.add(0,10); - arrayList.add(22, 44); - arrayList.add(40, 5); - arrayList.add(100,88); - Assert.assertEquals(arrayList.get(0), 10); - Assert.assertEquals(arrayList.get(22),44); - Assert.assertEquals(arrayList.get(40), 5); - Assert.assertEquals(arrayList.get(100), 88); - } - - @Test - public void testGet() { - Assert.assertEquals(arrayList.get(0), 0); - Assert.assertEquals(arrayList.get(33), 33); - Assert.assertEquals(arrayList.get(77), 77); - Assert.assertEquals(arrayList.get(99), 99); - } - - @Test - public void testRemove() { - Assert.assertEquals(arrayList.remove(0), 0); - Assert.assertEquals(arrayList.remove(0), 1); - Assert.assertEquals(arrayList.remove(97), 99); - Assert.assertEquals(arrayList.size(), 97); - } - - @Test - public void testSize() { - Assert.assertEquals(arrayList.size(), 100); - arrayList.add(5,5); - Assert.assertEquals(arrayList.size(),101); - arrayList.remove(5); - Assert.assertEquals(arrayList.size(), 100); - } - - @Test - public void testIterator() { - Iterator iterator = arrayList.iterator(); - for(int i=0;iterator.hasNext();i++){ - Assert.assertEquals(iterator.next(),i); - } - } -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java deleted file mode 100644 index 724e6c0e03..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java +++ /dev/null @@ -1,80 +0,0 @@ -package week01.BasicDataStructureTest; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import week01.BasicDataStructure.BinaryTreeNode; - - -public class BinaryTreeNodeTest { - - private BinaryTreeNode root = new BinaryTreeNode(5); - - @Before - public void setUp() throws Exception { - root.insert(2); - root.insert(7); - root.insert(1); - root.insert(6); - } - - @Test - public void testGetData() { - Assert.assertEquals(root.getData(), 5); - Assert.assertEquals(root.getLeft().getData(), 2); - Assert.assertEquals(root.getRight().getData(), 7); - Assert.assertEquals(root.getLeft().getLeft().getData(), 1); - Assert.assertEquals(root.getRight().getLeft().getData(), 6); - } - - @Test - public void testSetData() { - root.setData(8); - Assert.assertEquals(root.getData(),8); - root.getLeft().setData(88); - Assert.assertEquals(root.getLeft().getData(),88); - root.getRight().setData(888); - Assert.assertEquals(root.getRight().getData(),888); - } - - @Test - public void testGetLeft() { - BinaryTreeNode node_left = root.getLeft(); - Assert.assertEquals(node_left.getData(), 2); - BinaryTreeNode node_left_left = root.getLeft().getLeft(); - Assert.assertEquals(node_left_left.getData(), 1); - } - - @Test - public void testSetLeft() { - BinaryTreeNode node = new BinaryTreeNode(100); - root.setLeft(node); - Assert.assertEquals(root.getLeft().getData(), 100); - } - - @Test - public void testGetRight() { - BinaryTreeNode node_right = root.getRight(); - Assert.assertEquals(node_right.getData(), 7); - root.insert(8); - BinaryTreeNode node_right_right = root.getRight().getRight(); - Assert.assertEquals(node_right_right.getData(), 8); - } - - @Test - public void testSetRight() { - BinaryTreeNode node = new BinaryTreeNode(100); - root.setRight(node); - Assert.assertEquals(root.getRight().getData(), 100); - } - - @Test - public void testInsert() { - root.insert(4); - root.insert(8); - Assert.assertEquals(root.getLeft().getRight().getData(), 4); - Assert.assertEquals(root.getRight().getRight().getData(), 8); - } - -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java deleted file mode 100644 index 2fb20d12f4..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package week01.BasicDataStructureTest; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import week01.BasicDataStructure.Iterator; -import week01.BasicDataStructure.LinkedList; - -public class LinkedListTest { - - private LinkedList linkedList = new LinkedList(); - - @Before - public void setUp() throws Exception { - for(int i=0;i<100;i++){ - linkedList.add(i); - } - } - - @Test - public void testAddObject() { - for(int i=0;i<200;i++){ - linkedList.add(i); - } - for(int i=0;i<100;i++){ - Assert.assertEquals(linkedList.get(i), i); - } - for(int i=100;i<300;i++){ - Assert.assertEquals(linkedList.get(i), i-100); - } - } - - @Test - public void testAddIntObject() { - linkedList.add(0, 10); - Assert.assertEquals(linkedList.get(0), 10); - linkedList.add(5,60); - Assert.assertEquals(linkedList.get(5), 60); - Assert.assertEquals(linkedList.get(101), 99); - } - - @Test - public void testGet() { - for(int i =0;i<100;i++){ - Assert.assertEquals(linkedList.get(i), i); - } - } - - @Test - public void testRemove() { - Assert.assertEquals(linkedList.remove(0), 0); - Assert.assertEquals(linkedList.remove(0), 1); - Assert.assertEquals(linkedList.size(), 98); - linkedList.remove(97); - Assert.assertEquals(linkedList.get(96), 98); - } - - @Test - public void testSize() { - linkedList.add(0); - Assert.assertEquals(linkedList.size(), 101); - linkedList.add(0, 10); - Assert.assertEquals(linkedList.size(), 102); - linkedList.remove(0); - Assert.assertEquals(linkedList.size(), 101); - } - - @Test - public void testAddFirst() { - linkedList.addFirst(22); - Assert.assertEquals(linkedList.get(0), 22); - linkedList.addFirst(44); - Assert.assertEquals(linkedList.get(0), 44); - Assert.assertEquals(linkedList.size(), 102); - } - - @Test - public void testAddLast() { - linkedList.addLast(22); - Assert.assertEquals(linkedList.get(100), 22); - linkedList.addLast(44); - Assert.assertEquals(linkedList.get(101), 44); - } - - @Test - public void testRemoveFirst() { - Assert.assertEquals(linkedList.removeFirst(), 0); - Assert.assertEquals(linkedList.removeFirst(), 1); - } - - @Test - public void testRemoveLast() { - Assert.assertEquals(linkedList.removeLast(),99 ); - Assert.assertEquals(linkedList.removeLast(), 98); - } - - @Test - public void testIterator() { - Iterator iterator = linkedList.iterator(); - for(int i = 0;iterator.hasNext();i++){ - Assert.assertEquals(iterator.next(), i); - } - } - -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java deleted file mode 100644 index 7302b5ec38..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package week01.BasicDataStructureTest; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import week01.BasicDataStructure.Queue; - - -public class QueueTest { - - Queue queue = new Queue(); - - @Before - public void setUp() throws Exception { - for(int i=0;i<100;i++){ - queue.enQueue(i); - } - } - - @Test - public void testEnQueue() { - Assert.assertEquals(queue.size(), 100); - for(int i =0;i<100;i++){ - queue.enQueue(i); - } - Assert.assertEquals(queue.size(), 200); - } - - @Test - public void testDeQueue() { - for(int i =0;i<100;i++){ - Assert.assertEquals(queue.deQueue(), i); - } - - } - - @Test - public void testIsEmpty() { - Assert.assertEquals(queue.isEmpty(), false); - for(int i=0;i<100;i++){ - queue.deQueue(); - } - Assert.assertEquals(queue.isEmpty(), true); - } - - @Test - public void testSize() { - Assert.assertEquals(queue.size(), 100); - queue.enQueue(100); - Assert.assertEquals(queue.size(), 101); - queue.deQueue(); - Assert.assertEquals(queue.size(), 100); - } - -} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java deleted file mode 100644 index ae6d3a39d4..0000000000 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package week01.BasicDataStructureTest; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import week01.BasicDataStructure.Stack; - - -public class StackTest { - - Stack stack = new Stack(); - - @Before - public void setUp() throws Exception { - for(int i =0 ;i <100;i++){ - stack.push(i); - } - } - - @Test - public void testPush() { - Assert.assertEquals(stack.peek(), 99); - for(int i =0;i <200;i++){ - stack.push(i); - } - Assert.assertEquals(stack.peek(), 199); - Assert.assertEquals(stack.size(), 300); - } - - @Test - public void testPop() { - Assert.assertEquals(stack.pop(), 99); - Assert.assertEquals(stack.pop(), 98); - for(int i=0;i<98;i++){ - stack.pop(); - } - Assert.assertEquals(stack.size(), 0); - } - - @Test - public void testPeek() { - for(int i=0;i<100;i++){ - Assert.assertEquals(stack.peek(), 99); - Assert.assertEquals(stack.size(), 100); - } - stack.pop(); - Assert.assertEquals(stack.peek(), 98); - Assert.assertEquals(stack.peek(), 98); - } - - @Test - public void testIsEmpty() { - Assert.assertEquals(stack.isEmpty(), false); - for(int i =0 ;i <100;i++){ - stack.pop(); - } - Assert.assertEquals(stack.isEmpty(), true); - } - - @Test - public void testSize() { - stack.push(100); - Assert.assertEquals(stack.size(), 101); - stack.pop(); - Assert.assertEquals(stack.size(), 100); - stack.peek(); - Assert.assertEquals(stack.size(), 100); - } - -} diff --git a/group01/1814014897/zhouhui/src/week04/jvm/loader/ClassFileLoader.java b/group01/1814014897/zhouhui/src/week04/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..7b4d99d013 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week04/jvm/loader/ClassFileLoader.java @@ -0,0 +1,52 @@ + +package week04.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws Exception { + URL base = this.getClass().getResource("/"); + String baseToString = ""+base; + String filePath = baseToString.replaceAll("file:/", "")+className.replace(".", "\\")+".class"; + //String filePath = clzPaths.get(0)+"\\"+className.replace(".", "\\")+".class"; //符合Junit测试调用addClassPath方法 + File file = new File(filePath); + + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte[] buffer = new byte[1024]; + int len = 0; + while((len = bis.read(buffer)) != -1){ + baos.write(buffer,0,len); + } + return baos.toByteArray(); + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + StringBuffer strBuffer = new StringBuffer(); + for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + List list = DOT_SPLITTER.splitToList(className); + String childDirectory = SLASH_JOINER.join(list); + for (String clzPath : clzPaths) { + String fullPath = makeFullPath(clzPath, childDirectory); + if (fileExist(fullPath)) { + return readFileBytes(fullPath); + } + } + System.out.println("no this class file: " + className); + return null; + } + + private byte[] readFileBytes(String filePath) { + try { + File file = new File(filePath); + long length = file.length(); + byte[] fileBytes = new byte[(int) length]; + int readLength = new FileInputStream(filePath).read(fileBytes); + if (readLength != length) { + System.out.println("read file error. read length: " + readLength + ", full length : " + length); + return null; + } + return fileBytes; + } catch (IOException e) { + System.out.println("read file error. " + filePath); + return null; + } + } + + private boolean fileExist(String fullPath) { + File classFile = new File(fullPath); + return classFile.exists() && classFile.isFile(); + } + + private String makeFullPath(String clzPath, String childDirectory) { + if (clzPath.endsWith("/") || clzPath.endsWith("\\")) { + return clzPath + childDirectory + CLASS_SUFFIX; + } else { + return clzPath + "/" + childDirectory + CLASS_SUFFIX; + } + } + + public void addClassPath(String path) { + if (!clzPaths.contains(path)) { + clzPaths.add(path); + } + } + + public String getClassPath() { + return SEMICOLON_JOINER.join(clzPaths); + } + +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/week4/jvm/test/EmployeeV1.java b/group01/280646174/basic/src/main/java/com/coding2017/week4/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..7d0419dc77 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/week4/jvm/test/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.coding2017.week4.jvm.test; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group01/280646174/basic/src/main/java/com/coding2017/week4/lru/LRUPageFrame.java b/group01/280646174/basic/src/main/java/com/coding2017/week4/lru/LRUPageFrame.java new file mode 100644 index 0000000000..55039dda56 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/week4/lru/LRUPageFrame.java @@ -0,0 +1,131 @@ +package com.coding2017.week4.lru; + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin + * + */ +public class LRUPageFrame { + private static class Node { + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + private int size; // 当前个数 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + size = 0; + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + Node node = query(pageNum); + if (node == null) { + Node newNode = new Node(); + newNode.pageNum = pageNum; + accessNotExist(newNode); + } else { + accessExist(node); + } + } + + private void accessExist(Node node) { + removeNode(node); + addFirst(node); + } + + /** + * 此处没有要求传入的node的prev和next, 所以需要自己处理 + * + * @param node + */ + private void addFirst(Node node) { + node.prev = null; + node.next = null; + if (first == null) { + first = node; + last = node; + } else { + first.prev = node; + node.next = first; + first = node; + } + size++; + } + + /** + * 需要考虑删除的节点是头结点, 或尾节点的情况 + */ + private void removeNode(Node node) { + if (node.prev == null) { + first = node.next; + } + if (node.next == null) { + last = node.prev; + } + if (node.prev != null) { + node.prev.next = node.next; + } + if (node.next != null) { + node.next.prev = node.prev; + } + size--; + } + + /** + * 如果已经满了, 则挤出去一个, 然后追加 + * + * @param node + */ + private void accessNotExist(Node node) { + if (size == capacity) { + removeLast(); + } + addFirst(node); + } + + private void removeLast() { + last.prev.next = null; + last = last.prev; + size--; + } + + private Node query(int pageNum) { + for (Node node = first; node != null; node = node.next) { + if (pageNum == node.pageNum) { + return node; + } + } + return null; + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group01/280646174/basic/src/test/java/com/coding2017/week4/jvm/loader/ClassFileLoaderTest.java b/group01/280646174/basic/src/test/java/com/coding2017/week4/jvm/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..a9c118e20b --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/week4/jvm/loader/ClassFileLoaderTest.java @@ -0,0 +1,81 @@ +package com.coding2017.week4.jvm.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 2017/4/3. + */ +public class ClassFileLoaderTest { + + static String path1 = "/Users/kaitao.li/code/study/coding2017/group01/280646174/basic/target/classes"; + static String path2 = "C:\\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coding2017.week4.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1068, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coding2017.week4.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/week4/lru/LRUPageFrameTest.java b/group01/280646174/basic/src/test/java/com/coding2017/week4/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..ede685b83c --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/week4/lru/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding2017.week4.lru; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 2017/4/3. + */ +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group01/378213871/.classpath b/group01/378213871/.classpath index d815a5f517..695129670d 100644 --- a/group01/378213871/.classpath +++ b/group01/378213871/.classpath @@ -3,6 +3,6 @@ - + diff --git a/group01/378213871/src/com/coderising/week03/basic/LinkedList.java b/group01/378213871/src/com/coderising/week03/basic/LinkedList.java new file mode 100644 index 0000000000..7602a2f11e --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/basic/LinkedList.java @@ -0,0 +1,390 @@ +package com.coderising.week03.basic; + +import java.util.NoSuchElementException; +import java.util.Stack; + +import com.coding.basic.week01.Iterator; +import com.coding.basic.week01.List; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + size = 0; + head = null; + } + + public void add(Object o){ + Node node = new Node(o); + if(head == null) { + head = node; + } else { + //p为游标 从头遍历到尾 + Node p = head; + while(p.next != null) { + p = p.next; + } + p.next = node; + } + size++; + } + public void add(int index , Object o){ + //判断链表不为空链表 + if(head != null) { + Node p = head; + int k = 0; + //扫描单链表查找第index-1个节点 + while(k < index-1 && p.next != null) { + k++; + p = p.next; + } + //判断是否找到第index-1个节点 + if(p != null) { + Node node = new Node(o); + node.next = p.next; + p.next = node; + } + size++; + } + } + public Object get(int index){ + if(index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } else { + Node p = head; + int k = 0; + while(k < index && p.next != null) { + k++; + p = p.next; + } + return p.data; + } + } + public Object remove(int index){ + if(index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + if(head == null) { + return null; + } + if(index == 0) { + head = head.next; + size--; + return head.data; + } else { + if(head != null) { + int k = 0; + Node p = head; + while(k < index -1 && p != null) { + k++; + p = p.next; + } + Node pn = p.next; + if(pn != null) { + p.next = pn.next; + size--; + return pn.data; + } + } + } + return null; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node = new Node(o); + if(head == null) { + head = node; + } else { + Node p = head; + while(p.next != null) { + p = p.next; + } + p.next = node; + } + size++; + } + public Object removeFirst(){ + if(head == null) { + throw new NoSuchElementException(); + } + Node node = head; + head = node.next; + size--; + return node.data; + } + public Object removeLast(){ + if(head == null) { + throw new NoSuchElementException(); + } else { + Node p = head; + int k = 0; + while(k < size-1 && p.next != null ) { + k++; + p = p.next; + } + Node last = p.next; + p.next = null; + size--; + return last.data; + } + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + private Node(Object o) { + this.data = o; + this.next = null; + } + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(null == head || null == head.next) { + return; + } + Stack s = new Stack(); + + Node currentNode = head; + while(currentNode != null) { + + s.push(currentNode); + + Node nextNode = currentNode.next; + currentNode.next = null; //把链接断开 + currentNode = nextNode; + } + + head = s.pop(); + + currentNode = head; + while(!s.isEmpty()) { + Node nextNode = s.pop(); + currentNode.next = nextNode; + currentNode = nextNode; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int num = size/2; + for(int i = 0; i < num; i++) { + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + if(i < 0 || i >= size) { + throw new IndexOutOfBoundsException(); + } + + int len = size-i>=length ? length :size-i; + + int k = 0; + while(k < len) { + remove(i); + k++; + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + + int[] arr = new int[list.size()]; + + for(int i = 0; i < list.size(); i++) { + arr[i] = (int) this.get((int) list.get(i)); + } + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + for (int i = 0; i < list.size(); i++) { + this.remove(list.get(i)); + } + } + + /** + * 传入数据删除节点 + * @param obj + */ + public void remove(Object obj) { + if(head==null) { + throw new RuntimeException("LinkedList is empty!"); + } + //如果要删除的节点是第一个,则把下一个节点赋值给第一个节点 + if(head.data.equals(obj)){ + head = head.next; + size--; + } else { + Node pre=head; //上一节点 + Node cur=head.next; //当前节点 + while(cur != null) { + if(cur.data.equals(obj)){ + pre.next=cur.next; + size--; + } + pre=pre.next; + cur=cur.next; + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(head == null) { + throw new RuntimeException("LinkedList is empty!"); + } + + Node pre = head; + Node cur = head; + while(cur.next != null) { + cur = cur.next; + Object data = pre.data; + while(cur.data == data) { + if(cur.next == null) { + pre.next = null; + break; + } + pre.next = cur.next; + size--; + cur = cur.next; + if(cur == null) { + break; + } + } + pre = pre.next; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(head == null) { + return; + } + + Node node = head; + int start = -1; + int end = -1; + int i = 0; + while(node != null) { + if( (start == -1) && (int)node.data <= min) { + start = i; + } + if( (int)node.data >= max ) { + end = i; + break; + } + node = node.next; + i++; + } + + if(start == -1) { + start = 0; + } + if(end == -1) { + end = size; + } + this.remove(start, end-start); + } + + public String toString() { + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + Node node = head; + while (node != null) { + buffer.append(node.data); + if(node.next != null) { + buffer.append(","); + } + node = node.next; + } + buffer.append("]"); + return buffer.toString(); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + + if(list == null) { + return null; + } + + LinkedList result = new LinkedList(); + + int i1 = 0; + int i2 = 0; + + while( i1 < this.size && i2 < list.size() ) { + int value1 = (int)this.get(i1); + int value2 = (int)list.get(i2); + + if(value1 == value2) { + result.add(value1); + i1++; + i2++; + } else if (value1 < value2) { + i1++; + } else { + i2++; + } + } + return result; + } +} diff --git a/group01/378213871/src/com/coderising/week03/basic/LinkedListEx.java b/group01/378213871/src/com/coderising/week03/basic/LinkedListEx.java new file mode 100644 index 0000000000..f203d68b4c --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/basic/LinkedListEx.java @@ -0,0 +1,250 @@ +package com.coderising.week03.basic; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import com.coding.basic.week01.List; + + +public class LinkedListEx implements List { + + + + private Node head; + + private int size; + + public void add(Object o){ + if (head == null) { + head = new Node(o); + size++; + } else{ + addLast(o); + } + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (index == 0) { + addFirst(o); + } else { + //定义标记节点sentinelNode,标记节点的下一个节点即为要新加的元素 + Node sentinelNode = head; + for (int i = 0; i < index - 1; i++) { + sentinelNode = sentinelNode.next; + } + Node node = new Node(o); + node.next = sentinelNode.next; + sentinelNode.next = node; + size++; + } + } + public Object get(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } else { + Node indexNode = head; + for (int i = 0; i < index; i++) { + indexNode = indexNode.next; + } + return indexNode.data; + } + } + public Object remove(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else { + /** + * sentinelNode是所删除节点的上一个节点; + * indexNode是需要被删除的节点 + */ + Node sentinelNode = head; + Node indexNode = head; + for (int i = 0; i < index - 1; i++) { + sentinelNode = sentinelNode.next; + } + for (int i = 0; i < index; i++) { + indexNode = indexNode.next; + } + Node nextIndexNode = indexNode.next; + sentinelNode.next = nextIndexNode; + indexNode.next = null; + size--; + return indexNode.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + //定义尾节点并通过while循环找到当前链表的尾节点 + Node tailNode = head; + while (tailNode.next != null) { + tailNode = tailNode.next; + } + Node node = new Node(o); + tailNode.next = node; + size++; + } + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node newNode = head; + head = head.next; + size--; + return newNode.data; + } + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node newNode = head; + while (newNode.next.next != null) { + newNode = newNode.next; + } + Node lastNode = newNode.next; + newNode.next = null; + size--; + return lastNode.data; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; // 下一个节点 + + private Node(Object data) { + this.data = data; + next = null; + } + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + //节点逆置后的下一个节点;原头节点变尾节点所以初始为null + Node reverseNode = null; + while (head != null) { + Node temp = head; //临时节点temp存放旧头节点 + head = head.next; //旧头节点的下一个节点变为新头节点 + temp.next = reverseNode; //将reverseNode赋给旧头节点的下一个节点 + reverseNode = temp; //将temp(旧头节点)赋给reverseNode + } + head = reverseNode; + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + int startIndex = size/2; + for(int i = 0; i < startIndex; i++) { + head = head.next; + size--; + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if (i < 0) { + throw new IllegalArgumentException(); + } + //要考虑到超出size的情况 + if (i + length >= size) { + length = size - i; + } + if (i == 0) { + for (int j = 0; j < length; j++) { + head = head.next; + } + size = size - length; + } else { + Node startNode = head; //startNode的下一个节点指向第i个元素 + for (int j = 0; j < i - 1; j++) { + startNode = startNode.next; + } + Node endNode = startNode; + for (int j = 0; j < length; j++) { + endNode = endNode.next; + } + startNode.next = endNode.next; + size = size - length; + } + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedListEx list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedListEx list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedListEx intersection( LinkedListEx list){ + return null; + } +} diff --git a/group01/378213871/src/com/coderising/week03/basic/LinkedListTest.java b/group01/378213871/src/com/coderising/week03/basic/LinkedListTest.java new file mode 100644 index 0000000000..e0793e0ef1 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/basic/LinkedListTest.java @@ -0,0 +1,202 @@ +package com.coderising.week03.basic; + +import org.junit.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.week03.basic.LinkedList; + + +public class LinkedListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverse() { + LinkedList l = new LinkedList(); + + Assert.assertEquals("[]", l.toString()); + + l.add(1); + l.reverse(); + Assert.assertEquals("[1]", l.toString()); + + l.add(2); + l.add(3); + l.add(4); + + l.reverse(); + Assert.assertEquals("[4,3,2,1]",l.toString()); + } + + @Test + public void testRemoveFirstHalf() { + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.removeFirstHalf(); + Assert.assertEquals("[3,4]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.removeFirstHalf(); + Assert.assertEquals("[3,4,5]", linkedList.toString()); + } + } + + @Test + public void testRemoveIntInt() { + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(0, 2); + Assert.assertEquals("[3,4]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(3, 2); + Assert.assertEquals("[1,2,3]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(2, 2); + Assert.assertEquals("[1,2]", linkedList.toString()); + } + } + @Test + public void testGetElement() { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(6); + Assert.assertArrayEquals(new int[]{101,301,401,601}, linkedList.getElements(list)); + } + + @Test + public void testSubtract() { + LinkedList list1 = new LinkedList(); + list1.add(101); + list1.add(201); + list1.add(301); + list1.add(401); + list1.add(501); + list1.add(601); + list1.add(701); + + LinkedList list2 = new LinkedList(); + + list2.add(101); + list2.add(201); + list2.add(301); + list2.add(401); + list2.add(501); + + list1.subtract(list2); + + Assert.assertEquals("[601,701]", list1.toString()); + } + + + @Test + public void testRemoveDuplicateValues() { + LinkedList list = new LinkedList(); + list.add(1); + list.add(1); + list.add(2); + list.add(2); + list.add(3); + list.add(5); + list.add(5); + list.add(6); + list.removeDuplicateValues(); + + Assert.assertEquals("[1,2,3,5,6]", list.toString()); + } + + + @Test + public void testRemoveRange() { + + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(16); + linkedList.add(16); + linkedList.add(19); + + linkedList.removeRange(10, 19); + Assert.assertEquals("[19]", linkedList.toString()); + } + + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(16); + linkedList.add(16); + linkedList.add(19); + + linkedList.removeRange(10, 14); + Assert.assertEquals("[14,16,16,19]", linkedList.toString()); + } + } + @Test + public void testIntersection() { + LinkedList list1 = new LinkedList(); + list1.add(1); + list1.add(6); + list1.add(7); + + LinkedList list2 = new LinkedList(); + list2.add(2); + list2.add(5); + list2.add(6); + + LinkedList newList = list1.intersection(list2); + Assert.assertEquals("[6]", newList.toString()); + } + +} diff --git a/group01/378213871/src/com/coderising/week03/download/DownloadThread.java b/group01/378213871/src/com/coderising/week03/download/DownloadThread.java new file mode 100644 index 0000000000..845e570798 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/DownloadThread.java @@ -0,0 +1,48 @@ +package com.coderising.week03.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.week03.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + + public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + + try { + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + + byte[] data = conn.read(startPos, endPos); + + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + + file.seek(startPos); + + file.write(data); + + file.close(); + + conn.close(); + + barrier.await(); //等待别的线程完成 + + } catch (Exception e) { + e.printStackTrace(); + } + + } +} diff --git a/group01/378213871/src/com/coderising/week03/download/FileDownloader.java b/group01/378213871/src/com/coderising/week03/download/FileDownloader.java new file mode 100644 index 0000000000..f5a8e0d903 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/FileDownloader.java @@ -0,0 +1,129 @@ +package com.coderising.week03.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.week03.download.api.Connection; +import com.coderising.week03.download.api.ConnectionException; +import com.coderising.week03.download.api.ConnectionManager; +import com.coderising.week03.download.api.DownloadListener; + + +public class FileDownloader { + + private String url; + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int DOWNLOAD_THREAD_NUM = 3; + + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM, new Runnable() { + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + createPlaceHolderFile(this.localFile, length); + + int[][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM, length); + + for(int i=0; i < DOWNLOAD_THREAD_NUM; i++) { + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + + thread.start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + } + + private void createPlaceHolderFile(String fileName, int contentLen) throws IOException { + + RandomAccessFile file = new RandomAccessFile(fileName,"rw"); + + for(int i=0; i < contentLen; i++) { + file.write(0); + } + + file.close(); + } + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + int[][] ranges = new int[threadNum][2]; + + int eachThreadSize = contentLen / threadNum; //每个线程需要下载的文件大小 + int left = contentLen % threadNum; //剩下的归最后一个线程来处理 + + for(int i = 0; i < threadNum; i++) { + int startPos = i * eachThreadSize; + + int endPos = (i +1) * eachThreadSize - 1; + + if((i == (threadNum - 1))) { + endPos += left; + } + ranges[i][0] = startPos; + ranges[i][1] = endPos; + } + + return ranges; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group01/378213871/src/com/coderising/week03/download/FileDownloaderTest.java b/group01/378213871/src/com/coderising/week03/download/FileDownloaderTest.java new file mode 100644 index 0000000000..9f56ec3e92 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.week03.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.week03.download.api.ConnectionManager; +import com.coderising.week03.download.api.DownloadListener; +import com.coderising.week03.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + + FileDownloader downloader = new FileDownloader(url,"/Users/Victor/desktop/test.jpg"); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group01/378213871/src/com/coderising/week03/download/api/Connection.java b/group01/378213871/src/com/coderising/week03/download/api/Connection.java new file mode 100644 index 0000000000..a00b340d6b --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.week03.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group01/378213871/src/com/coderising/week03/download/api/ConnectionException.java b/group01/378213871/src/com/coderising/week03/download/api/ConnectionException.java new file mode 100644 index 0000000000..a51703e6df --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/api/ConnectionException.java @@ -0,0 +1,8 @@ +package com.coderising.week03.download.api; + +public class ConnectionException extends Exception { + public ConnectionException(Exception e) { + super(e); + } + +} diff --git a/group01/378213871/src/com/coderising/week03/download/api/ConnectionManager.java b/group01/378213871/src/com/coderising/week03/download/api/ConnectionManager.java new file mode 100644 index 0000000000..561993f33b --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.week03.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group01/378213871/src/com/coderising/week03/download/api/DownloadListener.java b/group01/378213871/src/com/coderising/week03/download/api/DownloadListener.java new file mode 100644 index 0000000000..02db08f642 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.week03.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group01/378213871/src/com/coderising/week03/download/impl/ConnectionImpl.java b/group01/378213871/src/com/coderising/week03/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..159f368ab2 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/impl/ConnectionImpl.java @@ -0,0 +1,82 @@ +package com.coderising.week03.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +import com.coderising.week03.download.api.Connection; +import com.coderising.week03.download.api.ConnectionException; + +class ConnectionImpl implements Connection{ + + URL url; + static final int BUFFER_SIZE = 1024; + + ConnectionImpl(String _url) throws ConnectionException { + try { + url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); + + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream is = httpConn.getInputStream(); + + byte[] buff = new byte[BUFFER_SIZE]; + + int totalLen = endPos -startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while(baos.size() < totalLen) { + + int len = is.read(buff); + if (len < 0) { + break; + } + baos.write(buff,0, len); + } + + if (baos.size() > totalLen) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group01/378213871/src/com/coderising/week03/download/impl/ConnectionManagerImpl.java b/group01/378213871/src/com/coderising/week03/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..38e3028e5b --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.week03.download.impl; + +import com.coderising.week03.download.api.Connection; +import com.coderising.week03.download.api.ConnectionException; +import com.coderising.week03.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group01/378213871/src/com/coderising/week03/download/test/ConnectionTest.java b/group01/378213871/src/com/coderising/week03/download/test/ConnectionTest.java new file mode 100644 index 0000000000..8a42668358 --- /dev/null +++ b/group01/378213871/src/com/coderising/week03/download/test/ConnectionTest.java @@ -0,0 +1,53 @@ +package com.coderising.week03.download.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.week03.download.api.Connection; +import com.coderising.week03.download.api.ConnectionManager; +import com.coderising.week03.download.impl.ConnectionManagerImpl; + +import junit.framework.Assert; + +public class ConnectionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testContentLength() throws Exception { + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception { + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + //测试不充分,未断言内容是否正确 + } + +} diff --git a/group01/765324639/src/zavier/week04/coderising/jvm/loader/ClassFileLoader.java b/group01/765324639/src/zavier/week04/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..4490da73ab --- /dev/null +++ b/group01/765324639/src/zavier/week04/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,72 @@ +package zavier.week04.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws ClassNotFoundException { + File classFile = getClassFile(className); + return readFileToByteArray(classFile); + } + + private File getClassFile(String className) throws ClassNotFoundException { + File classFile = null; + String classFilePath = convertClassNameToFilePath(className); + for (String path : clzPaths) { + File file = new File(path, classFilePath); + if (file.exists()) { + classFile = file; + break; + } + } + + if (classFile == null) { // 文件不存在 + throw new ClassNotFoundException(); + } + return classFile; + } + + private byte[] readFileToByteArray(File classFile) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (InputStream in = new FileInputStream(classFile);) { + byte[] data = new byte[1024]; + int len = -1; + while ((len = in.read(data)) != -1) { + out.write(data, 0, len); + } + } catch (FileNotFoundException e) { // 调用此函数时,已经确定文件是存在的 + e.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } + return out.toByteArray(); + } + + private String convertClassNameToFilePath(String fillClassName) { + return fillClassName.replace(".", "\\") + ".class"; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath() { + return String.join(";", clzPaths); + } + + + +} diff --git a/group01/765324639/src/zavier/week04/coderising/jvm/test/ClassFileloaderTest.java b/group01/765324639/src/zavier/week04/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..72c4c24127 --- /dev/null +++ b/group01/765324639/src/zavier/week04/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,90 @@ +package zavier.week04.coderising.jvm.test; + +import java.io.File; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week04.coderising.jvm.loader.ClassFileLoader; + + + +public class ClassFileloaderTest { + + + static String path1 = new File(".", "bin").getAbsolutePath(); + static String path2 = "C:\\temp"; + + static String className = EmployeeV1.class.getName(); + + @Before + public void setUp() throws Exception {} + + @After + public void tearDown() throws Exception {} + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() throws ClassNotFoundException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1076, byteCodes.length); + + } + + @Test(expected = ClassNotFoundException.class) + public void testClassNotFindException() throws ClassNotFoundException { + ClassFileLoader loader = new ClassFileLoader(); + loader.readBinaryCode(className); + } + + + @Test + public void testMagicNumber() throws ClassNotFoundException { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] {byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} diff --git a/group01/765324639/src/zavier/week04/coderising/jvm/test/EmployeeV1.java b/group01/765324639/src/zavier/week04/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..28e4981fbe --- /dev/null +++ b/group01/765324639/src/zavier/week04/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,31 @@ +package zavier.week04.coderising.jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} diff --git a/group01/765324639/src/zavier/week04/coderising/linklist/LRUPageFrame.java b/group01/765324639/src/zavier/week04/coderising/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..46a61f19aa --- /dev/null +++ b/group01/765324639/src/zavier/week04/coderising/linklist/LRUPageFrame.java @@ -0,0 +1,177 @@ +package zavier.week04.coderising.linklist; + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + Node prev; + Node next; + int pageNum; + + Node(int pageNum) { + this.pageNum = pageNum; + } + } + + private int capacity; + private int size; + + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + if (isEmpty()) { + initFirstNode(pageNum); + return; + } + + if (contains(pageNum)) { + moveToFirst(pageNum); + } else { + insert(pageNum); + } + } + + private void initFirstNode(int pageNum) { + Node node = new Node(pageNum); + first = last = node; + size++; + } + + private void moveToFirst(int pageNum) { + if (isFirst(pageNum)) { + return; + } + if (isLast(pageNum)) { + moveLastToFirst(); + } else { + moveMidToFirst(pageNum); + } + } + + private void moveLastToFirst() { + Node temp = last; + + removeLast(); + + addToFirst(temp); + } + + private void addToFirst(Node temp) { + temp.next = first; + first.prev = temp; + first = temp; + size++; + } + + private void moveMidToFirst(int pageNum) { + Node node = removeMidNode(pageNum); + addToFirst(node); + } + + private Node removeMidNode(int pageNum) { + Node temp = getNode(pageNum); + temp.prev.next = temp.next; + temp.next.prev = temp.prev; + + temp.next = null; + temp.prev = null; + + size--; + + return temp; + } + + private void insert(int pageNum) { + if (isFill()) { + removeLast(); + } + insertToFirst(pageNum); + } + + private void removeLast() { + last = last.prev; + last.next = null; + size--; + } + + private void insertToFirst(int pageNum) { + Node node = new Node(pageNum); + + addToFirst(node); + } + + private boolean contains(int pageNum) { + return indexOf(pageNum) != -1; + } + + private int indexOf(int num) { + Node temp = first; + for (int i = 0; i < size; i++) { + if (temp.pageNum == num) { + return i; + } + temp = temp.next; + } + return -1; + } + + private Node getNode(int pageNum) { + Node temp; + temp = first; + for (int i = 0; i < size; i++) { + if (temp.pageNum == pageNum) { + break; + } + temp = temp.next; + } + return temp; + } + + private boolean isEmpty() { + return size == 0; + } + + private boolean isFirst(int pageNum) { + return first.pageNum == pageNum; + } + + private boolean isLast(int pageNum) { + return last.pageNum == pageNum; + } + + private boolean isFill() { + return size == capacity; + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group01/765324639/src/zavier/week04/coderising/linklist/LRUPageFrameTest.java b/group01/765324639/src/zavier/week04/coderising/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..d022f48144 --- /dev/null +++ b/group01/765324639/src/zavier/week04/coderising/linklist/LRUPageFrameTest.java @@ -0,0 +1,30 @@ +package zavier.week04.coderising.linklist; + +import org.junit.Assert; +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group01/895457260/code/src/algorithm/test/ArrayUtilTest.java b/group01/895457260/code/src/algorithm/test/ArrayUtilTest.java deleted file mode 100644 index 85f04f3b31..0000000000 --- a/group01/895457260/code/src/algorithm/test/ArrayUtilTest.java +++ /dev/null @@ -1,234 +0,0 @@ -package algorithm.test; - -import algorithm.ArrayUtil; -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -/** - * ArrayUtil Tester. - * - * @author - * @version 1.0 - * @since
二月 27, 2017
- */ -public class ArrayUtilTest { - ArrayUtil util = new ArrayUtil(); - - /** - * Method: reverseArray(int[] origin) - */ - @Test - public void testReverseArray() throws Exception { -//TODO: Test goes here... - int[][] arrays = { - {7, 9, 30, 3}, - {7, 9, 30, 3, 4}, - {5}, - {} - }; - for (int[] a : arrays) { - util.reverseArray(a); - } - - int[][] result = { - {3, 30, 9, 7}, - {4, 3, 30, 9, 7}, - {5}, - {} - }; - Assert.assertArrayEquals(arrays, result); - } - - /** - * Method: removeZero(int[] oldArray) - */ - @Test - public void testRemoveZero() throws Exception { -//TODO: Test goes here... - int[][] arrays = { - {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}, - {6, 6, 3, 5, 4}, - {0, 0, 0}, - {} - }; - - for (int i = 0; i < arrays.length; ++i) { - arrays[i] = util.removeZero(arrays[i]); - } - - int[][] result = { - {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, - {6, 6, 3, 5, 4}, - {}, - {} - }; - Assert.assertArrayEquals(arrays, result); - } - - /** - * Method: merge(int[] array1, int[] array2) - */ - @Test - public void testMerge() throws Exception { -//TODO: Test goes here... - int[][] arrays1 = { - {3, 5, 7, 8}, - {2, 3, 4}, - {1, 2, 3, 3, 4, 5}, - {1, 2, 2}, - {}, - {} - }; - int[][] arrays2 = { - {4, 5, 6, 7}, - {6, 7, 8, 9, 9}, - {4, 4, 5, 7}, - {}, - {2, 2, 3}, - {} - }; - - int[][] merged = new int[arrays1.length][]; - for (int i = 0; i < arrays1.length; ++i) { - merged[i] = util.merge(arrays1[i], arrays2[i]); - } - - int[][] result = { - {3, 4, 5, 6, 7, 8}, - {2, 3, 4, 6, 7, 8, 9}, - {1, 2, 3, 4, 5, 7}, - {1, 2}, - {2, 3}, - {} - }; - Assert.assertArrayEquals(merged, result); - } - - /** - * Method: grow(int [] oldArray, int size) - */ - @Test - public void testGrow() throws Exception { -//TODO: Test goes here... - int[][] arrays = { - {2, 3, 6}, - {}, - {1} - }; - - int[] size = {3, 3, 0}; - - for (int i = 0; i < arrays.length; ++i) { - arrays[i] = util.grow(arrays[i], size[i]); - } - - int[][] result = { - {2, 3, 6, 0, 0, 0}, - {0, 0, 0}, - {1} - }; - Assert.assertArrayEquals(arrays, result); - } - - /** - * Method: fibonacci(int max) - */ - @Test - public void testFibonacci() throws Exception { -//TODO: Test goes here... - int[] max = {0, 1, 2, 15}; - - int[][] arrays = new int[max.length][]; - - for (int i = 0; i < arrays.length; ++i) { - arrays[i] = util.fibonacci(max[i]); - } - - int[][] result = { - {}, - {}, - {1, 1}, - {1, 1, 2, 3, 5, 8, 13} - }; - Assert.assertArrayEquals(arrays, result); - } - - /** - * Method: getPrimes(int max) - */ - @Test - public void testGetPrimes() throws Exception { -//TODO: Test goes here... - int[] max = {0, 1, 2, 3, 11}; - - int[][] arrays = new int[max.length][]; - - for (int i = 0; i < arrays.length; ++i) { - arrays[i] = util.getPrimes(max[i]); - } - - int[][] result = { - {}, - {}, - {}, - {2}, - {2, 3, 5, 7} - }; - Assert.assertArrayEquals(arrays, result); - } - - /** - * Method: getPerfectNumbers(int max) - */ - @Test - public void testGetPerfectNumbers() throws Exception { -//TODO: Test goes here... - int[] max = {0, 6, 7, 496, 497}; - - int[][] arrays = new int[max.length][]; - - for (int i = 0; i < arrays.length; ++i) { - arrays[i] = util.getPerfectNumbers(max[i]); - } - - int[][] result = { - {}, - {}, - {6}, - {6, 28}, - {6, 28, 496} - }; - Assert.assertArrayEquals(arrays, result); - } - - /** - * Method: join(int[] array, String seperator) - */ - @Test - public void testJoin() throws Exception { -//TODO: Test goes here... - int[][] arrays = { - {3, 8, 9}, - {5}, - {6, 6}, - {} - }; - - String[] sep = {"-", "**", "", "---"}; - String[] joined = new String[arrays.length]; - - for (int i = 0; i < arrays.length; ++i) { - joined[i] = util.join(arrays[i], sep[i]); - } - - String[] result = { - "3-8-9", - "5", - "66", - "" - }; - Assert.assertArrayEquals(joined, result); - } -} diff --git a/group01/895457260/code/src/datastructure/test/ArrayListTest.java b/group01/895457260/code/src/datastructure/test/ArrayListTest.java deleted file mode 100644 index 3a3f742634..0000000000 --- a/group01/895457260/code/src/datastructure/test/ArrayListTest.java +++ /dev/null @@ -1,152 +0,0 @@ -package datastructure.test; - -import datastructure.basic.ArrayList; -import datastructure.basic.Iterator; -import datastructure.basic.List; -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -/** - * ArrayList Tester. - * - * @author - * @version 1.0 - * @since
二月 24, 2017
- */ -public class ArrayListTest { - - @Before - public void before() throws Exception { - } - - @After - public void after() throws Exception { - } - - protected final List getList() { - List list = createList(); - init(list); - return list; - } - - List createList() { - return new ArrayList(5); - } - - private void init(List list) { - for (int i = 1; i <= 5; ++i) { - list.add(i); - } - } - - protected final Object[] toArray(List list) { - Object[] array = new Object[list.size()]; - Iterator iterator = list.iterator(); - int pos = 0; - while (iterator.hasNext()) { - array[pos++] = iterator.next(); - } - return array; - } - - /** - * Method: add(Object o) - */ - @Test - public void testAddO() throws Exception { -//TODO: Test goes here... - List list = getList(); - for (int i = 6; i <= 10; ++i) { - list.add(i); - } - Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - Assert.assertEquals(list.size(), 10); - } - - /** - * Method: add(int index, Object o) - */ - @Test - public void testAddForIndexO() throws Exception { -//TODO: Test goes here... - List list = getList(); - int nowSize = list.size(); - int[] indexes = {nowSize + 1, -1, nowSize, nowSize, 0, 1}; - Object[] values = {0, 0, 300, 400, 100, 200}; - boolean[] exceptions = new boolean[indexes.length]; - for (int i = 0; i < indexes.length; ++i) { - try { - list.add(indexes[i], values[i]); - } catch (IndexOutOfBoundsException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(toArray(list), new Object[]{100, 200, 1, 2, 3, 4, 5, 400, 300}); - Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); - Assert.assertEquals(list.size(), nowSize + 4); - } - - /** - * Method: get(int index) - */ - @Test - public void testGet() throws Exception { -//TODO: Test goes here... - List list = getList(); - int nowSize = list.size(); - int[] indexes = {-1, nowSize, 0, 1, nowSize - 1, nowSize - 2}; - Object[] values = new Object[indexes.length]; - boolean[] exceptions = new boolean[indexes.length]; - for (int i = 0; i < indexes.length; ++i) { - try { - values[i] = list.get(indexes[i]); - } catch (IndexOutOfBoundsException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(values, new Object[]{null, null, 1, 2, 5, 4}); - Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); - Assert.assertEquals(list.size(), nowSize); - } - - /** - * Method: remove(int index) - */ - @Test - public void testRemove() throws Exception { -//TODO: Test goes here... - List list = getList(); - int nowSize = list.size(); - int[] indexes = {-1, nowSize, nowSize - 2, nowSize - 2, 1, 0}; - Object[] values = new Object[indexes.length]; - boolean[] exceptions = new boolean[indexes.length]; - for (int i = 0; i < indexes.length; ++i) { - try { - values[i] = list.remove(indexes[i]); - } catch (IndexOutOfBoundsException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(values, new Object[]{null, null, 4, 5, 2, 1}); - Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); - Assert.assertEquals(list.size(), nowSize - 4); - } - - /** - * Method: iterator() - */ - @Test - public void testIterator() throws Exception { -//TODO: Test goes here... - List list = getList(); - Iterator iterator = list.iterator(); - Object[] values = new Object[list.size()]; - int pos = 0; - while (iterator.hasNext()) { - values[pos++] = iterator.next(); - } - Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5}); - } -} diff --git a/group01/895457260/code/src/datastructure/test/LinkedListTest.java b/group01/895457260/code/src/datastructure/test/LinkedListTest.java deleted file mode 100644 index d82cfa6ed5..0000000000 --- a/group01/895457260/code/src/datastructure/test/LinkedListTest.java +++ /dev/null @@ -1,252 +0,0 @@ -package datastructure.test; - -import datastructure.exception.EmptyListException; -import datastructure.basic.LinkedList; -import datastructure.basic.List; -import org.junit.Assert; -import org.junit.Test; - -/** - * LinkedList Tester. - * - * @author - * @version 1.0 - * @since
二月 24, 2017
- */ -public class LinkedListTest extends ArrayListTest { - - @Override - List createList() { - return new LinkedList(); - } - - /** - * Method: addFirst(Object o) - */ - @Test - public void testAddFirst() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.addFirst(100); - Assert.assertArrayEquals(toArray(list), new Object[]{100, 1, 2, 3, 4, 5}); - } - - /** - * Method: addLast(Object o) - */ - @Test - public void testAddLast() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.addLast(100); - Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 100}); - } - - /** - * Method: removeFirst() - */ - @Test - public void testRemoveFirst() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - int count = list.size() + 2; - Object[] values = new Object[count]; - boolean[] exceptions = new boolean[count]; - for (int i = 0; i < count; ++i) { - try { - values[i] = list.removeFirst(); - } catch (EmptyListException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); - Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); - Assert.assertArrayEquals(toArray(list), new Object[0]); - } - - /** - * Method: removeLast() - */ - @Test - public void testRemoveLast() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - int count = list.size() + 2; - Object[] values = new Object[count]; - boolean[] exceptions = new boolean[count]; - for (int i = 0; i < count; ++i) { - try { - values[i] = list.removeLast(); - } catch (EmptyListException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); - Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); - Assert.assertArrayEquals(toArray(list), new Object[0]); - } - - /** - * - * Method: reverse() - * - */ - @Test - public void testReverse() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.reverse(); - Assert.assertArrayEquals(toArray(list), new Object[] {5, 4, 3, 2, 1}); - } - - /** - * - * Method: removeFirstHalf() - * - */ - @Test - public void testRemoveFirstHalf() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.removeFirstHalf(); - Assert.assertArrayEquals(toArray(list), new Object[] {3, 4, 5}); - } - - /** - * - * Method: remove(int i, int length) - * - */ - @Test - public void testRemoveForILength() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.remove(1, 3); - Assert.assertArrayEquals(toArray(list), new Object[] {1, 5}); - } - - /** - * - * Method: getElements(LinkedList list) - * - */ - @Test - public void testGetElements() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - LinkedList indexList = new LinkedList(); - for (int i = 0; i < 3; ++i) { - indexList.add(2 * i); - } - Object[] elements = list.getElements(indexList); - Assert.assertArrayEquals(elements, new Object[] {1, 3, 5}); - } - - /** - * - * Method: subtract(LinkedList list) - * - */ - @Test - public void testSubtract() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - LinkedList removeList = new LinkedList(); - for (int i = 0; i < 3; ++i) { - removeList.add(2 * i); - } - list.subtract(removeList); - Assert.assertArrayEquals(toArray(list), new Object[] {1, 3, 5}); - } - - /** - * - * Method: removeDuplicateValues() - * - */ - @Test - public void testRemoveDuplicateValues() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.add(5); - list.add(6); - list.add(8); - list.add(8); - list.add(9); - list.removeDuplicateValues(); - Assert.assertArrayEquals(toArray(list), new Object[] {1, 2, 3, 4, 5, 6, 8, 9}); - } - - /** - * - * Method: removeRange(int min, int max) - * - */ - @Test - public void testRemoveRange() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - list.removeRange(2, 5); - Assert.assertArrayEquals(toArray(list), new Object[] {1, 2, 5}); - } - - /** - * - * Method: intersection(LinkedList list) - * - */ - @Test - public void testIntersection() throws Exception { -//TODO: Test goes here... - LinkedList list = (LinkedList) getList(); - LinkedList list1 = new LinkedList(); - for (int i = 0; i < 4; ++i) { - list1.add(2 * i); - } - LinkedList result = list.intersection(list1); - Assert.assertArrayEquals(toArray(result), new Object[] {2, 4}); - } - - @Test - public void testMultiMethod() throws Exception { - LinkedList list = (LinkedList) getList(); - LinkedList subtractList = new LinkedList(); - for (int i = 0; i < 3; ++i) { - subtractList.add(2 * i); - } - LinkedList intersectionList = new LinkedList(); - for (int i = 0; i < 4; ++i) { - intersectionList.add(2 * i); - } - - list.reverse(); - list.subtract(subtractList); - Assert.assertArrayEquals(toArray(list), new Object[] {5, 4, 3, 2, 1}); - LinkedList intersection = list.intersection(intersectionList); - Assert.assertArrayEquals(toArray(intersection), new Object[] {}); - list.reverse(); - list.add(6); - list.add(6); - list.add(6); - list.add(7); - list.add(7); - list.add(8); - list.add(9); - list.add(9); - list.add(9); - list.add(10); - list.add(11); - list.removeDuplicateValues(); - Assert.assertArrayEquals(toArray(list), new Object[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); - list.remove(0, 0); - list.remove(0, 2); - Assert.assertArrayEquals(toArray(list), new Object[] {3, 4, 5, 6, 7, 8, 9, 10, 11}); - list.removeRange(0, 3); - list.removeRange(11, 13); - list.removeRange(11, 8); - list.removeRange(8, 11); - Assert.assertArrayEquals(toArray(list), new Object[] {3, 4, 5, 6, 7, 8, 11}); - list.removeFirstHalf(); - Assert.assertArrayEquals(toArray(list), new Object[] {6, 7, 8, 11}); - } -} diff --git a/group01/895457260/code/src/datastructure/test/QueueTest.java b/group01/895457260/code/src/datastructure/test/QueueTest.java deleted file mode 100644 index 5a47f764a9..0000000000 --- a/group01/895457260/code/src/datastructure/test/QueueTest.java +++ /dev/null @@ -1,108 +0,0 @@ -package datastructure.test; - -import datastructure.exception.EmptyQueueException; -import datastructure.basic.Queue; -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -/** - * Queue Tester. - * - * @author - * @version 1.0 - * @since
二月 24, 2017
- */ -public class QueueTest { - - @Before - public void before() throws Exception { - } - - @After - public void after() throws Exception { - } - - private Queue getQueue() { - Queue queue = new Queue(5); - for (int i = 1; i <= 5; ++i) { - queue.enQueue(i); - } - return queue; - } - - private void assertQueue(Queue queue, Object[] actual) { - Class clazz = Queue.class; - Object[] array = null; - int head = 0; - int rear = 0; - Method mapIndex = null; - try { - Field arrayField = clazz.getDeclaredField("array"); - Field headField = clazz.getDeclaredField("head"); - Field rearField = clazz.getDeclaredField("rear"); - mapIndex = clazz.getDeclaredMethod("mapIndex", int.class); - arrayField.setAccessible(true); - headField.setAccessible(true); - rearField.setAccessible(true); - mapIndex.setAccessible(true); - array = (Object[]) arrayField.get(queue); - head = (int) headField.get(queue); - rear = (int) rearField.get(queue); - } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { - e.printStackTrace(); - } - int size = queue.size(); - Object[] excepted = new Object[size]; - int pos = 0; - try { - while (head < rear) { - excepted[pos++] = array[(int) mapIndex.invoke(queue, head)]; - head++; - } - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } - Assert.assertArrayEquals(excepted, actual); - } - - /** - * Method: enQueue(Object o) - */ - @Test - public void testEnQueue() throws Exception { -//TODO: Test goes here... - Queue queue = getQueue(); - for (int i = 6; i <= 10; ++i) { - queue.enQueue(i); - } - assertQueue(queue, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - } - - /** - * Method: deQueue() - */ - @Test - public void testDeQueue() throws Exception { -//TODO: Test goes here... - Queue queue = getQueue(); - int count = queue.size() + 2; - Object[] values = new Object[count]; - boolean[] exceptions = new boolean[count]; - for (int i = 0; i < count; ++i) { - try { - values[i] = queue.deQueue(); - } catch (EmptyQueueException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); - Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); - assertQueue(queue, new Object[0]); - } -} diff --git a/group01/895457260/code/src/datastructure/test/StackTest.java b/group01/895457260/code/src/datastructure/test/StackTest.java deleted file mode 100644 index 24d7db58d8..0000000000 --- a/group01/895457260/code/src/datastructure/test/StackTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package datastructure.test; - -import datastructure.basic.*; -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import java.lang.reflect.Field; -import java.util.EmptyStackException; - -/** - * Stack Tester. - * - * @author - * @version 1.0 - * @since
二月 24, 2017
- */ -public class StackTest { - - @Before - public void before() throws Exception { - } - - @After - public void after() throws Exception { - } - - private Stack getStack() { - Stack stack = new Stack(); - for (int i = 1; i <= 5; ++i) { - stack.push(i); - } - return stack; - } - - private void assertStack(Stack stack, Object[] actual) { - Class clazz = Stack.class; - ArrayList elementData = null; - try { - Field field = clazz.getDeclaredField("elementData"); - field.setAccessible(true); - elementData = (ArrayList) field.get(stack); - } catch (NoSuchFieldException | IllegalAccessException e) { - e.printStackTrace(); - } - - Object[] excepted = null; - if (elementData != null) { - int size = stack.size(); - excepted = new Object[size]; - for (int i = 0; i < size; ++i) { - excepted[i] = elementData.get(i); - } - } - Assert.assertArrayEquals(excepted, actual); - } - - /** - * Method: push(Object o) - */ - @Test - public void testPush() throws Exception { -//TODO: Test goes here... - Stack stack = getStack(); - for (int i = 6; i <= 10; ++i) { - stack.push(i); - } - assertStack(stack, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - } - - /** - * Method: pop() - */ - @Test - public void testPop() throws Exception { -//TODO: Test goes here... - Stack stack = getStack(); - int count = stack.size() + 2; - Object[] values = new Object[count]; - boolean[] exceptions = new boolean[count]; - for (int i = 0; i < count; ++i) { - try { - values[i] = stack.pop(); - } catch (EmptyStackException e) { - exceptions[i] = true; - } - } - Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); - Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); - assertStack(stack, new Object[0]); - } -} diff --git a/group01/895457260/code/src/download/Config.java b/group01/895457260/code/src/download/Config.java deleted file mode 100644 index a104355cef..0000000000 --- a/group01/895457260/code/src/download/Config.java +++ /dev/null @@ -1,15 +0,0 @@ -package download; - -import java.io.File; - -/** - * Created by Haochen on 2017/3/6. - * - */ -public class Config { - /** - * 保存下载文件的目录 - */ - public static File targetDirectory = new File("download/"); - public static File tempDirectory = new File(targetDirectory, "temp/"); -} diff --git a/group01/895457260/code/src/download/DownloadThread.java b/group01/895457260/code/src/download/DownloadThread.java deleted file mode 100644 index 97b9d66a51..0000000000 --- a/group01/895457260/code/src/download/DownloadThread.java +++ /dev/null @@ -1,135 +0,0 @@ -package download; - -import download.api.Connection; -import download.api.DownloadException; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; - -public class DownloadThread extends Thread { - private Connection conn; - private int startPos; - private int endPos; - - private File targetFile; - private OnCompleteListener onComplete; - private OnFailListener onFail; - - /** - * - * @param conn url连接 - * @param startPos 此线程会从url所指向文件的startPos处开始下载 - * @param endPos 此线程会在url所指向文件的endPos处停止下载 - * @param targetFile 保存下载内容的文件 - * @param onComplete 下载成功后自动调用 - * @param onFail 下载失败后自动调用 - * - * @see OnCompleteListener#onComplete() - * @see OnFailListener#onFail() - */ - DownloadThread(Connection conn, int startPos, int endPos, File targetFile, - OnCompleteListener onComplete, OnFailListener onFail) { - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - this.targetFile = targetFile; - this.onComplete = onComplete; - this.onFail = onFail; - } - - @Override - public void run() { - int maxFailCount = 5; - int failCount = 0; - boolean success = false; - while (!success) { - try { - success = tryDownload(); - } catch (DownloadException e) { - if (failCount < maxFailCount) { - failCount++; - retry(); - } else { - break; - } - } - } - callback(success); - } - - private void callback(boolean success) { - if (success) { - if (onComplete != null) { - onComplete.onComplete(); - } - } else { - if (onFail != null) { - onFail.onFail(); - } - } - } - - private boolean tryDownload() throws DownloadException { - FileOutputStream fos = null; - try { - fos = new FileOutputStream(targetFile); - download(fos); - return true; - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - throw new DownloadException(); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - return false; - } - - private void retry() { - try { - recreateFile(targetFile); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - - private void recreateFile(File file) throws IOException { - file.delete(); - file.createNewFile(); - } - - private void download(FileOutputStream fos) throws IOException { - int bufSize = 1024; - int from = startPos; - while (from < endPos) { - int to = Math.min(from + bufSize, endPos); - byte[] buf = conn.read(from, to); - from = to; - fos.write(buf); - fos.flush(); - } - } - - public interface OnCompleteListener { - /** - * 下载成功后自动调用此方法 - */ - void onComplete(); - } - - public interface OnFailListener { - /** - * 下载失败后自动调用此方法 - */ - void onFail(); - } -} diff --git a/group01/895457260/code/src/download/FileDownloader.java b/group01/895457260/code/src/download/FileDownloader.java deleted file mode 100644 index f72d371788..0000000000 --- a/group01/895457260/code/src/download/FileDownloader.java +++ /dev/null @@ -1,209 +0,0 @@ -package download; - -import download.api.*; - -import java.io.*; -import java.util.Date; - -/** - * TODO: - */ -public class FileDownloader { - private String url; - private DownloadListener listener; - private ConnectionManager manager; - private boolean failed = false; - - private final int[] completedThreadCount = new int[1]; - - /** - * 下载一个url指向的文件,下载目录见 Config - * - * @see Config#targetDirectory - * @see #execute() - */ - public FileDownloader(String url) { - this.url = url; - } - - /** - * 开始下载 - * 调用这个方法前,先调用{@link #setConnectionManager(ConnectionManager)}和{@link #setListener(DownloadListener)} - */ - public void execute() { - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - new Thread(() -> { - initDirectories(); - - int threadCount = 4; - File[] tempFiles = new File[threadCount]; - Connection[] connections = new Connection[threadCount]; - createMultiThread(threadCount, tempFiles, connections); - - waitForComplete(threadCount); - mergeTempFiles(tempFiles); - removeTempFiles(tempFiles); - - for (Connection c : connections) { - if (c != null) { - c.close(); - } - } - if (!failed && listener != null) { - listener.notifyFinished(); - } - }).start(); - } - - private void removeTempFiles(File[] tempFiles) { - for (File tempFile : tempFiles) { - tempFile.delete(); // 只删除临时文件,不删除临时目录 - } - } - - private void mergeTempFiles(File[] tempFiles) { - String[] split = url.replaceAll("/+", "/").split("/"); - File saveFile = new File(Config.targetDirectory, split[split.length - 1]); - FileOutputStream fos = null; - try { - fos = new FileOutputStream(saveFile); - for (File tempFile : tempFiles) { - write(tempFile, fos); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - private void waitForComplete(int threadCount) { - while (completedThreadCount[0] < threadCount) { - synchronized (completedThreadCount) { - if (completedThreadCount[0] < threadCount) { - try { - completedThreadCount.wait(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - } - } - - private void createMultiThread(int threadCount, File[] tempFiles, Connection[] connections) { - for (int i = 0; i < threadCount; ++i) { - File targetFile = new File(Config.tempDirectory, - new Date().getTime() + "_" + i); - tempFiles[i] = targetFile; - - Connection connection = connect(); - if (connection != null) { - connections[i] = connection; - int length = connection.getContentLength(); - int startPos = (int) (1.0 * length / threadCount * i); - int endPos = i == threadCount - 1 ? length : (int) (1.0 * length / threadCount * (i + 1)); - new DownloadThread(connection, startPos, endPos, targetFile, () -> { - synchronized (completedThreadCount) { - completedThreadCount[0]++; - completedThreadCount.notifyAll(); - } - }, () -> { - try { - downloadFailed(connections, tempFiles); - } catch (DownloadException e) { - e.printStackTrace(); - } - }).start(); - } - } - } - - private Connection connect() { - Connection conn = null; - try { - conn = manager.open(this.url); - } catch (ConnectionException e) { - e.printStackTrace(); - } - return conn; - } - - private void initDirectories() { - if (!Config.targetDirectory.exists()) { - Config.targetDirectory.mkdir(); - } - if (!Config.tempDirectory.exists()) { - Config.tempDirectory.mkdir(); - } - } - - private void downloadFailed(Connection[] connections, File[] tempFiles) throws DownloadException { - for (Connection c : connections) { - c.close(); - } - removeTempFiles(tempFiles); - failed = true; - throw new DownloadException(); - } - - private void write(File inputFile, OutputStream os) { - FileInputStream fis = null; - int bufSize = 1024; - byte[] buf = new byte[bufSize]; - int n; - try { - fis = new FileInputStream(inputFile); - while ((n = fis.read(buf)) != -1) { - os.write(buf, 0, n); - os.flush(); - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - /** - * - * @param listener 下载成功后会调用listener.notifyFinished(),失败则不会调用 - * @see DownloadListener#notifyFinished() - */ - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - /** - * - * @param manager 通过url打开连接 - * @see ConnectionManager#open(String) - */ - public void setConnectionManager(ConnectionManager manager) { - this.manager = manager; - } -} diff --git a/group01/895457260/code/src/download/FileDownloaderTest.java b/group01/895457260/code/src/download/FileDownloaderTest.java deleted file mode 100644 index 0792e6c0ba..0000000000 --- a/group01/895457260/code/src/download/FileDownloaderTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package download; - -import download.FileDownloader; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import download.api.ConnectionManager; -import download.api.DownloadListener; -import download.impl.ConnectionManagerImpl; - -import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - -// String url = "http://localhost:8080/test.jpg"; -// String url = "file:///E:/Video/download/88993.mp4"; -// String url = "file:///E:/Pictures/Clannad/Clannad高清图片/38.jpg"; - String url = "http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } -} diff --git a/group01/895457260/code/src/download/api/Connection.java b/group01/895457260/code/src/download/api/Connection.java deleted file mode 100644 index 48f6946eb5..0000000000 --- a/group01/895457260/code/src/download/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -package download.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - byte[] read(int startPos,int endPos) throws IOException; - /** - * 得到数据内容的长度 - * @return - */ - int getContentLength(); - - /** - * 关闭连接 - */ - void close(); -} diff --git a/group01/895457260/code/src/download/api/ConnectionManager.java b/group01/895457260/code/src/download/api/ConnectionManager.java deleted file mode 100644 index e8ef315639..0000000000 --- a/group01/895457260/code/src/download/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package download.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - Connection open(String url) throws ConnectionException; -} diff --git a/group01/895457260/code/src/download/api/DownloadListener.java b/group01/895457260/code/src/download/api/DownloadListener.java deleted file mode 100644 index 0acf8ea483..0000000000 --- a/group01/895457260/code/src/download/api/DownloadListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package download.api; - -public interface DownloadListener { - /** - * 下载成功后自动调用此方法 - */ - void notifyFinished(); -} diff --git a/group01/895457260/code/src/download/impl/ConnectionImpl.java b/group01/895457260/code/src/download/impl/ConnectionImpl.java deleted file mode 100644 index 74e431a324..0000000000 --- a/group01/895457260/code/src/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,59 +0,0 @@ -package download.impl; - -import java.io.*; -import java.net.URL; -import java.net.URLConnection; - -import download.api.Connection; -import download.api.ConnectionException; - -public class ConnectionImpl implements Connection { - private URLConnection connection; - private InputStream inputStream; - - ConnectionImpl(String url) throws ConnectionException { - try { - init(url); - } catch (IOException e) { - throw new ConnectionException(); - } - } - - private void init(String url) throws IOException { - connection = new URL(url).openConnection(); - inputStream = new BufferedInputStream(connection.getInputStream()); - inputStream.mark(connection.getContentLength()); // 标记在开头 - } - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - inputStream.reset(); // reset回到标记处 - skipBytes(startPos); - byte[] bytes = new byte[endPos - startPos]; - int n = inputStream.read(bytes); - return n == -1 ? new byte[0] : bytes; - } - - @Override - public int getContentLength() { - return connection.getContentLength(); - } - - @Override - public void close() { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - // InputStream.skip(long)实际跳过的字节数经常小于参数值,但不会大于参数值 - private void skipBytes(long n) throws IOException { - while (n > 0) { - n -= inputStream.skip(n); - } - } -} diff --git a/group01/895457260/code/src/download/impl/ConnectionManagerImpl.java b/group01/895457260/code/src/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index be17fa9110..0000000000 --- a/group01/895457260/code/src/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,12 +0,0 @@ -package download.impl; - -import download.api.Connection; -import download.api.ConnectionException; -import download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - @Override - public Connection open(String url) throws ConnectionException { - return new ConnectionImpl(url); - } -} diff --git a/group01/895457260/code/src/litestruts/struts.xml b/group01/895457260/code/src/litestruts/struts.xml deleted file mode 100644 index aad7e3f7fc..0000000000 --- a/group01/895457260/code/src/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group01/895457260/code/src/litestruts/test/StrutsTest.java b/group01/895457260/code/src/litestruts/test/StrutsTest.java deleted file mode 100644 index 0129a8ad31..0000000000 --- a/group01/895457260/code/src/litestruts/test/StrutsTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package litestruts.test; - -import litestruts.Struts; -import litestruts.View; -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} \ No newline at end of file diff --git a/group01/895457260/code/src/algorithm/ArrayUtil.java b/group01/895457260/code/src/main/java/algorithm/ArrayUtil.java similarity index 100% rename from group01/895457260/code/src/algorithm/ArrayUtil.java rename to group01/895457260/code/src/main/java/algorithm/ArrayUtil.java diff --git a/group01/895457260/code/src/main/java/algorithm/lru/LRUPageFrame.java b/group01/895457260/code/src/main/java/algorithm/lru/LRUPageFrame.java new file mode 100644 index 0000000000..96e28c9ba7 --- /dev/null +++ b/group01/895457260/code/src/main/java/algorithm/lru/LRUPageFrame.java @@ -0,0 +1,112 @@ +package algorithm.lru; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + private static class List { + int capacity; + int size; + Node head;// 链表头 + Node rear;// 链表尾 + static class Node { + Node pre; + Node next; + int pageNum; + Node() {} + void clear() { + pre = null; + next = null; + } + } + List(int capacity) { + this.capacity = capacity; + head = new Node(); + rear = new Node(); + head.next = rear; + rear.pre = head; + } + void put(int pageNum) { + Node node = findNode(pageNum); + if (node == null) { + if (size >= capacity) { + remove(); + } + Node n = new Node(); + n.pageNum = pageNum; + n.next = rear; + n.pre = rear.pre; + n.pre.next = n; + rear.pre = n; + size++; + } else { + pullUp(node); + } + } + Node findNode(int pageNum) { + for (Node n = head.next; n != null && n != rear; n = n.next) { + if (n.pageNum == pageNum) { + return n; + } + } + return null; + } + void remove() { + if (size == 0) { + return; + } + List.Node node = head.next; + node.next.pre = head; + head.next = node.next; + node.clear(); + size--; + } + void pullUp(Node node) { + node.next.pre = node.pre; + node.pre.next = node.next; + node.next = rear; + node.pre = rear.pre; + rear.pre.next = node; + rear.pre = node; + } + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = rear.pre; + while(node != null && node != head){ + buffer.append(node.pageNum); + + node = node.pre; + if(node != null){ + buffer.append(","); + } + } + return buffer.length() == 0 ? "" : buffer.substring(0, buffer.length() - 1); + } + } + + private List buf; + private int capacity; + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + buf = new List(capacity); + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + buf.put(pageNum); + } + + @Override + public String toString() { + return buf.toString(); + } +} diff --git a/group01/895457260/code/src/datastructure/basic/ArrayList.java b/group01/895457260/code/src/main/java/datastructure/basic/ArrayList.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/ArrayList.java rename to group01/895457260/code/src/main/java/datastructure/basic/ArrayList.java diff --git a/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java b/group01/895457260/code/src/main/java/datastructure/basic/BinarySortedTree.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/BinarySortedTree.java rename to group01/895457260/code/src/main/java/datastructure/basic/BinarySortedTree.java diff --git a/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java b/group01/895457260/code/src/main/java/datastructure/basic/BinaryTreeNode.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java rename to group01/895457260/code/src/main/java/datastructure/basic/BinaryTreeNode.java diff --git a/group01/895457260/code/src/datastructure/basic/Iterator.java b/group01/895457260/code/src/main/java/datastructure/basic/Iterator.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/Iterator.java rename to group01/895457260/code/src/main/java/datastructure/basic/Iterator.java diff --git a/group01/895457260/code/src/datastructure/basic/LinkedList.java b/group01/895457260/code/src/main/java/datastructure/basic/LinkedList.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/LinkedList.java rename to group01/895457260/code/src/main/java/datastructure/basic/LinkedList.java diff --git a/group01/895457260/code/src/datastructure/basic/List.java b/group01/895457260/code/src/main/java/datastructure/basic/List.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/List.java rename to group01/895457260/code/src/main/java/datastructure/basic/List.java diff --git a/group01/895457260/code/src/datastructure/basic/Queue.java b/group01/895457260/code/src/main/java/datastructure/basic/Queue.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/Queue.java rename to group01/895457260/code/src/main/java/datastructure/basic/Queue.java diff --git a/group01/895457260/code/src/datastructure/basic/Stack.java b/group01/895457260/code/src/main/java/datastructure/basic/Stack.java similarity index 100% rename from group01/895457260/code/src/datastructure/basic/Stack.java rename to group01/895457260/code/src/main/java/datastructure/basic/Stack.java diff --git a/group01/895457260/code/src/datastructure/exception/EmptyListException.java b/group01/895457260/code/src/main/java/datastructure/exception/EmptyListException.java similarity index 100% rename from group01/895457260/code/src/datastructure/exception/EmptyListException.java rename to group01/895457260/code/src/main/java/datastructure/exception/EmptyListException.java diff --git a/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java b/group01/895457260/code/src/main/java/datastructure/exception/EmptyQueueException.java similarity index 100% rename from group01/895457260/code/src/datastructure/exception/EmptyQueueException.java rename to group01/895457260/code/src/main/java/datastructure/exception/EmptyQueueException.java diff --git a/group01/895457260/code/src/main/java/download/Config.java b/group01/895457260/code/src/main/java/download/Config.java new file mode 100644 index 0000000000..1a2abee421 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/Config.java @@ -0,0 +1,21 @@ +package download; + +import java.io.File; + +/** + * Created by Haochen on 2017/3/6. + * + */ +public class Config { + public static final String packageName = "download"; + + /** + * 保存下载文件的目录 + */ + public static File targetDirectory = new File("download/"); + public static File tempDirectory = new File(targetDirectory, "temp/"); + + public static int maxLengthPerThread = 10 * 1024 * 1024; + public static int minThreadCount = 3; + public static int maxThreadCount = 8; +} diff --git a/group01/895457260/code/src/main/java/download/DownloadThread.java b/group01/895457260/code/src/main/java/download/DownloadThread.java new file mode 100644 index 0000000000..fe619d8775 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/DownloadThread.java @@ -0,0 +1,98 @@ +package download; + +import download.api.*; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +public class DownloadThread extends Thread { + private Connection conn; + private File targetFile; + private DownloadCallback callback = new DownloadCallback(); + + /** + * @param conn url连接 + * @param targetFile 保存下载内容的文件 + * @param onComplete 下载成功后自动调用 + * @param onFail 下载失败后自动调用 + * + * @see OnCompleteListener#onComplete() + * @see OnFailListener#onFail() + */ + DownloadThread(Connection conn, File targetFile, + OnCompleteListener onComplete, OnFailListener onFail) { + this.conn = conn; + this.targetFile = targetFile; + callback.setOnComplete(onComplete); + callback.setOnFail(onFail); + } + + @Override + public void run() { + int maxFailCount = 5; + int failCount = 0; + boolean success = false; + while (!success) { + try { + success = tryDownload(); + } catch (DownloadException e) { + if (failCount < maxFailCount) { + failCount++; + retry(); + } else { + break; + } + } + } + callback.callback(success); + } + + private boolean tryDownload() throws DownloadException { + FileOutputStream fos = null; + try { + fos = new FileOutputStream(targetFile); + download(fos); + return true; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + throw new DownloadException(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return false; + } + + private void retry() { + try { + recreateFile(targetFile); + conn.reset(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void recreateFile(File file) throws IOException { + file.delete(); + file.createNewFile(); + } + + private void download(FileOutputStream fos) throws IOException { + int bufSize = 1024; + byte[] buf = new byte[bufSize]; + int len; + while ((len = conn.read(buf)) != -1) { + fos.write(buf, 0, len); + fos.flush(); + } + } +} diff --git a/group01/895457260/code/src/main/java/download/FileDownloader.java b/group01/895457260/code/src/main/java/download/FileDownloader.java new file mode 100644 index 0000000000..a1d4197781 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/FileDownloader.java @@ -0,0 +1,245 @@ +package download; + +import download.api.*; + +import java.io.*; +import java.net.URL; +import java.util.Date; + +/** + * TODO: + */ +public class FileDownloader { + private String url = null; + private int contentLength; + private ConnectionManager manager; + private boolean failed = false; + + private DownloadCallback callback = new DownloadCallback(); + + private final int[] completedThreadCount = new int[1]; + + /** + * 下载一个url指向的文件,下载目录见 Config + * + * @see Config#targetDirectory + * @see #execute() + */ + public FileDownloader(String url) throws IOException { + this.url = url; + this.contentLength = new URL(url).openConnection().getContentLength(); + } + + /** + * 开始下载
+ * 调用这个方法前,先调用以下几个方法:
{@link #setConnectionManager(ConnectionManager)}
+ * {@link #setOnCompleteListener(OnCompleteListener)}
+ * {@link #setOnFailListener(OnFailListener)} + */ + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + new Thread(() -> { + initDirectories(); + + int threadCount; + try { + threadCount = getThreadCount(); + } catch (IOException e) { + e.printStackTrace(); + callback.callback(false); + return; + } + File[] tempFiles = new File[threadCount]; + Connection[] connections = new Connection[threadCount]; + createMultiThread(threadCount, tempFiles, connections); + + waitForComplete(threadCount); + mergeTempFiles(tempFiles); + removeTempFiles(tempFiles); + + for (Connection c : connections) { + if (c != null) { + c.close(); + } + } + callback.callback(true); + }).start(); + } + + private int getThreadCount() throws IOException { + if (this.url.split(":", 2)[0].toLowerCase().startsWith("http")) { + URL url = new URL(this.url); + int length = url.openConnection().getContentLength(); + int count = length / Config.maxLengthPerThread; + if (count < Config.minThreadCount) { + return Config.minThreadCount; + } else if (count > Config.maxThreadCount) { + return Config.maxThreadCount; + } else { + return count; + } + } else { + return 1; + } + } + + private void removeTempFiles(File[] tempFiles) { + for (File tempFile : tempFiles) { + tempFile.delete(); // 只删除临时文件,不删除临时目录 + } + } + + private void mergeTempFiles(File[] tempFiles) { + String[] split = url.replaceAll("/+", "/").split("/"); + File saveFile = new File(Config.targetDirectory, split[split.length - 1]); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(saveFile); + for (File tempFile : tempFiles) { + write(tempFile, fos); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private void waitForComplete(int threadCount) { + while (completedThreadCount[0] < threadCount) { + synchronized (completedThreadCount) { + if (completedThreadCount[0] < threadCount) { + try { + completedThreadCount.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + } + + private void createMultiThread(int threadCount, File[] tempFiles, Connection[] connections) { + for (int i = 0; i < threadCount; ++i) { + File targetFile = new File(Config.tempDirectory, + new Date().getTime() + "_" + i); + tempFiles[i] = targetFile; + + int startPos = (int) (1.0 * contentLength / threadCount * i); + int endPos = i == threadCount - 1 ? contentLength : (int) (1.0 * contentLength / threadCount * (i + 1)); + endPos--; + Connection connection = connect(startPos, endPos); + if (connection != null) { + connections[i] = connection; + new DownloadThread(connection, targetFile, () -> { + synchronized (completedThreadCount) { + completedThreadCount[0]++; + completedThreadCount.notifyAll(); + } + }, () -> { + try { + downloadFailed(connections, tempFiles); + } catch (DownloadException e) { + e.printStackTrace(); + } + }).start(); + } + } + } + + private Connection connect(int startPos, int endPos) { + try { + return manager.open(url, startPos, endPos); + } catch (ConnectionException e) { + e.printStackTrace(); + return null; + } + } + + private void initDirectories() { + if (!Config.targetDirectory.exists()) { + Config.targetDirectory.mkdir(); + } + if (!Config.tempDirectory.exists()) { + Config.tempDirectory.mkdir(); + } + } + + private void downloadFailed(Connection[] connections, File[] tempFiles) throws DownloadException { + for (Connection c : connections) { + c.close(); + } + removeTempFiles(tempFiles); + failed = true; + throw new DownloadException(); + } + + private void write(File inputFile, OutputStream os) { + FileInputStream fis = null; + int bufSize = 1024; + byte[] buf = new byte[bufSize]; + int n; + try { + fis = new FileInputStream(inputFile); + while ((n = fis.read(buf)) != -1) { + os.write(buf, 0, n); + os.flush(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * + * @param listener 下载成功后会调用onComplete.onComplete(),失败则不会调用 + * @see OnCompleteListener#onComplete() + */ + public void setOnCompleteListener(OnCompleteListener listener) { + callback.setOnComplete(listener); + } + + /** + * + * @param listener 下载失败后会调用onFail.onFail() + * @see OnFailListener#onFail() + */ + public void setOnFailListener(OnFailListener listener) { + callback.setOnFail(listener); + } + + /** + * + * @param manager 通过url打开连接 + * @see ConnectionManager#open(String, int, int) + */ + public void setConnectionManager(ConnectionManager manager) { + this.manager = manager; + } +} diff --git a/group01/895457260/code/src/main/java/download/api/Connection.java b/group01/895457260/code/src/main/java/download/api/Connection.java new file mode 100644 index 0000000000..4b58ae145d --- /dev/null +++ b/group01/895457260/code/src/main/java/download/api/Connection.java @@ -0,0 +1,27 @@ +package download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 读取字节 + * @param bytes 存放读出的字节 + * @return 实际读出的字节数 + */ + int read(byte[] bytes) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + int getContentLength(); + + /** + * 恢复到初始状态,可以重新读字节 + */ + void reset() throws IOException; + + /** + * 关闭连接 + */ + void close(); +} diff --git a/group01/895457260/code/src/download/api/ConnectionException.java b/group01/895457260/code/src/main/java/download/api/ConnectionException.java similarity index 100% rename from group01/895457260/code/src/download/api/ConnectionException.java rename to group01/895457260/code/src/main/java/download/api/ConnectionException.java diff --git a/group01/895457260/code/src/main/java/download/api/ConnectionManager.java b/group01/895457260/code/src/main/java/download/api/ConnectionManager.java new file mode 100644 index 0000000000..b88cf3b448 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/api/ConnectionManager.java @@ -0,0 +1,13 @@ +package download.api; + +public interface ConnectionManager { + /** + * 打开一个连接 + * @param url url + * @param startPos 开始位置 + * @param endPos 结束位置 + * @return 打开的连接 + * @throws ConnectionException 参数错误 + */ + Connection open(String url, int startPos, int endPos) throws ConnectionException; +} diff --git a/group01/895457260/code/src/main/java/download/api/DownloadCallback.java b/group01/895457260/code/src/main/java/download/api/DownloadCallback.java new file mode 100644 index 0000000000..13b3bd781f --- /dev/null +++ b/group01/895457260/code/src/main/java/download/api/DownloadCallback.java @@ -0,0 +1,30 @@ +package download.api; + +/** + * Created by Haochen on 2017/3/16. + * TODO: + */ +public class DownloadCallback { + private OnCompleteListener onComplete; + private OnFailListener onFail; + + public void callback(boolean success) { + if (success) { + if (onComplete != null) { + onComplete.onComplete(); + } + } else { + if (onFail != null) { + onFail.onFail(); + } + } + } + + public void setOnComplete(OnCompleteListener onComplete) { + this.onComplete = onComplete; + } + + public void setOnFail(OnFailListener onFail) { + this.onFail = onFail; + } +} diff --git a/group01/895457260/code/src/download/api/DownloadException.java b/group01/895457260/code/src/main/java/download/api/DownloadException.java similarity index 100% rename from group01/895457260/code/src/download/api/DownloadException.java rename to group01/895457260/code/src/main/java/download/api/DownloadException.java diff --git a/group01/895457260/code/src/main/java/download/api/OnCompleteListener.java b/group01/895457260/code/src/main/java/download/api/OnCompleteListener.java new file mode 100644 index 0000000000..f2ce5ea4e8 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/api/OnCompleteListener.java @@ -0,0 +1,12 @@ +package download.api; + +/** + * Created by Haochen on 2017/3/16. + * TODO: + */ +public interface OnCompleteListener { + /** + * 下载成功后自动调用此方法 + */ + void onComplete(); +} diff --git a/group01/895457260/code/src/main/java/download/api/OnFailListener.java b/group01/895457260/code/src/main/java/download/api/OnFailListener.java new file mode 100644 index 0000000000..7e79e6cb8a --- /dev/null +++ b/group01/895457260/code/src/main/java/download/api/OnFailListener.java @@ -0,0 +1,12 @@ +package download.api; + +/** + * Created by Haochen on 2017/3/16. + * TODO: + */ +public interface OnFailListener { + /** + * 下载失败后自动调用此方法 + */ + void onFail(); +} diff --git a/group01/895457260/code/src/main/java/download/impl/BaseConnection.java b/group01/895457260/code/src/main/java/download/impl/BaseConnection.java new file mode 100644 index 0000000000..72a2651e6e --- /dev/null +++ b/group01/895457260/code/src/main/java/download/impl/BaseConnection.java @@ -0,0 +1,76 @@ +package download.impl; + +import download.api.Connection; +import download.api.ConnectionException; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +/** + * Created by Haochen on 2017/3/16. + * TODO: + */ +public abstract class BaseConnection implements Connection { + URLConnection connection; + InputStream inputStream; + + private int contentLength; + private int readLen; + + BaseConnection(String url, int startPos, int endPos) throws ConnectionException { + contentLength = endPos - startPos + 1; + try { + connection = new URL(url).openConnection(); + init(startPos, endPos); + inputStream.mark(contentLength); + } catch (IOException e) { + throw new ConnectionException(); + } + } + + @Override + public int read(byte[] buf) throws IOException { + if (readLen >= contentLength) { + return -1; + } + int n = inputStream.read(buf); + if (readLen + n >= contentLength) { + n = contentLength - readLen; + readLen = contentLength; + } else { + readLen += n; + } + return n; + } + + protected abstract void init(int startPos, int endPos) throws IOException; + + @Override + public int getContentLength() { + return connection.getContentLength(); + } + + @Override + public void reset() throws IOException { + inputStream.reset(); + readLen = 0; + } + + @Override + public void close() { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + void openInputStream() throws IOException { + inputStream = new BufferedInputStream(connection.getInputStream()); + } +} diff --git a/group01/895457260/code/src/main/java/download/impl/ConnectionManagerImpl.java b/group01/895457260/code/src/main/java/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..116b6137b9 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,29 @@ +package download.impl; + +import download.Config; +import download.api.Connection; +import download.api.ConnectionException; +import download.api.ConnectionManager; + +import java.lang.reflect.InvocationTargetException; + +public class ConnectionManagerImpl implements ConnectionManager { + @Override + public Connection open(String url, int startPos, int endPos) throws ConnectionException { + String protocol = url.split(":", 2)[0]; + try { + Class clazz = Class.forName(getClassName(protocol)); + return (Connection) clazz.getDeclaredConstructor(String.class, int.class, int.class) + .newInstance(url, startPos, endPos); + } catch (ClassNotFoundException | IllegalAccessException | + NoSuchMethodException | InstantiationException | InvocationTargetException e) { + return new DefaultConnection(url, startPos, endPos); + } + } + + private String getClassName(String protocol) { + String packageName = Config.packageName + ".impl."; + String className = Character.toUpperCase(protocol.charAt(0)) + protocol.substring(1) + "Connection"; + return packageName + className; + } +} diff --git a/group01/895457260/code/src/main/java/download/impl/DefaultConnection.java b/group01/895457260/code/src/main/java/download/impl/DefaultConnection.java new file mode 100644 index 0000000000..54139b2e94 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/impl/DefaultConnection.java @@ -0,0 +1,28 @@ +package download.impl; + +import download.api.ConnectionException; + +import java.io.IOException; + +/** + * Created by Haochen on 2017/3/16. + * TODO: + */ +class DefaultConnection extends BaseConnection { + + DefaultConnection(String url, int startPos, int endPos) throws ConnectionException { + super(url, startPos, endPos); + } + + @Override + protected void init(int startPos, int endPos) throws IOException { + openInputStream(); + skipBytes(startPos); + } + + private void skipBytes(long n) throws IOException { + while (n > 0) { + n -= inputStream.skip(n); + } + } +} diff --git a/group01/895457260/code/src/main/java/download/impl/HttpConnection.java b/group01/895457260/code/src/main/java/download/impl/HttpConnection.java new file mode 100644 index 0000000000..04fbcc9dea --- /dev/null +++ b/group01/895457260/code/src/main/java/download/impl/HttpConnection.java @@ -0,0 +1,20 @@ +package download.impl; + +import java.io.*; +import java.net.HttpURLConnection; + +import download.api.ConnectionException; + +class HttpConnection extends BaseConnection { + + HttpConnection(String url, int startPos, int endPos) throws ConnectionException { + super(url, startPos, endPos); + } + + @Override + protected void init(int startPos, int endPos) throws IOException { + HttpURLConnection connection = (HttpURLConnection) super.connection; + connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + openInputStream(); + } +} diff --git a/group01/895457260/code/src/main/java/download/impl/HttpsConnection.java b/group01/895457260/code/src/main/java/download/impl/HttpsConnection.java new file mode 100644 index 0000000000..0179044719 --- /dev/null +++ b/group01/895457260/code/src/main/java/download/impl/HttpsConnection.java @@ -0,0 +1,24 @@ +package download.impl; + +import download.api.ConnectionException; + +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; + +/** + * Created by Haochen on 2017/3/16. + * TODO: + */ +public class HttpsConnection extends BaseConnection { + + HttpsConnection(String url, int startPos, int endPos) throws ConnectionException { + super(url, startPos, endPos); + } + + @Override + protected void init(int startPos, int endPos) throws IOException { + HttpsURLConnection connection = (HttpsURLConnection) super.connection; + connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + openInputStream(); + } +} diff --git a/group01/895457260/code/src/main/java/jvm/ClassFileLoader.java b/group01/895457260/code/src/main/java/jvm/ClassFileLoader.java new file mode 100644 index 0000000000..53e401c119 --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/ClassFileLoader.java @@ -0,0 +1,80 @@ +package jvm; + +import jvm.exception.ClassDuplicateException; +import jvm.exception.ClassNotExistsException; +import jvm.exception.ReadClassException; +import jvm.util.ArrayUtils; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ClassFileLoader { + + private List classPaths = new ArrayList<>(); + + public byte[] readBinaryCode(String className) throws ReadClassException { + File file = getClassFile(className); + if (file == null) { + throw new ClassNotExistsException(); + } + InputStream is; + try { + is = new FileInputStream(file); + } catch (FileNotFoundException e) { + throw new ClassNotExistsException(); + } + List bytes = new ArrayList<>(); + byte[] buf = new byte[1024]; + int len; + try { + while ((len = is.read(buf)) != -1) { + bytes.addAll(ArrayUtils.toList(buf, 0, len)); + } + } catch (IOException e) { + e.printStackTrace(); + } + return ArrayUtils.toArray(bytes); + } + + private File getClassFile(String className) throws ClassDuplicateException { + int split = className.lastIndexOf('.'); + String fileName = className.substring(split + 1) + ".class"; + String subPath = className.substring(0, split).replaceAll("[.]", "/"); + List files = new ArrayList<>(); + for (String path : classPaths) { + File dir = new File(path + '/' + subPath); + File[] listFile = dir.listFiles((dir1, name) -> name.equals(fileName)); + if (listFile != null) { + Arrays.stream(listFile).forEach(files::add); + } + } + if (files.size() > 1) { + throw new ClassDuplicateException(); + } + return files.size() == 1 ? files.get(0) : null; + } + + public void addClassPath(String path) { + if (path != null && !"".equals(path)) { + classPaths.add(path); + } + } + + public String getClassPath() { + StringBuilder builder = new StringBuilder(); + classPaths.forEach((i) -> builder.append(';').append(i)); + return builder.substring(1); + } + + boolean checkMagicNumber(byte[] bytes) { + String magicNumber = "CAFEBABE"; + String str = ""; + int byteNum = 4; + for (int i = 0; i < byteNum; ++i) { + str += Integer.toHexString(Byte.toUnsignedInt(bytes[i])); + } + return magicNumber.equals(str.toUpperCase()); + } +} diff --git a/group01/895457260/code/src/main/java/jvm/LiteJvm.java b/group01/895457260/code/src/main/java/jvm/LiteJvm.java new file mode 100644 index 0000000000..d642654d5f --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/LiteJvm.java @@ -0,0 +1,25 @@ +package jvm; + +import jvm.exception.MagicNumberException; +import jvm.exception.ReadClassException; + +/** + * Created by Haochen on 2017/3/26. + * TODO: + */ +public enum LiteJvm { + INSTANCE; + + private ClassFileLoader classFileLoader = new ClassFileLoader(); + + public void launch(String className) throws MagicNumberException, ReadClassException { + byte[] bytes = getBytes(className); + if (!classFileLoader.checkMagicNumber(bytes)) { + throw new MagicNumberException(); + } + } + + private byte[] getBytes(String className) throws ReadClassException { + return classFileLoader.readBinaryCode(className); + } +} diff --git a/group01/895457260/code/src/main/java/jvm/exception/ClassDuplicateException.java b/group01/895457260/code/src/main/java/jvm/exception/ClassDuplicateException.java new file mode 100644 index 0000000000..8666fb9177 --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/exception/ClassDuplicateException.java @@ -0,0 +1,7 @@ +package jvm.exception; + +/** + * Created by Haochen on 2017/3/27. + * TODO: + */ +public class ClassDuplicateException extends ReadClassException {} diff --git a/group01/895457260/code/src/main/java/jvm/exception/ClassNotExistsException.java b/group01/895457260/code/src/main/java/jvm/exception/ClassNotExistsException.java new file mode 100644 index 0000000000..053fd19076 --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/exception/ClassNotExistsException.java @@ -0,0 +1,7 @@ +package jvm.exception; + +/** + * Created by Haochen on 2017/3/27. + * TODO: + */ +public class ClassNotExistsException extends ReadClassException {} diff --git a/group01/895457260/code/src/main/java/jvm/exception/MagicNumberException.java b/group01/895457260/code/src/main/java/jvm/exception/MagicNumberException.java new file mode 100644 index 0000000000..712ea4f540 --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/exception/MagicNumberException.java @@ -0,0 +1,7 @@ +package jvm.exception; + +/** + * Created by Haochen on 2017/3/26. + * TODO: + */ +public class MagicNumberException extends Exception {} diff --git a/group01/895457260/code/src/main/java/jvm/exception/ReadClassException.java b/group01/895457260/code/src/main/java/jvm/exception/ReadClassException.java new file mode 100644 index 0000000000..490405a24f --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/exception/ReadClassException.java @@ -0,0 +1,7 @@ +package jvm.exception; + +/** + * Created by Haochen on 2017/3/27. + * TODO: + */ +public class ReadClassException extends Exception {} diff --git a/group01/895457260/code/src/main/java/jvm/util/ArrayUtils.java b/group01/895457260/code/src/main/java/jvm/util/ArrayUtils.java new file mode 100644 index 0000000000..7cc915a98d --- /dev/null +++ b/group01/895457260/code/src/main/java/jvm/util/ArrayUtils.java @@ -0,0 +1,31 @@ +package jvm.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +/** + * Created by Haochen on 2017/3/26. + * TODO: + */ +public class ArrayUtils { + public static Collection toList(byte[] array, int start, int length){ + Collection list = new ArrayList<>(); + byte[] newArray = new byte[length]; + System.arraycopy(array, start, newArray, 0, length); + for (byte b : newArray) { + list.add(b); + } + return list; + } + + public static byte[] toArray(Collection c) { + byte[] bytes = new byte[c.size()]; + int pos = 0; + for (byte b : c) { + bytes[pos++] = b; + } + return bytes; + } +} diff --git a/group01/895457260/code/src/litestruts/Struts.java b/group01/895457260/code/src/main/java/litestruts/Struts.java similarity index 100% rename from group01/895457260/code/src/litestruts/Struts.java rename to group01/895457260/code/src/main/java/litestruts/Struts.java diff --git a/group01/895457260/code/src/litestruts/View.java b/group01/895457260/code/src/main/java/litestruts/View.java similarity index 100% rename from group01/895457260/code/src/litestruts/View.java rename to group01/895457260/code/src/main/java/litestruts/View.java diff --git a/group01/895457260/code/src/litestruts/action/LoginAction.java b/group01/895457260/code/src/main/java/litestruts/action/LoginAction.java similarity index 100% rename from group01/895457260/code/src/litestruts/action/LoginAction.java rename to group01/895457260/code/src/main/java/litestruts/action/LoginAction.java diff --git a/group01/895457260/code/src/litestruts/exception/XmlElementNotFoundException.java b/group01/895457260/code/src/main/java/litestruts/exception/XmlElementNotFoundException.java similarity index 100% rename from group01/895457260/code/src/litestruts/exception/XmlElementNotFoundException.java rename to group01/895457260/code/src/main/java/litestruts/exception/XmlElementNotFoundException.java diff --git a/group01/895457260/code/src/test/java/algorithm/ArrayUtilTest.java b/group01/895457260/code/src/test/java/algorithm/ArrayUtilTest.java new file mode 100644 index 0000000000..8ea1f2e81a --- /dev/null +++ b/group01/895457260/code/src/test/java/algorithm/ArrayUtilTest.java @@ -0,0 +1,234 @@ +package algorithm; + +import algorithm.ArrayUtil; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * ArrayUtil Tester. + * + * @author + * @version 1.0 + * @since
二月 27, 2017
+ */ +public class ArrayUtilTest { + ArrayUtil util = new ArrayUtil(); + + /** + * Method: reverseArray(int[] origin) + */ + @Test + public void testReverseArray() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {7, 9, 30, 3}, + {7, 9, 30, 3, 4}, + {5}, + {} + }; + for (int[] a : arrays) { + util.reverseArray(a); + } + + int[][] result = { + {3, 30, 9, 7}, + {4, 3, 30, 9, 7}, + {5}, + {} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: removeZero(int[] oldArray) + */ + @Test + public void testRemoveZero() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}, + {6, 6, 3, 5, 4}, + {0, 0, 0}, + {} + }; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.removeZero(arrays[i]); + } + + int[][] result = { + {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, + {6, 6, 3, 5, 4}, + {}, + {} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: merge(int[] array1, int[] array2) + */ + @Test + public void testMerge() throws Exception { +//TODO: Test goes here... + int[][] arrays1 = { + {3, 5, 7, 8}, + {2, 3, 4}, + {1, 2, 3, 3, 4, 5}, + {1, 2, 2}, + {}, + {} + }; + int[][] arrays2 = { + {4, 5, 6, 7}, + {6, 7, 8, 9, 9}, + {4, 4, 5, 7}, + {}, + {2, 2, 3}, + {} + }; + + int[][] merged = new int[arrays1.length][]; + for (int i = 0; i < arrays1.length; ++i) { + merged[i] = util.merge(arrays1[i], arrays2[i]); + } + + int[][] result = { + {3, 4, 5, 6, 7, 8}, + {2, 3, 4, 6, 7, 8, 9}, + {1, 2, 3, 4, 5, 7}, + {1, 2}, + {2, 3}, + {} + }; + Assert.assertArrayEquals(merged, result); + } + + /** + * Method: grow(int [] oldArray, int size) + */ + @Test + public void testGrow() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {2, 3, 6}, + {}, + {1} + }; + + int[] size = {3, 3, 0}; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.grow(arrays[i], size[i]); + } + + int[][] result = { + {2, 3, 6, 0, 0, 0}, + {0, 0, 0}, + {1} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: fibonacci(int max) + */ + @Test + public void testFibonacci() throws Exception { +//TODO: Test goes here... + int[] max = {0, 1, 2, 15}; + + int[][] arrays = new int[max.length][]; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.fibonacci(max[i]); + } + + int[][] result = { + {}, + {}, + {1, 1}, + {1, 1, 2, 3, 5, 8, 13} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: getPrimes(int max) + */ + @Test + public void testGetPrimes() throws Exception { +//TODO: Test goes here... + int[] max = {0, 1, 2, 3, 11}; + + int[][] arrays = new int[max.length][]; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.getPrimes(max[i]); + } + + int[][] result = { + {}, + {}, + {}, + {2}, + {2, 3, 5, 7} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: getPerfectNumbers(int max) + */ + @Test + public void testGetPerfectNumbers() throws Exception { +//TODO: Test goes here... + int[] max = {0, 6, 7, 496, 497}; + + int[][] arrays = new int[max.length][]; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.getPerfectNumbers(max[i]); + } + + int[][] result = { + {}, + {}, + {6}, + {6, 28}, + {6, 28, 496} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: join(int[] array, String seperator) + */ + @Test + public void testJoin() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {3, 8, 9}, + {5}, + {6, 6}, + {} + }; + + String[] sep = {"-", "**", "", "---"}; + String[] joined = new String[arrays.length]; + + for (int i = 0; i < arrays.length; ++i) { + joined[i] = util.join(arrays[i], sep[i]); + } + + String[] result = { + "3-8-9", + "5", + "66", + "" + }; + Assert.assertArrayEquals(joined, result); + } +} diff --git a/group01/895457260/code/src/test/java/algorithm/lru/LRUPageFrameTest.java b/group01/895457260/code/src/test/java/algorithm/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..cfc6a7836c --- /dev/null +++ b/group01/895457260/code/src/test/java/algorithm/lru/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package algorithm.lru; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group01/895457260/code/src/test/java/datastructure/ArrayListTest.java b/group01/895457260/code/src/test/java/datastructure/ArrayListTest.java new file mode 100644 index 0000000000..907ea916ae --- /dev/null +++ b/group01/895457260/code/src/test/java/datastructure/ArrayListTest.java @@ -0,0 +1,152 @@ +package datastructure; + +import datastructure.basic.ArrayList; +import datastructure.basic.Iterator; +import datastructure.basic.List; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * ArrayList Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class ArrayListTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + protected final List getList() { + List list = createList(); + init(list); + return list; + } + + List createList() { + return new ArrayList(5); + } + + private void init(List list) { + for (int i = 1; i <= 5; ++i) { + list.add(i); + } + } + + protected final Object[] toArray(List list) { + Object[] array = new Object[list.size()]; + Iterator iterator = list.iterator(); + int pos = 0; + while (iterator.hasNext()) { + array[pos++] = iterator.next(); + } + return array; + } + + /** + * Method: add(Object o) + */ + @Test + public void testAddO() throws Exception { +//TODO: Test goes here... + List list = getList(); + for (int i = 6; i <= 10; ++i) { + list.add(i); + } + Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + Assert.assertEquals(list.size(), 10); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndexO() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {nowSize + 1, -1, nowSize, nowSize, 0, 1}; + Object[] values = {0, 0, 300, 400, 100, 200}; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + list.add(indexes[i], values[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(toArray(list), new Object[]{100, 200, 1, 2, 3, 4, 5, 400, 300}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize + 4); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {-1, nowSize, 0, 1, nowSize - 1, nowSize - 2}; + Object[] values = new Object[indexes.length]; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + values[i] = list.get(indexes[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{null, null, 1, 2, 5, 4}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {-1, nowSize, nowSize - 2, nowSize - 2, 1, 0}; + Object[] values = new Object[indexes.length]; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + values[i] = list.remove(indexes[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{null, null, 4, 5, 2, 1}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize - 4); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { +//TODO: Test goes here... + List list = getList(); + Iterator iterator = list.iterator(); + Object[] values = new Object[list.size()]; + int pos = 0; + while (iterator.hasNext()) { + values[pos++] = iterator.next(); + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5}); + } +} diff --git a/group01/895457260/code/src/datastructure/test/BinarySortedTreeTest.java b/group01/895457260/code/src/test/java/datastructure/BinarySortedTreeTest.java similarity index 98% rename from group01/895457260/code/src/datastructure/test/BinarySortedTreeTest.java rename to group01/895457260/code/src/test/java/datastructure/BinarySortedTreeTest.java index f0374b2700..6419d73951 100644 --- a/group01/895457260/code/src/datastructure/test/BinarySortedTreeTest.java +++ b/group01/895457260/code/src/test/java/datastructure/BinarySortedTreeTest.java @@ -1,4 +1,4 @@ -package datastructure.test; +package datastructure; import datastructure.basic.BinarySortedTree; import datastructure.basic.BinaryTreeNode; diff --git a/group01/895457260/code/src/test/java/datastructure/LinkedListTest.java b/group01/895457260/code/src/test/java/datastructure/LinkedListTest.java new file mode 100644 index 0000000000..dee34c8b3f --- /dev/null +++ b/group01/895457260/code/src/test/java/datastructure/LinkedListTest.java @@ -0,0 +1,252 @@ +package datastructure; + +import datastructure.exception.EmptyListException; +import datastructure.basic.LinkedList; +import datastructure.basic.List; +import org.junit.Assert; +import org.junit.Test; + +/** + * LinkedList Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class LinkedListTest extends ArrayListTest { + + @Override + List createList() { + return new LinkedList(); + } + + /** + * Method: addFirst(Object o) + */ + @Test + public void testAddFirst() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.addFirst(100); + Assert.assertArrayEquals(toArray(list), new Object[]{100, 1, 2, 3, 4, 5}); + } + + /** + * Method: addLast(Object o) + */ + @Test + public void testAddLast() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.addLast(100); + Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 100}); + } + + /** + * Method: removeFirst() + */ + @Test + public void testRemoveFirst() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + int count = list.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = list.removeFirst(); + } catch (EmptyListException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + Assert.assertArrayEquals(toArray(list), new Object[0]); + } + + /** + * Method: removeLast() + */ + @Test + public void testRemoveLast() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + int count = list.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = list.removeLast(); + } catch (EmptyListException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + Assert.assertArrayEquals(toArray(list), new Object[0]); + } + + /** + * + * Method: reverse() + * + */ + @Test + public void testReverse() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.reverse(); + Assert.assertArrayEquals(toArray(list), new Object[] {5, 4, 3, 2, 1}); + } + + /** + * + * Method: removeFirstHalf() + * + */ + @Test + public void testRemoveFirstHalf() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.removeFirstHalf(); + Assert.assertArrayEquals(toArray(list), new Object[] {3, 4, 5}); + } + + /** + * + * Method: remove(int i, int length) + * + */ + @Test + public void testRemoveForILength() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.remove(1, 3); + Assert.assertArrayEquals(toArray(list), new Object[] {1, 5}); + } + + /** + * + * Method: getElements(LinkedList list) + * + */ + @Test + public void testGetElements() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + LinkedList indexList = new LinkedList(); + for (int i = 0; i < 3; ++i) { + indexList.add(2 * i); + } + Object[] elements = list.getElements(indexList); + Assert.assertArrayEquals(elements, new Object[] {1, 3, 5}); + } + + /** + * + * Method: subtract(LinkedList list) + * + */ + @Test + public void testSubtract() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + LinkedList removeList = new LinkedList(); + for (int i = 0; i < 3; ++i) { + removeList.add(2 * i); + } + list.subtract(removeList); + Assert.assertArrayEquals(toArray(list), new Object[] {1, 3, 5}); + } + + /** + * + * Method: removeDuplicateValues() + * + */ + @Test + public void testRemoveDuplicateValues() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.add(5); + list.add(6); + list.add(8); + list.add(8); + list.add(9); + list.removeDuplicateValues(); + Assert.assertArrayEquals(toArray(list), new Object[] {1, 2, 3, 4, 5, 6, 8, 9}); + } + + /** + * + * Method: removeRange(int min, int max) + * + */ + @Test + public void testRemoveRange() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.removeRange(2, 5); + Assert.assertArrayEquals(toArray(list), new Object[] {1, 2, 5}); + } + + /** + * + * Method: intersection(LinkedList list) + * + */ + @Test + public void testIntersection() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + LinkedList list1 = new LinkedList(); + for (int i = 0; i < 4; ++i) { + list1.add(2 * i); + } + LinkedList result = list.intersection(list1); + Assert.assertArrayEquals(toArray(result), new Object[] {2, 4}); + } + + @Test + public void testMultiMethod() throws Exception { + LinkedList list = (LinkedList) getList(); + LinkedList subtractList = new LinkedList(); + for (int i = 0; i < 3; ++i) { + subtractList.add(2 * i); + } + LinkedList intersectionList = new LinkedList(); + for (int i = 0; i < 4; ++i) { + intersectionList.add(2 * i); + } + + list.reverse(); + list.subtract(subtractList); + Assert.assertArrayEquals(toArray(list), new Object[] {5, 4, 3, 2, 1}); + LinkedList intersection = list.intersection(intersectionList); + Assert.assertArrayEquals(toArray(intersection), new Object[] {}); + list.reverse(); + list.add(6); + list.add(6); + list.add(6); + list.add(7); + list.add(7); + list.add(8); + list.add(9); + list.add(9); + list.add(9); + list.add(10); + list.add(11); + list.removeDuplicateValues(); + Assert.assertArrayEquals(toArray(list), new Object[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); + list.remove(0, 0); + list.remove(0, 2); + Assert.assertArrayEquals(toArray(list), new Object[] {3, 4, 5, 6, 7, 8, 9, 10, 11}); + list.removeRange(0, 3); + list.removeRange(11, 13); + list.removeRange(11, 8); + list.removeRange(8, 11); + Assert.assertArrayEquals(toArray(list), new Object[] {3, 4, 5, 6, 7, 8, 11}); + list.removeFirstHalf(); + Assert.assertArrayEquals(toArray(list), new Object[] {6, 7, 8, 11}); + } +} diff --git a/group01/895457260/code/src/test/java/datastructure/QueueTest.java b/group01/895457260/code/src/test/java/datastructure/QueueTest.java new file mode 100644 index 0000000000..f2a9495484 --- /dev/null +++ b/group01/895457260/code/src/test/java/datastructure/QueueTest.java @@ -0,0 +1,108 @@ +package datastructure; + +import datastructure.exception.EmptyQueueException; +import datastructure.basic.Queue; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Queue Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class QueueTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private Queue getQueue() { + Queue queue = new Queue(5); + for (int i = 1; i <= 5; ++i) { + queue.enQueue(i); + } + return queue; + } + + private void assertQueue(Queue queue, Object[] actual) { + Class clazz = Queue.class; + Object[] array = null; + int head = 0; + int rear = 0; + Method mapIndex = null; + try { + Field arrayField = clazz.getDeclaredField("array"); + Field headField = clazz.getDeclaredField("head"); + Field rearField = clazz.getDeclaredField("rear"); + mapIndex = clazz.getDeclaredMethod("mapIndex", int.class); + arrayField.setAccessible(true); + headField.setAccessible(true); + rearField.setAccessible(true); + mapIndex.setAccessible(true); + array = (Object[]) arrayField.get(queue); + head = (int) headField.get(queue); + rear = (int) rearField.get(queue); + } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { + e.printStackTrace(); + } + int size = queue.size(); + Object[] excepted = new Object[size]; + int pos = 0; + try { + while (head < rear) { + excepted[pos++] = array[(int) mapIndex.invoke(queue, head)]; + head++; + } + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + Assert.assertArrayEquals(excepted, actual); + } + + /** + * Method: enQueue(Object o) + */ + @Test + public void testEnQueue() throws Exception { +//TODO: Test goes here... + Queue queue = getQueue(); + for (int i = 6; i <= 10; ++i) { + queue.enQueue(i); + } + assertQueue(queue, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + } + + /** + * Method: deQueue() + */ + @Test + public void testDeQueue() throws Exception { +//TODO: Test goes here... + Queue queue = getQueue(); + int count = queue.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = queue.deQueue(); + } catch (EmptyQueueException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + assertQueue(queue, new Object[0]); + } +} diff --git a/group01/895457260/code/src/test/java/datastructure/StackTest.java b/group01/895457260/code/src/test/java/datastructure/StackTest.java new file mode 100644 index 0000000000..ec3a8ed0a6 --- /dev/null +++ b/group01/895457260/code/src/test/java/datastructure/StackTest.java @@ -0,0 +1,93 @@ +package datastructure; + +import datastructure.basic.*; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.lang.reflect.Field; +import java.util.EmptyStackException; + +/** + * Stack Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class StackTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private Stack getStack() { + Stack stack = new Stack(); + for (int i = 1; i <= 5; ++i) { + stack.push(i); + } + return stack; + } + + private void assertStack(Stack stack, Object[] actual) { + Class clazz = Stack.class; + ArrayList elementData = null; + try { + Field field = clazz.getDeclaredField("elementData"); + field.setAccessible(true); + elementData = (ArrayList) field.get(stack); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + + Object[] excepted = null; + if (elementData != null) { + int size = stack.size(); + excepted = new Object[size]; + for (int i = 0; i < size; ++i) { + excepted[i] = elementData.get(i); + } + } + Assert.assertArrayEquals(excepted, actual); + } + + /** + * Method: push(Object o) + */ + @Test + public void testPush() throws Exception { +//TODO: Test goes here... + Stack stack = getStack(); + for (int i = 6; i <= 10; ++i) { + stack.push(i); + } + assertStack(stack, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + } + + /** + * Method: pop() + */ + @Test + public void testPop() throws Exception { +//TODO: Test goes here... + Stack stack = getStack(); + int count = stack.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = stack.pop(); + } catch (EmptyStackException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + assertStack(stack, new Object[0]); + } +} diff --git a/group01/895457260/code/src/datastructure/test/TestSuite.java b/group01/895457260/code/src/test/java/datastructure/TestSuite.java similarity index 91% rename from group01/895457260/code/src/datastructure/test/TestSuite.java rename to group01/895457260/code/src/test/java/datastructure/TestSuite.java index d13bff3e5d..76ffdd40e0 100644 --- a/group01/895457260/code/src/datastructure/test/TestSuite.java +++ b/group01/895457260/code/src/test/java/datastructure/TestSuite.java @@ -1,4 +1,4 @@ -package datastructure.test; +package datastructure; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/group01/895457260/code/src/test/java/download/FileDownloaderTest.java b/group01/895457260/code/src/test/java/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ce3158e98 --- /dev/null +++ b/group01/895457260/code/src/test/java/download/FileDownloaderTest.java @@ -0,0 +1,87 @@ +package download; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import download.api.ConnectionManager; +import download.impl.ConnectionManagerImpl; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +public class FileDownloaderTest { + private boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "file:///E:/Video/download/88993.mp4"; +// String url = "file:///E:/Pictures/Clannad/Clannad高清图片/38.jpg"; +// String url = "http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"; + + FileDownloader downloader = null; + try { + downloader = new FileDownloader(url); + } catch (IOException e) { + e.printStackTrace(); + Assert.fail("wrong url"); + } + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setOnCompleteListener(() -> { + downloadFinished = true; + System.out.println("下载完成"); + }); + downloader.setOnFailListener(() -> { + downloadFinished = true; + System.out.println("下载失败"); + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("正在下载…………"); + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private boolean actualContent(File downloaded, File source) { + String expected = readFile(downloaded); + String actual = readFile(source); + return expected.equals(actual); + } + + private String readFile(File file) { + int n; + StringBuilder builder = new StringBuilder(); + byte[] buf = new byte[1024]; + try { + InputStream is = new FileInputStream(file); + while ((n = is.read(buf)) != -1) { + for (int i = 0; i < n; ++i) { + builder.append(String.format("%d", buf[i])); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return builder.toString(); + } +} diff --git a/group01/895457260/code/src/test/java/jvm/ClassFileLoaderTest.java b/group01/895457260/code/src/test/java/jvm/ClassFileLoaderTest.java new file mode 100644 index 0000000000..ac48c1a4d6 --- /dev/null +++ b/group01/895457260/code/src/test/java/jvm/ClassFileLoaderTest.java @@ -0,0 +1,61 @@ +package jvm; + +import jvm.exception.ReadClassException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileLoaderTest { + + private static ClassFileLoader loader; + private static String path1 = "target/classes"; + private static String path2 = "target/test-classes"; + + @Before + public void setUp() throws Exception { + loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + } + + @After + public void tearDown() throws Exception {} + + @Test + public void testClassPath() { + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1+";"+path2,clzPath); + } + + @Test + public void testClassFileLength() throws ReadClassException { + String className = "jvm.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1016, byteCodes.length); + } + + @Test + public void testMagicNumber() throws ReadClassException { + String className = "jvm.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] {byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3]}; + + String actualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe", actualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuilder buffer = new StringBuilder(); + for (byte b : codes) { + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group01/895457260/code/src/test/java/jvm/EmployeeV1.java b/group01/895457260/code/src/test/java/jvm/EmployeeV1.java new file mode 100644 index 0000000000..0ab0e5bd1f --- /dev/null +++ b/group01/895457260/code/src/test/java/jvm/EmployeeV1.java @@ -0,0 +1,29 @@ +package jvm; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + } +} \ No newline at end of file diff --git a/group01/895457260/code/src/test/java/jvm/LiteJvmTest.java b/group01/895457260/code/src/test/java/jvm/LiteJvmTest.java new file mode 100644 index 0000000000..a6f36bb3ca --- /dev/null +++ b/group01/895457260/code/src/test/java/jvm/LiteJvmTest.java @@ -0,0 +1,83 @@ +package jvm; + +import jvm.exception.MagicNumberException; +import jvm.exception.ReadClassException; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * LiteJvm Tester. + * + * @author + * @version 1.0 + * @since
三月 26, 2017
+ */ +public class LiteJvmTest { + + private LiteJvm jvm = LiteJvm.INSTANCE; + private String fileName; + + @Before + public void before() throws Exception { + fileName = "target/classes/algorithm/ArrayUtil.class"; + } + + @After + public void after() throws Exception { + } + + /** + * Method: launch(File fileName) + */ + @Test + public void testLaunch() { +//TODO: Test goes here... + try { + jvm.launch(fileName); + } catch (MagicNumberException | ReadClassException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + } + + + /** + * Method: checkMagicNumber(byte[] bytes) + */ + @Test + public void testCheckMagicNumber() throws Exception { +//TODO: Test goes here... +// try { +// Method method = LiteJvm.class.getDeclaredMethod("checkMagicNumber", byte[].class); +// method.setAccessible(true); +// method.invoke(jvm, ???); +// } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { +// e.printStackTrace(); +// } + } + + /** + * Method: getBytes(File fileName) + */ + @Test + public void testGetBytes() throws Exception { +//TODO: Test goes here... + try { + Method method = LiteJvm.class.getDeclaredMethod("getBytes", File.class); + method.setAccessible(true); + byte[] bytes = (byte[]) method.invoke(jvm, fileName); + Assert.assertEquals(3851, bytes.length); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + } + +} diff --git a/group01/895457260/code/src/test/java/litestruts/StrutsTest.java b/group01/895457260/code/src/test/java/litestruts/StrutsTest.java new file mode 100644 index 0000000000..2526dc9354 --- /dev/null +++ b/group01/895457260/code/src/test/java/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package litestruts; + +import litestruts.Struts; +import litestruts.View; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group01/932573198/20170306/src/com/coderising/download/DownloadThread.java b/group01/932573198/20170306/src/com/coderising/download/DownloadThread.java index 8101fec04d..07894e6a1f 100644 --- a/group01/932573198/20170306/src/com/coderising/download/DownloadThread.java +++ b/group01/932573198/20170306/src/com/coderising/download/DownloadThread.java @@ -1,32 +1,43 @@ package com.coderising.download; +import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; public class DownloadThread extends Thread { Connection conn; int startPos; int endPos; - byte[] content; - - public DownloadThread(Connection conn, int startPos, int endPos) { + String targetPath; + DownloadListener listener; + public DownloadThread(){} + + public DownloadThread(Connection conn, int startPos, int endPos, String targetPath, DownloadListener listener) { this.conn = conn; this.startPos = startPos; this.endPos = endPos; + this.targetPath = targetPath; + this.listener = listener; } public void run() { + System.out.println("下载:" + startPos + "--" + endPos); try { - content = conn.read(startPos, endPos); + byte[] content = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile(new File(this.targetPath), "rw"); + //偏移量超出文件末尾时会自动更改其长度 + raf.seek(startPos); + raf.write(content, 0, content.length); + raf.close(); + listener.notifyFinished(); } catch (IOException e) { e.printStackTrace(); } - } - - public byte[] getContent() { - return content; + System.out.println("线程" + startPos + "-" + endPos + "的下载完成."); } } diff --git a/group01/932573198/20170306/src/com/coderising/download/FileDownloader.java b/group01/932573198/20170306/src/com/coderising/download/FileDownloader.java index 01e98c3711..fe9a8143fa 100644 --- a/group01/932573198/20170306/src/com/coderising/download/FileDownloader.java +++ b/group01/932573198/20170306/src/com/coderising/download/FileDownloader.java @@ -1,8 +1,5 @@ package com.coderising.download; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -10,23 +7,25 @@ import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; import com.coderising.download.api.DownloadListener; -/** - * 借鉴954958168 - */ + public class FileDownloader { + String targetPath; + String url; DownloadListener listener; ConnectionManager cm; - public FileDownloader(String _url) { - this.url = _url; + final int threadCount = 3; + public FileDownloader(String _url, String _targetPath) { + this.url = _url; + this.targetPath = _targetPath; } - public void execute() throws ConnectionException, InterruptedException, IOException { + public void execute() { // 在这里实现你的代码, 注意: 需要用多线程实现下载 // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, @@ -42,31 +41,26 @@ public void execute() throws ConnectionException, InterruptedException, IOExcept // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - - Connection conn = cm.open(this.url); - int length = conn.getContentLength(); - conn.close(); - List threads = new ArrayList<>(); - int i; - for (i = 0; i < 3; i++) { - DownloadThread thread = new DownloadThread(cm.open(this.url), i * (length / 3), (i + 1) * (length / 3) - 1); - threads.add(thread); - thread.start(); + Connection conn = null; + try { + conn = cm.open(this.url); + } catch (ConnectionException e) { + e.printStackTrace(); } - if (i * (length / 3) < length) { - DownloadThread thread = new DownloadThread(cm.open(this.url), i * (length / 3), length - 1); - threads.add(thread); - thread.start(); + int length = conn.getContentLength(); + + List list = new ArrayList<>(); + int n = length / threadCount; + list.add(-1); + for (int i = 1; i < threadCount; i++) { + list.add(i * n - 1); } - for (DownloadThread thread : threads) { - thread.join(); + if (length / threadCount != 0) { + list.add(length - 1); } - FileOutputStream fos = new FileOutputStream(new File("temp.jpg")); - for (DownloadThread thread : threads) { - fos.write(thread.getContent()); + for (int i = 1; i < list.size(); i++) { + new DownloadThread(conn, list.get(i - 1) + 1, list.get(i), this.targetPath, this.listener).start(); } - fos.close(); - this.listener.notifyFinished(); } @@ -78,8 +72,4 @@ public void setConnectionManager(ConnectionManager ucm) { this.cm = ucm; } - public DownloadListener getListener() { - return this.listener; - } - } diff --git a/group01/932573198/20170306/src/com/coderising/download/FileDownloaderTest.java b/group01/932573198/20170306/src/com/coderising/download/FileDownloaderTest.java index 5434dbfa10..6f5a012b55 100644 --- a/group01/932573198/20170306/src/com/coderising/download/FileDownloaderTest.java +++ b/group01/932573198/20170306/src/com/coderising/download/FileDownloaderTest.java @@ -1,12 +1,9 @@ package com.coderising.download; -import java.io.IOException; - import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; import com.coderising.download.api.DownloadListener; import com.coderising.download.impl.ConnectionManagerImpl; @@ -24,8 +21,10 @@ public void tearDown() throws Exception { @Test public void testDownload() { + String url = "http://localhost:8080/test/kittens.jpg"; - FileDownloader downloader = new FileDownloader(url); + String targetPath = "E://kittens.jpg"; + FileDownloader downloader = new FileDownloader(url, targetPath); ConnectionManager cm = new ConnectionManagerImpl(); downloader.setConnectionManager(cm); downloader.setListener(new DownloadListener() { @@ -34,11 +33,8 @@ public void notifyFinished() { downloadFinished = true; } }); - try { - downloader.execute(); - } catch (ConnectionException | InterruptedException | IOException e1) { - e1.printStackTrace(); - } + + downloader.execute(); // 等待多线程下载程序执行完毕 while (!downloadFinished) { @@ -51,7 +47,6 @@ public void notifyFinished() { } } System.out.println("下载完成!"); - } } diff --git a/group01/932573198/20170306/src/com/coderising/download/api/ConnectionException.java b/group01/932573198/20170306/src/com/coderising/download/api/ConnectionException.java index 0a52074ed1..c3ed7396e7 100644 --- a/group01/932573198/20170306/src/com/coderising/download/api/ConnectionException.java +++ b/group01/932573198/20170306/src/com/coderising/download/api/ConnectionException.java @@ -1,15 +1,5 @@ package com.coderising.download.api; -public class ConnectionException extends RuntimeException { - public ConnectionException(String message) { - super(message); - } - - public ConnectionException(String message, Throwable cause) { - super(message, cause); - } - - public ConnectionException(Throwable cause) { - super(cause); - } +public class ConnectionException extends Exception { + } diff --git a/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionImpl.java b/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionImpl.java index eb75b3c25f..a937084949 100644 --- a/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,42 +1,61 @@ package com.coderising.download.impl; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import com.coderising.download.api.Connection; public class ConnectionImpl implements Connection { - private FileInputStream fis; - private File file; + private HttpURLConnection ucon = null; - public ConnectionImpl(String source) throws FileNotFoundException { - file = new File(getClass().getClassLoader().getResource(source).getFile()); - fis = new FileInputStream(file); + private URL url = null; + + public ConnectionImpl() { + } + + public ConnectionImpl(String _url) throws MalformedURLException, IOException { + url = new URL(_url); } @Override public byte[] read(int startPos, int endPos) throws IOException { - fis.skip(startPos); + ucon = (HttpURLConnection) url.openConnection(); + // openConnection()已将连接打开 + // 不用ucon.connect(); + ucon.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream in = ucon.getInputStream(); + byte[] content = new byte[endPos - startPos + 1]; - fis.read(content, 0, content.length); + + byte[] data = new byte[1024]; + int read = 0, i = 0; + while ((read = in.read(data, 0, data.length)) != -1) { + System.arraycopy(data, 0, content, i, read); + i += read; + } return content; } @Override public int getContentLength() { - return (int) file.length(); - } - - @Override - public void close() { + int length = 0; try { - fis.close(); + ucon = (HttpURLConnection) url.openConnection(); + length = ucon.getContentLength(); } catch (IOException e) { e.printStackTrace(); + } finally { + ucon.disconnect(); } + return length; + } + + @Override + public void close() { } } diff --git a/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionManagerImpl.java index 851f1146a0..537155c67d 100644 --- a/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/group01/932573198/20170306/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -1,19 +1,19 @@ package com.coderising.download.impl; -import java.io.FileNotFoundException; +import java.io.IOException; import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; public class ConnectionManagerImpl implements ConnectionManager { + private Connection conn = null; + @Override - public Connection open(String url) throws ConnectionException { - ConnectionImpl conn = null; + public Connection open(String url) { try { conn = new ConnectionImpl(url); - } catch (FileNotFoundException e) { + } catch (IOException e) { e.printStackTrace(); } return conn; diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LRUPageFrame.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LRUPageFrame.java new file mode 100644 index 0000000000..05961c124e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LRUPageFrame.java @@ -0,0 +1,117 @@ +package com.aaront.exercise.basic; + +import java.util.Optional; + +/** + * 用双向链表实现LRU算法 + * + * @author tonyhui + */ +public class LRUPageFrame { + + private static class Node { + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + private int size = 0; + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + */ + public void access(int pageNum) { + Optional node = getNodeByPageNum(pageNum); + node.map(r -> { + delete(r); + addFirst(r); + return ""; + }).orElseGet(() -> { + Node newNode = new Node(); + newNode.pageNum = pageNum; + addFirst(newNode); + if (size >= capacity) { + delete(last); + } else { + size++; + } + return ""; + }); + } + + private Optional getNodeByPageNum(int pageNum) { + Node node = first; + while (node != null) { + if (node.pageNum == pageNum) return Optional.of(node); + node = node.next; + } + return Optional.empty(); + } + + private void delete(Node node) { + if (node == null) return; + if (node == first) { + deleteFirst(); + return; + } + if (node == last) { + deleteLast(); + return; + } + Node prevNode = node.prev; + Node nextNode = node.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + node.next = null; + node.prev = null; + } + + private void deleteFirst() { + if (first == null) return; + first = first.next; + first.prev = null; + } + + private void deleteLast() { + if (last == null) return; + last = last.prev; + last.next = null; + } + + private void addFirst(Node node) { + if (node == null) return; + if (first == null) { + first = node; + last = node; + } else { + node.next = first; + first.prev = node; + first = node; + } + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LRUPageFrameTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LRUPageFrameTest.java new file mode 100644 index 0000000000..bad7c8b736 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LRUPageFrameTest.java @@ -0,0 +1,32 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.LRUPageFrame; +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/DownloadThread.java b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/DownloadThread.java index 8dcf538f42..a26a539a5b 100644 --- a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/DownloadThread.java +++ b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/DownloadThread.java @@ -2,31 +2,33 @@ import com.aaront.exercise.api.Connection; +import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; public class DownloadThread extends Thread { Connection conn; int startPos; int endPos; - byte[] content; + File file; public DownloadThread(Connection conn, int startPos, int endPos) { this.conn = conn; this.startPos = startPos; this.endPos = endPos; + this.file = new File("hehe.jpg"); } public void run() { try { - content = conn.read(startPos, endPos); + byte[] content = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf.seek(startPos); + raf.write(content); } catch (IOException e) { e.printStackTrace(); } } - - public byte[] getContent() { - return this.content; - } } \ No newline at end of file diff --git a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloader.java b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloader.java index 128292ea8e..d9231a7b15 100644 --- a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloader.java +++ b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloader.java @@ -5,9 +5,6 @@ import com.aaront.exercise.api.ConnectionManager; import com.aaront.exercise.api.DownloadListener; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -64,14 +61,6 @@ public void execute() { e.printStackTrace(); } - try (FileOutputStream fos = new FileOutputStream(new File("temp.jpg"))) { - for(DownloadThread thread : threads) { - fos.write(thread.getContent()); - } - } catch (IOException e) { - e.printStackTrace(); - } - this.listener.notifyFinished(); } diff --git a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloaderTest.java b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloaderTest.java index 6f75b3cd6c..60354d9ca2 100644 --- a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloaderTest.java +++ b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/FileDownloaderTest.java @@ -19,7 +19,8 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "http://localhost:8080/test.jpg"; + String url = "http://121.42.185.101/forum/test.jpg"; + //String url = "https://raw.githubusercontent.com/thlcly/coding2017/master/README.md"; FileDownloader downloader = new FileDownloader(url); ConnectionManager cm = new ConnectionManagerImpl(); downloader.setConnectionManager(cm); diff --git a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionImpl.java b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionImpl.java index 5ad971c7c3..ffdb763201 100644 --- a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionImpl.java +++ b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionImpl.java @@ -3,38 +3,45 @@ import com.aaront.exercise.api.Connection; import com.aaront.exercise.api.ConnectionException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.BufferedInputStream; import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; public class ConnectionImpl implements Connection { + private BufferedInputStream bis; + private int contentLength; - private FileInputStream fis; - private File file; - - public ConnectionImpl(String source) throws FileNotFoundException { - file = new File(getClass().getClassLoader().getResource(source).getFile()); - fis = new FileInputStream(file); + public ConnectionImpl(String url) throws IOException { + URLConnection connection = new URL(url).openConnection(); + bis = new BufferedInputStream(connection.getInputStream()); + contentLength = connection.getContentLength(); } @Override public byte[] read(int startPos, int endPos) throws IOException { - fis.skip(startPos); + long skipped = bis.skip(startPos); + while(skipped < startPos) { + skipped += bis.skip(startPos - skipped); + } byte[] content = new byte[endPos - startPos + 1]; - fis.read(content, 0, content.length); + int len = bis.read(content, 0, content.length); + while (len < content.length) { + len += bis.read(content, len, content.length - len); + System.out.println(len); + } return content; } @Override public int getContentLength() { - return (int)file.length(); + return contentLength; } @Override public void close() { try { - fis.close(); + bis.close(); } catch (IOException e) { throw new ConnectionException("连接关闭失败"); } diff --git a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionManagerImpl.java b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionManagerImpl.java index a80c293176..7745aed166 100644 --- a/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionManagerImpl.java +++ b/group01/954958168/class03/MultiThreadDownload/src/main/java/com/aaront/exercise/impl/ConnectionManagerImpl.java @@ -4,25 +4,16 @@ import com.aaront.exercise.api.ConnectionException; import com.aaront.exercise.api.ConnectionManager; -import java.io.FileNotFoundException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.io.IOException; public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { try { - return new ConnectionImpl(parse(url)); - } catch (FileNotFoundException e) { + return new ConnectionImpl(url); + } catch (IOException e) { throw new ConnectionException("创建连接失败"); } } - - private String parse(String url) { - String pattern = "(http|https)://[a-zA-Z0-9]+:[0-9]+/([a-zA-Z0-9.]+)"; - Matcher compile = Pattern.compile(pattern).matcher(url); - if(!compile.matches()) throw new ConnectionException("资源url不合法"); - return compile.group(2); - } } diff --git a/group01/954958168/class03/MultiThreadDownload/src/main/resources/test.jpg b/group01/954958168/class03/MultiThreadDownload/src/main/resources/test.jpg deleted file mode 100644 index 0eb7a002d8..0000000000 Binary files a/group01/954958168/class03/MultiThreadDownload/src/main/resources/test.jpg and /dev/null differ diff --git a/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/loader/ClassFileLoader.java b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..b9eb85129a --- /dev/null +++ b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/loader/ClassFileLoader.java @@ -0,0 +1,64 @@ +package com.aaront.exercise.jvm.loader; + +import com.aaront.exercise.jvm.utils.string.FileUtils; +import com.aaront.exercise.jvm.utils.string.StringUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws IOException { + String[] parts = _parseClassPath(className); + List files = _convertFromNameToFile(clzPaths); + for (int i = 0, len = parts.length; i < len; i++) { + files = _filterFileByName(files, parts[i]); + } + if (files.size() != 1) throw new IllegalArgumentException("className不合法"); + return _readFile(files.get(0)); + } + + public void addClassPath(String path) { + if (StringUtils.isEmpty(path)) return; + if (!FileUtils.isDictionary(path)) return; + clzPaths.add(path); + } + + public String getClassPath() { + return StringUtils.join(clzPaths, ";"); + } + + private String[] _parseClassPath(String className) { + String[] parts = className.split("\\."); + parts[parts.length - 1] = parts[parts.length - 1] + ".class"; + return parts; + } + + private List _convertFromNameToFile(List paths) { + return paths.stream().map(File::new).collect(Collectors.toList()); + } + + private List _filterFileByName(List paths, String name) { + return paths.stream().flatMap(path -> { + File[] files = path.listFiles(file -> file.getName().equals(name)); + if (files == null) return Stream.of(); + return Arrays.stream(files); + }).collect(Collectors.toList()); + } + + private byte[] _readFile(File file) throws IOException { + byte[] content = new byte[(int) file.length()]; + FileInputStream fis = new FileInputStream(file); + fis.read(content); + return content; + } +} diff --git a/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/loader/EmployeeV1.java b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..af9d7238e3 --- /dev/null +++ b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.aaront.exercise.jvm.loader; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/utils/string/FileUtils.java b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/utils/string/FileUtils.java new file mode 100644 index 0000000000..770ebe5060 --- /dev/null +++ b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/utils/string/FileUtils.java @@ -0,0 +1,16 @@ +package com.aaront.exercise.jvm.utils.string; + +import java.io.File; + +/** + * @author tonyhui + * @since 17/3/31 + */ +public class FileUtils { + + public static Boolean isDictionary(String path) { + File f = new File(path); + return f.isDirectory(); + } + +} diff --git a/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/utils/string/StringUtils.java b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/utils/string/StringUtils.java new file mode 100644 index 0000000000..f82971bbaa --- /dev/null +++ b/group01/954958168/class04/mini-jvm/src/main/java/com/aaront/exercise/jvm/utils/string/StringUtils.java @@ -0,0 +1,40 @@ +package com.aaront.exercise.jvm.utils.string; + +/** + * @author tonyhui + * @since 17/3/31 + */ +public class StringUtils { + + public static Boolean isEmpty(CharSequence charSequence) { + if (charSequence == null) return true; + if (charSequence.length() == 0) return true; + int i = 0; + int len = charSequence.length(); + for (; i < len; i++) { + if (!Character.isWhitespace(charSequence.charAt(i))) break; + } + return i == len; + } + + public static String join(Object[] parts, String separator) { + StringBuilder sb = new StringBuilder(); + int len = parts.length; + for (int i = 0; i < len; i++) { + sb.append(parts[i]); + if (i != len - 1) { + sb.append(separator); + } + } + return sb.toString(); + } + + public static String join(Iterable iterable, final String separator) { + StringBuilder sb = new StringBuilder(); + iterable.forEach(r -> { + sb.append(r); + sb.append(separator); + }); + return sb.deleteCharAt(sb.length() - 1).toString(); + } +} diff --git a/group01/954958168/class04/mini-jvm/src/test/java/com/aaront/exercise/jvm/loader/ClassFileLoaderTest.java b/group01/954958168/class04/mini-jvm/src/test/java/com/aaront/exercise/jvm/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..45cce6ca72 --- /dev/null +++ b/group01/954958168/class04/mini-jvm/src/test/java/com/aaront/exercise/jvm/loader/ClassFileLoaderTest.java @@ -0,0 +1,77 @@ +package com.aaront.exercise.jvm.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; + +/** + * @author tonyhui + * @since 17/3/31 + */ +public class ClassFileLoaderTest { + private static String path1 = "/Users/tonyhui/Code/coding2017/group01/954958168/class04/mini-jvm/target/classes"; + private static String path2 = "."; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2, clzPath); + + } + + @Test + public void testClassFileLength() throws IOException { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.aaront.exercise.jvm.loader.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1070, byteCodes.length); + + } + + + @Test + public void testMagicNumber() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.aaront.exercise.jvm.loader.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coderising0305/download/impl/ConnectionManagerImpl.java b/group02/527705641/src/com/github/fei9009/coderising0305/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..6f5f006a59 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0305/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.github.fei9009.coderising0305.download.impl; + +import com.github.fei9009.coderising0305.download.api.Connection; +import com.github.fei9009.coderising0305.download.api.ConnectionException; +import com.github.fei9009.coderising0305.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java index 1ed5923ee9..c1e8691a66 100644 --- a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java @@ -88,4 +88,135 @@ private static class Node{ } } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int length = size/ 2; + for (int i = 0; i < length; i++) { + head = head.next; + } + size = size - length; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if (i == 0) { + while (i < length) { + if (head == null) { + size = 0; + break; + } + head = head.next; + i++; + } + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result = new int[list.size]; + for (int i = 0; i < result.length; i++) { + result[i] = (int)get((int)list.get(i)); + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + while (head.next != null && head.data == head.next.data) { + head = head.next; + size--; + } + Node dummy = head; + while (dummy.next != null) { + if (dummy.data == dummy.next.data) { + dummy.next = dummy.next.next; + size --; + } else { + dummy = dummy.next; + } + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if ((int)head.data > max) { + return ; + } + if ((int)get(size-1) < min) { + return; + } + while ((int)head.data > min && (int) head.data < max) { + head = head.next; + size--; + if (head == null) { + break; + } + } + Node dummy = head; + if (dummy == null) { + return; + } + while (dummy.next != null) { + if ((int)dummy.next.data > min && (int) dummy.next.data < max) { + dummy.next = dummy.next.next; + size --; + }else { + dummy = dummy.next; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } } diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java index 1eb379a020..4f42f62e0a 100644 --- a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java @@ -1,16 +1,16 @@ package com.github.fei9009.coding2017.basic; import static org.junit.Assert.*; - import org.junit.Before; import org.junit.Test; -public class LinkedListTest { +public class LinkedListTest extends ListTest { + + private LinkedList aLinkedList; -private LinkedList aLinkedList; - @Before public void setUpLinkedList() { + aList = new LinkedList(); aLinkedList = new LinkedList(); } @@ -18,75 +18,331 @@ public void setUpLinkedList() { public void testAddFirst() { aLinkedList.addFirst(5); assertEquals(5, aLinkedList.get(0)); - + aLinkedList.addFirst(6); assertEquals(6, aLinkedList.get(0)); assertEquals(5, aLinkedList.get(1)); assertEquals(2, aLinkedList.size()); } - + @Test public void testAddLast() { aLinkedList.addLast("hello"); assertEquals("hello", aLinkedList.get(0)); - + aLinkedList.addLast("world"); assertEquals("hello", aLinkedList.get(0)); assertEquals("world", aLinkedList.get(1)); assertEquals(2, aLinkedList.size()); } - + @Test public void testRemoveFirst() { aLinkedList.addLast("hello"); aLinkedList.addLast("world"); - + aLinkedList.removeFirst(); assertEquals("world", aLinkedList.get(0)); assertEquals(1, aLinkedList.size()); - + aLinkedList.removeFirst(); assertEquals(0, aLinkedList.size()); } - + @Test public void testRemoveLast() { aLinkedList.addFirst("world"); aLinkedList.addFirst("hello"); - + aLinkedList.removeLast(); assertEquals("hello", aLinkedList.get(0)); assertEquals(1, aLinkedList.size()); - + aLinkedList.removeLast(); assertEquals(0, aLinkedList.size()); } - + @Test public void testLinkedListFunctional() { - for (int i=1; i<4; i++) { - aLinkedList.add(i); // [1,2,3] + for (int i = 1; i < 4; i++) { + aLinkedList.add(i); // [1,2,3] } - aLinkedList.remove(1); // [1,3] - - aLinkedList.add(1, 0); // [1,0,3] - for (int i=4; i<6; i++) { - aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i = 4; i < 6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] } assertEquals(5, aLinkedList.size()); assertEquals(5, aLinkedList.get(0)); assertEquals(1, aLinkedList.get(2)); assertEquals(0, aLinkedList.get(3)); - - aLinkedList.remove(3); // [5, 4, 1, 3] - assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); - aLinkedList.removeLast(); // [5, 4, 1] - assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); - aLinkedList.removeFirst(); // [4,1] - + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size() - 1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size() - 1)); + aLinkedList.removeFirst(); // [4,1] + assertEquals(4, aLinkedList.get(0)); assertEquals(1, aLinkedList.get(1)); assertEquals(2, aLinkedList.size()); } + @Test + public void testReverse() { + // 测试当aLinkedList为空时的情况 + aLinkedList.reverse(); + assertEquals(0, aLinkedList.size()); + + // 测试当aLinkedList长度为1时的情况 + aLinkedList.add(4); + aLinkedList.reverse(); + assertEquals(1, aLinkedList.size()); + assertEquals(4, aLinkedList.get(0)); + + for (int i = 1; i < 4; i++) { + aLinkedList.add(i); // [4,1,2,3] + } + aLinkedList.reverse(); + assertEquals(4, aLinkedList.size()); + assertEquals(3, aLinkedList.get(0)); + assertEquals(2, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10, 删除以后的值为7,8,10 + */ + @Test + public void testRemoveFirstHalf() { + aLinkedList.removeFirstHalf(); + assertEquals(0, aLinkedList.size()); + + aLinkedList.add(2); + aLinkedList.add(5); + aLinkedList.add(7); + aLinkedList.add(8); // [2,5,7,8] + + aLinkedList.removeFirstHalf(); // [7,8] + assertEquals(2, aLinkedList.size()); + assertEquals(7, aLinkedList.get(0)); + assertEquals(8, aLinkedList.get(1)); + + aLinkedList.add(10); // [7,8,10] + + aLinkedList.removeFirstHalf(); // [8,10] + assertEquals(2, aLinkedList.size()); + assertEquals(8, aLinkedList.get(0)); + assertEquals(10, aLinkedList.get(1)); + + aLinkedList.removeFirstHalf(); // [10] + aLinkedList.removeFirstHalf(); // [10] + assertEquals(1, aLinkedList.size()); + assertEquals(10, aLinkedList.get(0)); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + @Test + public void testRemoveIntInt() { + for (int i = 0; i < 4; i++) { + aLinkedList.add(i); // [0,1,2,3] + } + + expectedEx.expect(Exception.class); + aLinkedList.remove(1, -1); + + expectedEx.expect(Exception.class); + aLinkedList.remove(-1, 1); + + aLinkedList.remove(0, 2); // [2,3] + assertEquals(2, aLinkedList.size()); + assertEquals(2, aLinkedList.get(0)); + assertEquals(3, aLinkedList.get(1)); + + aLinkedList.remove(1, 0); + aLinkedList.remove(0, 0); + assertEquals(2, aLinkedList.size()); + + aLinkedList.remove(1, 1); // [2] + assertEquals(1, aLinkedList.size()); + assertEquals(2, aLinkedList.get(0)); + + aLinkedList.remove(0, 1); // [] + assertEquals(0, aLinkedList.size()); + + expectedEx.expect(Exception.class); + aLinkedList.remove(1, 3); + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + @Test + public void testGetElements() { + for (int i = 0; i < 4; i++) { + aLinkedList.add(i * i); // [0,1,4,9] + } + + LinkedList bLinkedList = new LinkedList(); + int[] z1 = aLinkedList.getElements(bLinkedList); // [] + assertArrayEquals(new int[0], z1); + + bLinkedList.add(1); + bLinkedList.add(3); // [1, 3] + + z1 = aLinkedList.getElements(bLinkedList); // [1, 9] + assertArrayEquals(new int[] { 1, 9 }, z1); + + bLinkedList.add(1, 2); // bLinkedList = [1, 2, 3] + z1 = aLinkedList.getElements(bLinkedList); // [1, 4, 9] + assertArrayEquals(new int[] { 1, 4, 9 }, z1); + + bLinkedList.add(0, 0); // bLinkedList = [0, 1, 2, 3] + z1 = aLinkedList.getElements(bLinkedList); // [0, 1, 4, 9] + assertArrayEquals(new int[] { 0, 1, 4, 9 }, z1); + + // aLinkedList不应该变化 + assertEquals(4, aLinkedList.size()); + for (int i = 0; i < 4; i++) { + assertEquals(i * i, aLinkedList.get(i)); // [0,1,4,9] + } + + // Exception + bLinkedList.add(5); // bLinkedList = [0, 1, 2, 3, 5] + expectedEx.expect(Exception.class); + z1 = aLinkedList.getElements(bLinkedList); + } + + @Test + public void TestSubtract() { + // 传进的list为null,什么都不干 + LinkedList list = null; + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); // [0,1,2,3,4,5] + } + aLinkedList.subtract(list); + assertEquals(6, aLinkedList.size()); + for (int i = 0; i < 6; i++) { + assertEquals(i, aLinkedList.get(i)); // [0,1,2,3,4,5] + } + + // 传进的list为空链表 + list = new LinkedList(); + aLinkedList.subtract(list); + assertEquals(6, aLinkedList.size()); + for (int i = 0; i < 6; i++) { + assertEquals(i, aLinkedList.get(i)); // [0,1,2,3,4,5] + } + + aLinkedList.add(1, 1); // [0,1,1,2,3,4,5] + aLinkedList.add(4, 3); // [0, 1, 1, 2, 3, 3, 4, 5] + + // list添加元素[0, 1, 3, 7] + list.add(0); + list.add(1); + list.add(3); + list.add(7); + + aLinkedList.subtract(list); // [2, 4, 5] + + assertEquals(3, aLinkedList.size()); + assertEquals(2, aLinkedList.get(0)); + assertEquals(4, aLinkedList.get(1)); + assertEquals(5, aLinkedList.get(2)); + } + + @Test + public void testRemoveDuplicateValues() { + aLinkedList.add(3); + aLinkedList.add(3); + aLinkedList.add(3); + aLinkedList.add(4); + aLinkedList.add(5); + aLinkedList.add(6); + aLinkedList.add(6); // [3, 3, 3, 4, 5, 6, 6] + + aLinkedList.removeDuplicateValues(); // [3, 4, 5, 6] + + assertEquals(4, aLinkedList.size()); + assertEquals(3, aLinkedList.get(0)); + assertEquals(4, aLinkedList.get(1)); + assertEquals(5, aLinkedList.get(2)); + assertEquals(6, aLinkedList.get(3)); + } + + @Test + public void testRemoveRange() { + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); // [0, 1, 2, 3, 4, 5] //考虑重复元素 + } + aLinkedList.addFirst(0); // [0, 0, 1, 2, 3, 4, 5] + aLinkedList.add(3, 2); // [0, 0, 1, 2, 2, 3, 4, 5] + + aLinkedList.removeRange(1, 4); // 大于1小于4 [0, 0, 1, 4, 5] + + assertEquals(5, aLinkedList.size()); + assertEquals(0, aLinkedList.get(0)); + assertEquals(0, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + assertEquals(5, aLinkedList.get(4)); + + // 若出现 min >= max的情况,什么都不做 + aLinkedList.removeRange(4, 1); + assertEquals(5, aLinkedList.size()); + assertEquals(0, aLinkedList.get(0)); + assertEquals(0, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + assertEquals(5, aLinkedList.get(4)); + + // 将整个链表中的元素删除 + aLinkedList.removeRange(-1, 9); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testIntersection() { + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); + } + aLinkedList.add(6); + aLinkedList.add(7); // [0, 1, 2, 3, 4, 5, 6, 7] + // list为null + LinkedList list = null; + LinkedList newList1 = aLinkedList.intersection(list); + assertNull(newList1); + + // list为空链表 + list = new LinkedList(); + LinkedList newList2 = aLinkedList.intersection(list); + assertEquals(0, newList2.size()); + + list.add(0); + list.add(3); + list.add(4); + list.add(7); + list.add(8); // [0, 3, 4, 7, 8] + LinkedList newList3 = aLinkedList.intersection(list); + + assertEquals(4, newList3.size()); + assertEquals(0, newList3.get(0)); + assertEquals(3, newList3.get(1)); + assertEquals(4, newList3.get(2)); + assertEquals(7, newList3.get(3)); + } } diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/ListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ListTest.java new file mode 100644 index 0000000000..06ac2257bb --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ListTest.java @@ -0,0 +1,93 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i = 0; i < 1000; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(100, aList.get(100)); + assertEquals(999, aList.get(999)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i = 0; i < 10; i++) + aList.add(i * 2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + + aList.add(3); + + expectedEx.expect(Exception.class); + aList.add(2, 5); + } + + + +} diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore index c24866af6d..0a2df7bbee 100644 --- a/group02/812350401/.gitignore +++ b/group02/812350401/.gitignore @@ -1,3 +1,4 @@ -/bin/ -/lib/ -/src/java_training +target/ +.idea/ +src/main/java/train/ +src/main/resources/ \ No newline at end of file diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java deleted file mode 100644 index 1c1b9a6df3..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; - -import java.util.Arrays; - - -public class ArrayList implements List { - - private int size = 0; - private int DEFAULT_LENGTH = 10; - - private Object[] elementData = new Object[DEFAULT_LENGTH]; - - public void add(Object o){ - ensureCapcacity(); - elementData[size++] = o; - } - public void add(int index, Object o){ - ensurnPosition(index); - ensureCapcacity(); - System.arraycopy(elementData, index, elementData, index + 1, - size - index); - elementData[index] = o; - size++; - } - - public Object get(int index){ - ensureIndex(index); - return elementData[index]; - } - - public Object remove(int index){ - ensureIndex(index); - Object temp = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, - size - index - 1); - elementData[size-1] = null; - size--; - return temp; - } - - public void clear() { - elementData = new Object[DEFAULT_LENGTH]; - size = 0; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - Iterator iterator = new IteratorImp(this); - return iterator; - } - - private void ensureCapcacity() { - int oldLength = elementData.length; - if (size+1 > oldLength) { - elementData = Arrays.copyOf(elementData, 2*oldLength); - } - } - - private void ensureIndex(int index) { - if (index >= size || index < 0) - throw new ArrayIndexOutOfBoundsException(String.format("index %d out of bounds [0-%d)", index, size)); - } - - private void ensurnPosition(int index) { - if (index <0 || index>size) - throw new ArrayIndexOutOfBoundsException(String.format("position %d out of position [0-%d)", index, size)); - } - - @Override - public String toString() { - Object[] tempArray = Arrays.copyOf(elementData, size); - return Arrays.toString(tempArray); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index 5b1f17342b..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; - -public class BinaryTreeNode > { - private E data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(E x) { - data = x; - } - public E getData() { - return data; - } - public void setData(E data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(E o){ - BinaryTreeNode node = new BinaryTreeNode(o); - boolean left = true; - BinaryTreeNode currentNode = this; - BinaryTreeNode previousNode = null; - - while (currentNode != null) { - previousNode = currentNode; - int compareTo = node.getData().compareTo(currentNode.getData()); - if (compareTo <= 0) { // 小于,往左插入 - currentNode = currentNode.left; - left = true; - } else { - currentNode = currentNode.right; - left = false; - } - } - if (left) { - previousNode.left = node; - } else { - previousNode.right = node; - } - return node; - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java deleted file mode 100644 index 7705715c84..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; - -public interface Iterator { - boolean hasNext(); - Object next(); -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java deleted file mode 100644 index 0ba1d9861c..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,365 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; -import static org.junit.Assert.assertEquals; - -import java.util.Arrays; -import java.util.Objects; - -import org.omg.CORBA.PRIVATE_MEMBER; - -import com.sun.tracing.dtrace.ArgsAttributes; - -import utils.ArrayUtils; - -public class LinkedList implements List { - - private Node head; - private int size = 0; - - /** - * node接收的index一定是范围内的值,不可能越界 - * @param index - * @return a Node - */ - Node node(int index) { - Node x = head; - for (int i=0; i= size) - throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); - } - - private void checkPositionIndex(int index) { - if (index < 0 || index > size) - throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - // 自感写的还行,没有多余代码 - Node previous = null; - while (head != null) { - Node next = head.next; - head.next = previous; - previous = head; - head = next; - } - head = previous; - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - int halfIndex = size() / 2; - head = node(halfIndex); - size = size() - halfIndex; - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - * @throws Exception - */ - public void remove(int i, int length) throws Exception{ - if (length <0 || i <0 || i>=size()) throw new Exception(); - if (length == 0) return; - if (i+length>size()) throw new Exception(); - - Node after = (i+length==size()?null:node(i+length)); - if (i>=1) { - node(i-1).next = after; - } else { - head = after; - } - - size -= length; - } - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - * @throws Exception - */ - public int[] getElements(LinkedList list) throws Exception { - // 不允许用get方法取得当前链表的元素,重分利用当前链表和list的有序性 - if (list.size == 0) return new int[0]; - if ((int)list.get(0)<0 || (int)list.get(list.size-1)>=size) { - throw new Exception(); - } - java.util.LinkedList aStandardList = new java.util.LinkedList<>(); - Node current = head; - for (int i=0,j=0; i bValue) { - b++; - } else if (aValue < bValue) { - a++; - } else { - cList.add(a); - a++; - b++; - } - } - return cList; - } - - public static LinkedList copy(LinkedList list) { - LinkedList copy = new LinkedList(); - Iterator it = list.iterator(); - while (it.hasNext()) { - copy.add(it.next()); - } - return copy; - } - - public static void main(String args[]) throws Exception { - LinkedList aLinkedList = new LinkedList(); - for (int i=0; i<4; i=i*i) { - aLinkedList.add(i); // [0,1,4,9] - } - LinkedList bLinkedList = new LinkedList(); - int[] z1 = aLinkedList.getElements(bLinkedList); // [] - System.out.println(Arrays.toString(z1)); - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java deleted file mode 100644 index c9eae11190..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; - -public interface List { - void add(Object o); - void add(int index, Object o); - Object get(int index); - Object remove(int index); - int size(); - Iterator iterator(); -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java deleted file mode 100644 index bcb9d0f9e4..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; - -public class Queue { - - // 队列入口 -》1,2,3,4 -》队列出口 - private LinkedList aList = new LinkedList(); - - public void enQueue(Object o){ - aList.addFirst(o); - } - - public Object deQueue(){ - return aList.removeLast(); - } - - public boolean isEmpty(){ - return aList.size() == 0; - } - - public int size(){ - return aList.size(); - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java deleted file mode 100644 index ddfe76f774..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.github.miniyk2012.coding2017.basic; - -public class Stack { - - // 栈顶 《-》 1,2,3,4 栈底 - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(0, o); - } - - public Object pop(){ - return elementData.remove(0); - } - - public Object peek(){ - return elementData.get(0); - } - public boolean isEmpty(){ - return elementData.size() == 0; - } - public int size(){ - return elementData.size(); - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java deleted file mode 100644 index b308c6bfef..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.miniyk2012.coding2017.basic.test; - -import org.junit.Before; -import com.github.miniyk2012.coding2017.basic.ArrayList; - - -public class ArrayListTest extends ListTest { - - @Before - public void setUpArrayList() { - aList = new ArrayList(); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java deleted file mode 100644 index 698506e2b5..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.github.miniyk2012.coding2017.basic.test; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; - -public class BinaryTreeNodeTest { - - private BinaryTreeNode binaryTreeNode; - - /** - // 4 - // 1 5 - // 2 3 - */ - @Before - public void setUpBinaryTreeNode() { - binaryTreeNode = new BinaryTreeNode(4); - binaryTreeNode.insert(1); - binaryTreeNode.insert(3); - binaryTreeNode.insert(5); - binaryTreeNode.insert(2); - } - - @Test - public void testBinaryTreeNodeFunctional1() { - assertEquals(new Integer(4), binaryTreeNode.getData()); - assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); - assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); - assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); - assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); - } - - @Test - public void testBinaryTreeFunctional2() { - BinaryTreeNode node1 = binaryTreeNode.getLeft(); - assertEquals(new Integer(1), node1.getData()); - assertEquals(new Integer(3), node1.getRight().getData()); - assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); - } - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public void testBinaryTreeFunctional3() - { - BinaryTreeNode treeNode = new BinaryTreeNode(100); - treeNode.insert(10); - binaryTreeNode.setRight(treeNode); - // 4 - // 1 100 - // 2 3 10 - assertEquals(new Integer(4), binaryTreeNode.getData()); - assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); - assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); - assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); - assertEquals(new Integer(100), binaryTreeNode.getRight().getData()); - assertEquals(new Integer(10), binaryTreeNode.getRight().getLeft().getData()); - - expectedEx.expect(Exception.class); - binaryTreeNode.getRight().getRight().getRight(); // null exception - binaryTreeNode.getRight().getRight().getLeft(); // null exception - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java deleted file mode 100644 index 199e6a9a12..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java +++ /dev/null @@ -1,404 +0,0 @@ -package com.github.miniyk2012.coding2017.basic.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.github.miniyk2012.coding2017.basic.Iterator; -import com.github.miniyk2012.coding2017.basic.LinkedList; - - -public class LinkedListTest extends ListTest{ - - private LinkedList aLinkedList; - - @Before - public void setUpLinkedList() { - aList = new LinkedList(); - aLinkedList = new LinkedList(); - } - - /** - * 测试iterator不因为各种操作而失效 - */ - @After - public void testIterator2() { - java.util.LinkedList aStandardList = new java.util.LinkedList(); - for (int i=0; i5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - @Test - public void testRemoveFirstHalf() { - aLinkedList.removeFirstHalf(); - assertEquals(0, aLinkedList.size()); - - aLinkedList.add(2); - aLinkedList.add(5); - aLinkedList.add(7); - aLinkedList.add(8); // [2,5,7,8] - - aLinkedList.removeFirstHalf(); // [7,8] - assertEquals(2, aLinkedList.size()); - assertEquals(7, aLinkedList.get(0)); - assertEquals(8, aLinkedList.get(1)); - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - * @throws Exception - */ - @Test - public void testRemoveIntInt() throws Exception { - - for (int i=0; i<4; i++) { - aLinkedList.add(i); // [0,1,2,3] - } - - aLinkedList.remove(0, 2); // [2,3] - assertEquals(2, aLinkedList.get(0)); - assertEquals(3, aLinkedList.get(1)); - assertEquals(2, aLinkedList.size()); - - aLinkedList.remove(1, 0); - aLinkedList.remove(0, 0); - assertEquals(2, aLinkedList.size()); - - aLinkedList.remove(1, 1); // [2] - assertEquals(1, aLinkedList.size()); - assertEquals(2, aLinkedList.get(0)); - - aLinkedList.remove(0, 1); // [] - assertEquals(0, aLinkedList.size()); - - expectedEx.expect(Exception.class); - aLinkedList.remove(1, 3); - } - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - * @throws Exception - */ - @Test - public void testGetElements() throws Exception { - for (int i=0; i<4; i=i+1) { - aLinkedList.add(i*i); // [0,1,4,9] - } - LinkedList bLinkedList = new LinkedList(); - int[] z1 = aLinkedList.getElements(bLinkedList); // [] - assertArrayEquals(new int[0], z1); - - bLinkedList.add(1); - bLinkedList.add(3); // [1, 3] - - z1 = aLinkedList.getElements(bLinkedList); // [1, 9] - assertArrayEquals(new int[] {1,9}, z1); - - bLinkedList.add(1, 2); // bLinkedList = [1, 2, 3] - z1 = aLinkedList.getElements(bLinkedList); // [1, 4, 9] - assertArrayEquals(new int[] {1,4,9}, z1); - - bLinkedList.add(0, 0); // bLinkedList = [0, 1, 2, 3] - z1 = aLinkedList.getElements(bLinkedList); // [0, 1, 4, 9] - assertArrayEquals(new int[] {0,1,4,9}, z1); - - // aLinkedList不应该变化 - assertEquals(4, aLinkedList.size()); - for (int i=0; i<4; i++) { - assertEquals(i*i, aLinkedList.get(i)); // [0,1,4,9] - } - - // Exception - bLinkedList.add(5); // bLinkedList = [0, 1, 2, 3, 5] - expectedEx.expect(Exception.class); - z1 = aLinkedList.getElements(bLinkedList); - } - - @Test - public void TestSubtract() - { - //传进的list为null,什么都不干 - LinkedList list = null; - for (int i=0; i<6; i++) - { - aLinkedList.add(i); //[0,1,2,3,4,5] - } - aLinkedList.subtract(list); - assertEquals(6, aLinkedList.size()); - - //传进的list为空 - list = new LinkedList(); - aLinkedList.subtract(list); - assertEquals(6, aLinkedList.size()); - - aLinkedList.add(1, 1); //[0,1,1,2,3,4,5] - aLinkedList.add(4, 3); //[0,1, 1, 2, 3, 3, 4, 5] - - - // list添加元素[0,1, 6, 3] - list.add(0); - list.add(1); - list.add(6); - list.add(3); - - aLinkedList.subtract(list); //[ 2, 4, 5] - - assertEquals(2, aLinkedList.get(0)); - assertEquals(4, aLinkedList.get(1)); - assertEquals(5, aLinkedList.get(2)); - assertEquals(3, aLinkedList.size()); - - list = new LinkedList(); - list.add(7); - list.add(4); - list.add(2); - list.add(2); // [7,4,2,2] - - aLinkedList.subtract(list); // [5] - - assertEquals(5, aLinkedList.get(0)); - assertEquals(1, aLinkedList.size()); - - list = new LinkedList(); - list.add(5); - list.add(5); - list.add(5); // [5,5,5] - - aLinkedList.subtract(list); // [] - assertEquals(0, aLinkedList.size()); - - aLinkedList.subtract(list); - assertEquals(0, aLinkedList.size()); - } - - @Test - public void testRemoveDuplicateValues() - { - aLinkedList.add(3); - aLinkedList.add(3); - aLinkedList.add(4); - aLinkedList.add(4); - aLinkedList.add(5); - aLinkedList.add(6); - aLinkedList.add(6); //[3, 3, 4, 4, 5, 6, 6] - assertEquals(7, aLinkedList.size()); - - aLinkedList.removeDuplicateValues(); //[3, 4, 5, 6] - - assertEquals(3, aLinkedList.get(0)); - assertEquals(4, aLinkedList.get(1)); - assertEquals(5, aLinkedList.get(2)); - assertEquals(6, aLinkedList.get(3)); - assertEquals(4, aLinkedList.size()); - - aLinkedList = new LinkedList(); - aLinkedList.removeDuplicateValues(); - assertEquals(0, aLinkedList.size()); - - aLinkedList = new LinkedList(); - aLinkedList.add(0); - aLinkedList.removeDuplicateValues(); - assertEquals(1, aLinkedList.size()); - assertEquals(0, aLinkedList.get(0)); - - for (int i=1; i<4; i++) { - aLinkedList.add(i); // [0,1,2,3] - } - aLinkedList.removeDuplicateValues(); - assertEquals(4, aLinkedList.size()); - assertEquals(0, aLinkedList.get(0)); - assertEquals(1, aLinkedList.get(1)); - assertEquals(2, aLinkedList.get(2)); - assertEquals(3, aLinkedList.get(3)); - - - } - - @Test - public void testRemoveRange() throws Exception - { - for (int i=0; i<6; i++) - { - aLinkedList.add(i); // [0, 1, 2, 3, 4, 5] //考虑重复元素 - } - aLinkedList.addFirst(0); // [0, 0, 1, 2, 3, 4, 5] - aLinkedList.add(3, 2); // [0, 0, 1, 2, 2, 3, 4, 5] - - aLinkedList.removeRange(1, 4); // 大于1小于4 [0, 0, 1, 4, 5] - - assertEquals(0, aLinkedList.get(0)); - assertEquals(0, aLinkedList.get(1)); - assertEquals(1, aLinkedList.get(2)); - assertEquals(4, aLinkedList.get(3)); - assertEquals(5, aLinkedList.get(4)); - assertEquals(5, aLinkedList.size()); - - //若出现 min >= max的情况,什么都不做 - aLinkedList.removeRange(4, 1); - assertEquals(0, aLinkedList.get(0)); - assertEquals(0, aLinkedList.get(1)); - assertEquals(1, aLinkedList.get(2)); - assertEquals(4, aLinkedList.get(3)); - assertEquals(5, aLinkedList.get(4)); - assertEquals(5, aLinkedList.size()); - - // 将整个链表中的元素删除 - aLinkedList.removeRange(-1, 9); - assertEquals(0, aLinkedList.size()); - - } - - @Test - public void testIntersection() { - for (int i = 0; i < 6; i++) { - aLinkedList.add(i); - } - aLinkedList.add(6); - aLinkedList.add(7); // [0, 1, 2, 3, 4, 5, 6, 7] - // list为null - LinkedList list = null; - LinkedList newList1 = aLinkedList.intersection(list); - assertNull(newList1); - - // list为空链表 - list = new LinkedList(); - LinkedList newList2 = aLinkedList.intersection(list); - assertEquals(0, newList2.size()); - - list.add(0); - list.add(3); - list.add(4); - list.add(7); - list.add(8); // [0, 3, 4, 7, 8] - LinkedList newList3 = aLinkedList.intersection(list); - - assertEquals(4, newList3.size()); - assertEquals(0, newList3.get(0)); - assertEquals(3, newList3.get(1)); - assertEquals(4, newList3.get(2)); - assertEquals(7, newList3.get(3)); - } - -} - diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java deleted file mode 100644 index b9f32fcc3b..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.github.miniyk2012.coding2017.basic.test; - -import static org.junit.Assert.*; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import com.github.miniyk2012.coding2017.basic.Iterator; -import com.github.miniyk2012.coding2017.basic.List; - -public class ListTest { - - protected static List aList; - - @Test - public void testFunctional() { - aList.add(1); - aList.add(2); - assertEquals(1, aList.get(0)); - assertEquals(2, aList.get(1)); - - aList.add(3); - aList.add(0, 5); - aList.add(2, 11); - assertEquals(5, aList.get(0)); - assertEquals(11, aList.get(2)); - - aList.add("hi"); - assertEquals("hi", aList.get(5)); - assertEquals(6, aList.size()); - - aList.remove(1); - assertEquals(11, aList.get(1)); - assertEquals(2, aList.get(2)); - - assertEquals(5, aList.size()); - } - - @Test - public void testAdd() { - for (int i=0; i<100; i++) - aList.add(i); - assertEquals(0, aList.get(0)); - assertEquals(99, aList.get(99)); - assertEquals(44, aList.get(44)); - } - - @Test - public void testRemove() { - aList.add(1); - aList.add(2); - aList.add(3); - int u = (Integer)aList.remove(2); - assertEquals(3, u); - assertEquals(2, aList.size()); - - aList.add(1, 5); - u = (Integer)aList.remove(0); - assertEquals(1, u); - assertEquals(5, aList.get(0)); - assertEquals(2, aList.get(1)); - assertEquals(2, aList.size()); - - aList.remove(0); - aList.remove(0); - assertEquals(0, aList.size()); - - - } - - @Test - public void testSize() { - for (int i=0; i<10; i++) - aList.add(i*2); - assertEquals(10, aList.size()); - } - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public void testException() { - expectedEx.expect(Exception.class); - - aList.remove(1); - aList.add(3); - aList.add(2, 5); - } - - @Test - public void testIterator() { - Iterator it = aList.iterator(); - assertEquals(false, it.hasNext()); - - aList.add(1); - aList.add(2); - aList.add(3); - - it = aList.iterator(); - assertEquals(true, it.hasNext()); - assertEquals(1, it.next()); - assertEquals(2, it.next()); - assertEquals(3, it.next()); - assertEquals(false, it.hasNext()); - - aList.remove(1); - it = aList.iterator(); - assertEquals(true, it.hasNext()); - assertEquals(1, it.next()); - assertEquals(3, it.next()); - assertEquals(false, it.hasNext()); - - expectedEx.expect(Exception.class); - it.next(); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java deleted file mode 100644 index 67035c1dcf..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.github.miniyk2012.coding2017.basic.test; - -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; -import com.github.miniyk2012.coding2017.basic.Queue; - -public class QueueTest { - private Queue queue; - - @Before - public void setUpQueue() { - queue = new Queue(); - } - - @Test - public void testQueueFunctional() { - assertEquals(true, queue.isEmpty()); - queue.enQueue(4); - queue.enQueue(2); - assertEquals(2, queue.size()); - assertEquals(false, queue.isEmpty()); - - int i = (Integer)queue.deQueue(); - assertEquals(4, i); - i = (Integer)queue.deQueue(); - assertEquals(2, i); - - assertEquals(0, queue.size()); - assertEquals(true, queue.isEmpty()); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java deleted file mode 100644 index a9b4e14afd..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.github.miniyk2012.coding2017.basic.test; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -import com.github.miniyk2012.coding2017.basic.Stack; - -public class StackTest { - - private Stack stack; - - @Before - public void setUpStack() { - stack = new Stack(); - } - - @Test - public void testStackFunctional() { - assertEquals(true, stack.isEmpty()); - stack.push(4); - stack.push(2); - assertEquals(2, stack.size()); - assertEquals(false, stack.isEmpty()); - - int i = (Integer)stack.pop(); - assertEquals(2, i); - - i = (Integer)stack.peek(); - assertEquals(4, i); - - i = (Integer)stack.pop(); - assertEquals(4, i); - - assertEquals(0, stack.size()); - assertEquals(true, stack.isEmpty()); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java deleted file mode 100644 index 5d7ad8f4d3..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,279 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.array; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.IntStream; -import java.util.Arrays; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - int size = origin.length; - if (size == 0) return; - int start = 0, end = size-1; - while (start < end) { - int temp = origin[start]; - origin[start++] = origin[end]; - origin[end--] = temp; - } - } - - /** - * 现在有如下的一个数组:int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - public int[] removeZero(int[] oldArray){ - if (oldArray==null) return null; - List list=new ArrayList<>(); - for (int e : oldArray) { - if (e != 0) { - list.add(e); - } - } - return list2Array(list); - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - if (array1==null || array2==null) return null; - if (array1.length == 0) return Arrays.copyOf(array2, array2.length); - List list = array2List(array1); - int currentIndex = 0; - for (int e : array2) { - for (int index = currentIndex; index < list.size(); index++ ) { - currentIndex = index + 1; - if (list.get(index) == e) break; - if (list.get(index) > e) { - list.add(index, e); - break; - } - } - if (e > list.get(list.size()-1)) list.add(e); - } - return list2Array(list); - } - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - * @throws Exception - */ - public int[] grow(int [] oldArray, int size) throws Exception{ - if (oldArray==null) return null; - if (size < 0) throw new Exception(); - int oldSize = oldArray.length; - int[] newArray = new int[size+oldSize]; - System.arraycopy(oldArray, 0, newArray, 0, oldSize); - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - if (max <= 1) return new int[0]; - int i = 1, j = 1; - List list = new ArrayList<>(); - list.add(i); - list.add(j); - int next = i+j; - while (max > next) { - list.add(next); - i = j; - j = next; - next = i+j; - } - return list2Array(list); - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - // TODO 使用筛法,写的不好,有待改善 - if (max <= 2) return new int[0]; - List list = new ArrayList<>(); - int i; - for (i=2; i= 0) - list.remove(index); - k += currentNum; - } - currentNum = list.get(++i); - } - return list2Array(list); - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - List list = new ArrayList<>(); - int[] factors; - for (int i=1; i list = new ArrayList<>(); - for (int i=1; i < num; i++) { - if(num % i == 0) list.add(i); - } - return list2Array(list); - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - if (array.length == 0) return ""; - if (array.length == 1) return "" + array[0]; - StringBuilder s = new StringBuilder(); - for (int i=0; i转换为相同顺序和长度的int[] - * @param list - * @return - */ - private int[] list2Array(List list) { - int size = list.size(); - int[] newArray = new int[size]; - for (int i=0; i - * @param array - * @return - */ - private List array2List(int[] array) { - List list = new ArrayList<>(); - for (int e : array) { - list.add(e); - } - return list; - } - - public static void main(String []args) throws Exception { - ArrayUtil arrayUtil = new ArrayUtil(); - - // merge - int[] a1 = {1,2,3}, a2 = {-4,-2,2,3,4}; -// int[] a1 = {}, a2 = {}; -// int[] a1 = {1,2,3}, a2 = {}; -// int[] a1 = {}, a2 = {1,2,3}; -// int[] a1 = {4,5,6}, a2 = {1,2,3}; - int[] a3 = arrayUtil.merge(a1, a2); - System.out.println(Arrays.toString(a3)); - - // reverse -// a1 = new int[] {}; -// a1 = new int[] {4,}; -// a1 = new int[] {4,3,5,6,7,7,8}; - a1 = new int[] {4,3,5,6,7,7,8, 9}; - arrayUtil.reverseArray(a1); - System.out.println(Arrays.toString(a1)); - - // remove zero -// a1 = new int[] {}; -// a1 = new int[] {0,0}; - a1 = new int[] {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; - a2 = arrayUtil.removeZero(a1); - System.out.println(Arrays.toString(a2)); - - // grow - a1 = new int[] {1,2,3}; - a2 = arrayUtil.grow(a1, 4); -// a2 = arrayUtil.grow(a1, 2); -// a2 = arrayUtil.grow(a1, 0); - System.out.println(Arrays.toString(a2)); - - // fibonacci - a1 = arrayUtil.fibonacci(15); -// a1 = arrayUtil.fibonacci(1); -// a1 = arrayUtil.fibonacci(2); -// a1 = arrayUtil.fibonacci(-2); - System.out.println(Arrays.toString(a1)); - - // prime - a1 = arrayUtil.getPrimes(2); -// a1 = arrayUtil.getPrimes(3); -// a1 = arrayUtil.getPrimes(8); -// a1 = arrayUtil.getPrimes(12); -// a1 = arrayUtil.getPrimes(23); -// a1 = arrayUtil.getPrimes(24); -// a1 = arrayUtil.getPrimes(50); - a1 = arrayUtil.getPrimes(100); - System.out.println(Arrays.toString(a1)); - - // perfectNumbers - a1 = arrayUtil.getPerfectNumbers(1000); - System.out.println(Arrays.toString(a1)); - - // join -// a1 = new int[] {}; -// a1 = new int[] {1}; - a1 = new int[] {1,2,3}; - String str = arrayUtil.join(a1, "-"); - System.out.println(str); - - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java deleted file mode 100644 index 58de9f3efb..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.array.test; - -import static org.junit.Assert.*; - -import java.util.Arrays; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import com.github.miniyk2012.coding2017.coderising.array.ArrayUtil; - -public class ArrayUtilTest -{ - private ArrayUtil myArray; - - @Before - public void setUp() throws Exception - { - myArray = new ArrayUtil(); - } - - @Test - public void testReverseArray() - { - int[] a = {1, 2, 1, 3, 5, 6}; - int[] b = {6, 5, 3, 1, 2, 1}; - - myArray.reverseArray(a); - assertArrayEquals(a, b); - - int[] c = new int[0]; - myArray.reverseArray(c); - assertArrayEquals(c, new int[0]); - - } - - @Test - public void testRemoveZero() - { - int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; - int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; - int[] c = myArray.removeZero(oldArr); - assertArrayEquals(b, c); - - int[] d = null; - int[] e = myArray.removeZero(d); - assertNull(e); - - } - - @Test - public void testMerge() - { - int a1[] = {1, 2, 3, 4, 5}; - int b1[] = {3, 4, 5, 6, 7, 8}; - int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; - int[] newArray1 = myArray.merge(a1, b1); - assertArrayEquals(c1, newArray1); - - int a2[] = new int[0]; - int b2[] = {0, 2, 3, 6, 7, 8}; - int c2[] = {0, 2, 3, 6, 7, 8}; - int[] newArray2 = myArray.merge(a2, b2); - assertArrayEquals(c2, newArray2); - - int a3[] = {0, 2, 3, 6, 7, 8}; - int b3[] = new int[0]; - int c3[] = {0, 2, 3, 6, 7, 8}; - int[] newArray3 = myArray.merge(a3, b3); - assertArrayEquals(c3, newArray3); - - int[] a4 = null; - int[] b4 = null; - int[] newArray4 = myArray.merge(a4, b4); - assertNull(newArray4); - } - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public void testGrow() throws Exception - { - int[] a = {3, 5, 7, 8, 9}; - int [] oldA = Arrays.copyOf(a, a.length); - int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; - int[] newArray = myArray.grow(a, 3); - assertArrayEquals(b, newArray); - assertArrayEquals(a, oldA); - - int[] c = null; - int[] newArray1 = myArray.grow(c, 3); - assertNull(newArray1); - - // size < 0 抛出异常 - expectedEx.expect(Exception.class); - myArray.grow(a, -3); - } - - @Test - public void testFibonacci() - { - //max == 1时返回空数组 - int[] array1 = myArray.fibonacci(1); - int[] b = new int[0]; - assertArrayEquals(array1, b); - - - int[] array2= myArray.fibonacci(35); - int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; - assertArrayEquals(c, array2); - } - - @Test - public void testGetPrimes() - { - int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; - int[] array1 = myArray.getPrimes(35); - assertArrayEquals(a, array1); - - //max <= 2的时候没有素数,数组为空数组 - int[] array2 = myArray.getPrimes(1); - int[] b = new int[0]; - assertArrayEquals(array2, b); - } - - @Test - public void testGetPerfectNumbers() - { - int[] array = myArray.getPerfectNumbers(10000); - int[] a = {6, 28, 496, 8128 }; - assertArrayEquals(a, array); - } - - @Test - public void testJoin() - { - int[] Array0 = {3, 5, 7, 8, 9}; - String s0 = myArray.join(Array0, "-"); - String s1 = "3-5-7-8-9"; - assertEquals(s1, s0); - - int[] Array1 = {3}; - String s2 = myArray.join(Array1, "-"); - String s3 = "3"; - assertEquals(s2, s3); - - //传递空数组时,返回空字符串 - int[] Array2 = new int[0]; - String s4 = myArray.join(Array2, "-"); - String s5 = ""; - assertEquals(s4, s5); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/DownloadThread.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/DownloadThread.java deleted file mode 100644 index c05f264c0b..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/DownloadThread.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download; - -import com.github.miniyk2012.coding2017.coderising.download.api.Connection; -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.concurrent.BrokenBarrierException; -import java.util.concurrent.CyclicBarrier; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - String fileName; - CyclicBarrier barrier; - - public DownloadThread(String name, Connection conn, int startPos, int endPos, String fileName, CyclicBarrier barrier){ - super(name); - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - this.fileName = fileName; - this.barrier = barrier; - } - public void run(){ - try (RandomAccessFile raf = new RandomAccessFile(new File(fileName), "rwd")) { - raf.seek(startPos); - byte[] buf = conn.read(startPos, endPos); -// String desc = Thread.currentThread().getName()+"startPos:"+startPos+",length:"+length + "buf size:"+buf.length; -// System.out.println(desc); - raf.write(buf, 0, buf.length); - if (null != barrier) { - barrier.await(); - } - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (BrokenBarrierException e) { - e.printStackTrace(); - } finally { - conn.close(); - } - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/FileDownloader.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/FileDownloader.java deleted file mode 100644 index 94fa72e741..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/FileDownloader.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download; - -import com.github.miniyk2012.coding2017.coderising.download.api.Connection; -import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionException; -import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; -import com.github.miniyk2012.coding2017.coderising.download.api.DownloadListener; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - - -public class FileDownloader { - - private String url; - private String fileName; - private DownloadListener listener; - private ConnectionManager cm; - private int threadNum = 5; - private int length = 0; - private Connection conn; - - - public FileDownloader(String _url, String _fileName) { - this.url = _url; - this.fileName = _fileName; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - try (RandomAccessFile raf = new RandomAccessFile(new File(fileName), "rwd")) { - conn = cm.open(this.url); - length = conn.getContentLength(); - raf.setLength(length); - threadPoolDownload(); -// oneThreadDownload(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (ConnectionException e) { - e.printStackTrace(); - } - - } - - public void oneThreadDownload() { - final CyclicBarrier barrier = new CyclicBarrier(1 ,new Runnable() { - @Override - public void run() { - getListener().notifyFinished(); - } - }); - try { - Thread thread = new DownloadThread("oneThread", conn,0,length, fileName, barrier); - thread.start(); - } finally { - if (conn != null) { - conn.close(); - } - } - } - - public void threadPoolDownload() throws ConnectionException { - final CyclicBarrier barrier = new CyclicBarrier(threadNum ,new Runnable() { - @Override - public void run() { - getListener().notifyFinished(); // 栅栏 - } - }); - ExecutorService threadPool = Executors.newCachedThreadPool(); - int len = conn.getContentLength(); - for(int i = 0; i< threadNum; i++) - { - int start=i*len/ threadNum; - int end = (i+1)*len/ threadNum -1; - conn = cm.open(this.url); - if(i== threadNum -1) - { - end =len; - } - Thread thread = new DownloadThread("thread"+i, conn, start, end, fileName, barrier); - threadPool.execute(thread); - } - if (conn != null) { - conn.close(); - } - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java deleted file mode 100644 index 501326dae0..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; -import com.github.miniyk2012.coding2017.coderising.download.api.DownloadListener; -import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - private double time = 0; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; -// String url = "https://www.baidu.com/img/bd_logo.png"; - - FileDownloader downloader = new FileDownloader(url, "test.png"); - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - }); - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠0.01秒"); - time += 0.01; - //休眠0.01秒 - Thread.sleep(10); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!耗时"+time+"秒"); - - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/Connection.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/Connection.java deleted file mode 100644 index 9845729e18..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/Connection.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - byte[] read(int startPos, int endPos) throws IOException; - - /** - * 得到数据内容的长度 - * @return - */ - int getContentLength(); - - /** - * 关闭连接 - */ - void close(); -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionException.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionException.java deleted file mode 100644 index 7d185584dc..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download.api; - -public class ConnectionException extends Exception { - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionManager.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionManager.java deleted file mode 100644 index 19bd1a092e..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionManager.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download.api; - -import java.io.IOException; -import java.net.ProtocolException; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - Connection open(String url) throws ConnectionException; -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/DownloadListener.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/DownloadListener.java deleted file mode 100644 index 1488a94f22..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download.api; - -public interface DownloadListener { - void notifyFinished(); -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImpl.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImpl.java deleted file mode 100644 index b928e0ced7..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download.impl; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.Arrays; - -import com.github.miniyk2012.coding2017.basic.ArrayList; -import com.github.miniyk2012.coding2017.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - private HttpURLConnection downLoadConn; - private HttpURLConnection getLengthConn; - - public ConnectionImpl(URL urlObject) { - HttpURLConnection conn = null; - try { - downLoadConn = (HttpURLConnection) urlObject.openConnection(); - downLoadConn.setRequestMethod("GET"); - - getLengthConn = (HttpURLConnection) urlObject.openConnection(); - getLengthConn.setRequestMethod("GET"); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - downLoadConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - - InputStream in = downLoadConn.getInputStream(); - byte[] buf = new byte[endPos-startPos+1]; - byte[] tempBuf = new byte[1024]; - BufferedInputStream bis = new BufferedInputStream(in); - int len = 0; - int totalLen = 0; - while((len=bis.read(tempBuf,0,tempBuf.length))!=-1){ - System.arraycopy(tempBuf, 0, buf, totalLen, len); - totalLen += len; - } - String desc = " bytes=" + startPos + "-" + endPos + " "; - System.out.println(Thread.currentThread().getName()+desc+totalLen); - in.close(); - bis.close(); - return Arrays.copyOf(buf, totalLen); - } - - @Override - public int getContentLength() { - int len = getLengthConn.getContentLength(); - return len; - } - - @Override - public void close() { - downLoadConn.disconnect(); - getLengthConn.disconnect(); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index c7870f9127..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.download.impl; - -import com.github.miniyk2012.coding2017.coderising.download.api.Connection; -import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionException; -import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; - -import java.io.IOException; -import java.net.URL; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - URL urlObject; - try { - urlObject = new URL(url); - return new ConnectionImpl(urlObject); - } catch (IOException e) { - e.printStackTrace(); - throw new ConnectionException(); - } - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java deleted file mode 100644 index b89a242df6..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public void setMessage(String message) { - this.message = message; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } - - @Override - public String toString() { - return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]"; - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java deleted file mode 100644 index f283373b48..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LogoutAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public void setMessage(String message) { - this.message = message; - } - - @Override - public String toString() { - return "LogoutAction [name=" + name + ", password=" + password + ", message=" + message + "]"; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "logout successful"; - return "success"; - } - this.message = "logout failed,please check your user/pwd"; - return "error"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java deleted file mode 100644 index 54e8468077..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.litestruts; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.logging.Logger; -import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.beans.IntrospectionException; -import java.beans.PropertyDescriptor; -import java.lang.reflect.Field; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.io.SAXReader; -import org.dom4j.Element; - - -public class Struts { - - /** dom4j object model representation of a xml document. Note: We use the interface(!) not its implementation */ - private static Document doc; - private static Element aElement; - private static Object object; - private static View view; - private static final Logger logger = Logger.getLogger(Struts.class.getName()); - - public static View runAction(String actionName, Map parameters) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - readXml(); - String retValue = processAction(actionName, parameters); - view = generateView(retValue); - return view; - } - - private static View generateView(String retValue) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - view = new View(); - Map map = getFields(); - String jsp = getJsp(retValue); - view.setParameters(map); - view.setJsp(jsp); - return view; - } - - private static String getJsp(String retValue) { - for (Iterator i = aElement.elementIterator( "result" ); i.hasNext();) { - Element result = (Element) i.next(); - if (result.attributeValue("name").equals(retValue)) { - return result.getText(); - } - } - return ""; - } - - /** - * @return - * @throws IntrospectionException - * @throws IllegalAccessException - * @throws InvocationTargetException - */ - private static Map getFields() - throws IntrospectionException, IllegalAccessException, InvocationTargetException { - Map map = new HashMap<>(); - Class clazz = object.getClass(); - Field[] fields = object.getClass().getDeclaredFields();//获得属性 - for (Field field : fields) { - PropertyDescriptor pd = new PropertyDescriptor(field.getName(), - clazz); - Method getMethod = pd.getReadMethod();//获得get方法 - String value = (String) getMethod.invoke(object);//执行get方法返回一个Object - map.put(field.getName(), value); - } - return map; - } - - private static void readXml() throws DocumentException { - String fileName = Thread.currentThread().getContextClassLoader().getResource("").getPath() - + "com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml"; - File aFile = new File(fileName); - SAXReader xmlReader = new SAXReader(); - doc = xmlReader.read(aFile); - } - - private static String processAction(String actionName, Map parameters) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { - generateObject(actionName); - setFields(parameters); - return doExecute(); - } - - /** - * @return - * @throws NoSuchMethodException - * @throws IllegalAccessException - * @throws InvocationTargetException - */ - private static String doExecute() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { - Class c = object.getClass(); - Method method = c.getMethod("execute"); - String ret = (String) method.invoke(object); - return ret; - } - - /** - * @param parameters - * @throws NoSuchMethodException - * @throws IllegalAccessException - * @throws InvocationTargetException - */ - private static void setFields(Map parameters) - throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { - for (Map.Entry entry: parameters.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - key = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); - Class c = object.getClass(); - Method method = c.getMethod(key, String.class); - method.invoke(object, value); - } - } - - /** - * @param actionName - * @throws InstantiationException - * @throws IllegalAccessException - * @throws ClassNotFoundException - */ - private static void generateObject(String actionName) - throws InstantiationException, IllegalAccessException, ClassNotFoundException { - Element root = doc.getRootElement(); - String className = null; - for ( Iterator i = root.elementIterator(); i.hasNext(); ) { - Element actionElement = (Element) i.next(); - if (actionElement.attributeValue("name").equals(actionName)) { - aElement = actionElement; - className = actionElement.attributeValue("class"); - break; - } - } - if (className == null) throw new InstantiationException("no such className"); - object = Class.forName(className).newInstance(); - } - - - public static void main(String args[]) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException - { - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - View view = runAction("login", params); - logger.info(view.toString()); - } - -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java deleted file mode 100644 index 9443a32d97..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.litestruts; - -import java.beans.IntrospectionException; -import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; -import java.util.Map; - -import org.dom4j.DocumentException; -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } - - @Test - public void testLogoutActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { - - String actionName = "logout"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); - Assert.assertEquals("logout successful", view.getParameters().get("message")); - } - - @Test - public void testLogoutActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { - String actionName = "logout"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/error.jsp", view.getJsp()); - Assert.assertEquals("logout failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java deleted file mode 100644 index 46759e7d01..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.miniyk2012.coding2017.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } - @Override - public String toString() { - return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; - } -} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml deleted file mode 100644 index 0d3fd6495f..0000000000 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/ArrayList.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..dd72d043fb --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.github.miniyk2012.coding2017.basic; + +import java.util.Arrays; + + +public class ArrayList implements List { + + private int size = 0; + private int DEFAULT_LENGTH = 10; + + private Object[] elementData = new Object[DEFAULT_LENGTH]; + + public void add(Object o){ + ensureCapcacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + ensurnPosition(index); + ensureCapcacity(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + ensureIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + ensureIndex(index); + Object temp = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, + size - index - 1); + elementData[size-1] = null; + size--; + return temp; + } + + public void clear() { + elementData = new Object[DEFAULT_LENGTH]; + size = 0; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + Iterator iterator = new IteratorImp(this); + return iterator; + } + + private void ensureCapcacity() { + int oldLength = elementData.length; + if (size+1 > oldLength) { + elementData = Arrays.copyOf(elementData, 2*oldLength); + } + } + + private void ensureIndex(int index) { + if (index >= size || index < 0) + throw new ArrayIndexOutOfBoundsException(String.format("index %d out of bounds [0-%d)", index, size)); + } + + private void ensurnPosition(int index) { + if (index <0 || index>size) + throw new ArrayIndexOutOfBoundsException(String.format("position %d out of position [0-%d)", index, size)); + } + + @Override + public String toString() { + Object[] tempArray = Arrays.copyOf(elementData, size); + return Arrays.toString(tempArray); + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7c78767fb0 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +package com.github.miniyk2012.coding2017.basic; + +public class BinaryTreeNode > { + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(E x) { + data = x; + } + public E getData() { + return data; + } + public void setData(E data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(E o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode currentNode = this; + BinaryTreeNode previousNode = null; + + while (currentNode != null) { + previousNode = currentNode; + int compareTo = node.getData().compareTo(currentNode.getData()); + if (compareTo <= 0) { // 小于,往左插入 + currentNode = currentNode.left; + left = true; + } else { + currentNode = currentNode.right; + left = false; + } + } + if (left) { + previousNode.left = node; + } else { + previousNode.right = node; + } + return node; + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Iterator.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..589532f42d --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.miniyk2012.coding2017.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/IteratorImp.java similarity index 100% rename from group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java rename to group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/IteratorImp.java diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/LinkedList.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..09e4e1b941 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/LinkedList.java @@ -0,0 +1,365 @@ +package com.github.miniyk2012.coding2017.basic; +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Objects; + +import org.omg.CORBA.PRIVATE_MEMBER; + +import com.sun.tracing.dtrace.ArgsAttributes; + +import utils.ArrayUtils; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + /** + * node接收的index一定是范围内的值,不可能越界 + * @param index + * @return a Node + */ + Node node(int index) { + Node x = head; + for (int i=0; i= size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + + private void checkPositionIndex(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + // 自感写的还行,没有多余代码 + Node previous = null; + while (head != null) { + Node next = head.next; + head.next = previous; + previous = head; + head = next; + } + head = previous; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int halfIndex = size() / 2; + head = node(halfIndex); + size = size() - halfIndex; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + * @throws Exception + */ + public void remove(int i, int length) throws Exception{ + if (length <0 || i <0 || i>=size()) throw new Exception(); + if (length == 0) return; + if (i+length>size()) throw new Exception(); + + Node after = (i+length==size()?null:node(i+length)); + if (i>=1) { + node(i-1).next = after; + } else { + head = after; + } + + size -= length; + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + * @throws Exception + */ + public int[] getElements(LinkedList list) throws Exception { + // 不允许用get方法取得当前链表的元素,重分利用当前链表和list的有序性 + if (list.size == 0) return new int[0]; + if ((int)list.get(0)<0 || (int)list.get(list.size-1)>=size) { + throw new Exception(); + } + java.util.LinkedList aStandardList = new java.util.LinkedList<>(); + Node current = head; + for (int i=0,j=0; i bValue) { + b++; + } else if (aValue < bValue) { + a++; + } else { + cList.add(a); + a++; + b++; + } + } + return cList; + } + + public static LinkedList copy(LinkedList list) { + LinkedList copy = new LinkedList(); + Iterator it = list.iterator(); + while (it.hasNext()) { + copy.add(it.next()); + } + return copy; + } + + public static void main(String args[]) throws Exception { + LinkedList aLinkedList = new LinkedList(); + for (int i=0; i<4; i=i*i) { + aLinkedList.add(i); // [0,1,4,9] + } + LinkedList bLinkedList = new LinkedList(); + int[] z1 = aLinkedList.getElements(bLinkedList); // [] + System.out.println(Arrays.toString(z1)); + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/List.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/List.java new file mode 100644 index 0000000000..63db6ed431 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/List.java @@ -0,0 +1,10 @@ +package com.github.miniyk2012.coding2017.basic; + +public interface List { + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); + Iterator iterator(); +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Queue.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Queue.java new file mode 100644 index 0000000000..9f22f2471d --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Queue.java @@ -0,0 +1,23 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Queue { + + // 队列入口 -》1,2,3,4 -》队列出口 + private LinkedList aList = new LinkedList(); + + public void enQueue(Object o){ + aList.addFirst(o); + } + + public Object deQueue(){ + return aList.removeLast(); + } + + public boolean isEmpty(){ + return aList.size() == 0; + } + + public int size(){ + return aList.size(); + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Stack.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Stack.java new file mode 100644 index 0000000000..b2f2d6366b --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Stack { + + // 栈顶 《-》 1,2,3,4 栈底 + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0, o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..05bd6716b1 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,279 @@ +package com.github.miniyk2012.coding2017.coderising.array; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.IntStream; +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int size = origin.length; + if (size == 0) return; + int start = 0, end = size-1; + while (start < end) { + int temp = origin[start]; + origin[start++] = origin[end]; + origin[end--] = temp; + } + } + + /** + * 现在有如下的一个数组:int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public int[] removeZero(int[] oldArray){ + if (oldArray==null) return null; + List list=new ArrayList<>(); + for (int e : oldArray) { + if (e != 0) { + list.add(e); + } + } + return list2Array(list); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + if (array1==null || array2==null) return null; + if (array1.length == 0) return Arrays.copyOf(array2, array2.length); + List list = array2List(array1); + int currentIndex = 0; + for (int e : array2) { + for (int index = currentIndex; index < list.size(); index++ ) { + currentIndex = index + 1; + if (list.get(index) == e) break; + if (list.get(index) > e) { + list.add(index, e); + break; + } + } + if (e > list.get(list.size()-1)) list.add(e); + } + return list2Array(list); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + * @throws Exception + */ + public int[] grow(int [] oldArray, int size) throws Exception{ + if (oldArray==null) return null; + if (size < 0) throw new Exception(); + int oldSize = oldArray.length; + int[] newArray = new int[size+oldSize]; + System.arraycopy(oldArray, 0, newArray, 0, oldSize); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max <= 1) return new int[0]; + int i = 1, j = 1; + List list = new ArrayList<>(); + list.add(i); + list.add(j); + int next = i+j; + while (max > next) { + list.add(next); + i = j; + j = next; + next = i+j; + } + return list2Array(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + // TODO 使用筛法,写的不好,有待改善 + if (max <= 2) return new int[0]; + List list = new ArrayList<>(); + int i; + for (i=2; i= 0) + list.remove(index); + k += currentNum; + } + currentNum = list.get(++i); + } + return list2Array(list); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList<>(); + int[] factors; + for (int i=1; i list = new ArrayList<>(); + for (int i=1; i < num; i++) { + if(num % i == 0) list.add(i); + } + return list2Array(list); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + if (array.length == 0) return ""; + if (array.length == 1) return "" + array[0]; + StringBuilder s = new StringBuilder(); + for (int i=0; i转换为相同顺序和长度的int[] + * @param list + * @return + */ + private int[] list2Array(List list) { + int size = list.size(); + int[] newArray = new int[size]; + for (int i=0; i + * @param array + * @return + */ + private List array2List(int[] array) { + List list = new ArrayList<>(); + for (int e : array) { + list.add(e); + } + return list; + } + + public static void main(String []args) throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + + // merge + int[] a1 = {1,2,3}, a2 = {-4,-2,2,3,4}; +// int[] a1 = {}, a2 = {}; +// int[] a1 = {1,2,3}, a2 = {}; +// int[] a1 = {}, a2 = {1,2,3}; +// int[] a1 = {4,5,6}, a2 = {1,2,3}; + int[] a3 = arrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(a3)); + + // reverse +// a1 = new int[] {}; +// a1 = new int[] {4,}; +// a1 = new int[] {4,3,5,6,7,7,8}; + a1 = new int[] {4,3,5,6,7,7,8, 9}; + arrayUtil.reverseArray(a1); + System.out.println(Arrays.toString(a1)); + + // remove zero +// a1 = new int[] {}; +// a1 = new int[] {0,0}; + a1 = new int[] {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + a2 = arrayUtil.removeZero(a1); + System.out.println(Arrays.toString(a2)); + + // grow + a1 = new int[] {1,2,3}; + a2 = arrayUtil.grow(a1, 4); +// a2 = arrayUtil.grow(a1, 2); +// a2 = arrayUtil.grow(a1, 0); + System.out.println(Arrays.toString(a2)); + + // fibonacci + a1 = arrayUtil.fibonacci(15); +// a1 = arrayUtil.fibonacci(1); +// a1 = arrayUtil.fibonacci(2); +// a1 = arrayUtil.fibonacci(-2); + System.out.println(Arrays.toString(a1)); + + // prime + a1 = arrayUtil.getPrimes(2); +// a1 = arrayUtil.getPrimes(3); +// a1 = arrayUtil.getPrimes(8); +// a1 = arrayUtil.getPrimes(12); +// a1 = arrayUtil.getPrimes(23); +// a1 = arrayUtil.getPrimes(24); +// a1 = arrayUtil.getPrimes(50); + a1 = arrayUtil.getPrimes(100); + System.out.println(Arrays.toString(a1)); + + // perfectNumbers + a1 = arrayUtil.getPerfectNumbers(1000); + System.out.println(Arrays.toString(a1)); + + // join +// a1 = new int[] {}; +// a1 = new int[] {1}; + a1 = new int[] {1,2,3}; + String str = arrayUtil.join(a1, "-"); + System.out.println(str); + + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/DownloadThread.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..9c17846b42 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/DownloadThread.java @@ -0,0 +1,46 @@ +package com.github.miniyk2012.coding2017.coderising.download; + +import com.github.miniyk2012.coding2017.coderising.download.api.Connection; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String fileName; + CyclicBarrier barrier; + + public DownloadThread(String name, Connection conn, int startPos, int endPos, String fileName, CyclicBarrier barrier){ + super(name); + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.fileName = fileName; + this.barrier = barrier; + } + public void run(){ + try (RandomAccessFile raf = new RandomAccessFile(new File(fileName), "rwd")) { + raf.seek(startPos); + byte[] buf = conn.read(startPos, endPos); +// String desc = Thread.currentThread().getName()+"startPos:"+startPos+",length:"+length + "buf size:"+buf.length; +// System.out.println(desc); + raf.write(buf, 0, buf.length); + if (null != barrier) { + barrier.await(); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } finally { + conn.close(); + } + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloader.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..a0d76cbf92 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloader.java @@ -0,0 +1,118 @@ +package com.github.miniyk2012.coding2017.coderising.download; + +import com.github.miniyk2012.coding2017.coderising.download.api.Connection; +import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionException; +import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; +import com.github.miniyk2012.coding2017.coderising.download.api.DownloadListener; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + + +public class FileDownloader { + + private String url; + private String fileName; + private DownloadListener listener; + private ConnectionManager cm; + private int threadNum = 5; + private int length = 0; + private Connection conn; + + + public FileDownloader(String _url, String _fileName) { + this.url = _url; + this.fileName = _fileName; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + try (RandomAccessFile raf = new RandomAccessFile(new File(fileName), "rwd")) { + conn = cm.open(this.url); + length = conn.getContentLength(); + raf.setLength(length); + threadPoolDownload(); +// oneThreadDownload(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ConnectionException e) { + e.printStackTrace(); + } + + } + + public void oneThreadDownload() { + final CyclicBarrier barrier = new CyclicBarrier(1 ,new Runnable() { + @Override + public void run() { + getListener().notifyFinished(); + } + }); + try { + Thread thread = new DownloadThread("oneThread", conn,0,length, fileName, barrier); + thread.start(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + public void threadPoolDownload() throws ConnectionException { + final CyclicBarrier barrier = new CyclicBarrier(threadNum ,new Runnable() { + @Override + public void run() { + getListener().notifyFinished(); // 栅栏 + } + }); + ExecutorService threadPool = Executors.newCachedThreadPool(); + int len = conn.getContentLength(); + for(int i = 0; i< threadNum; i++) + { + int start=i*len/ threadNum; + int end = (i+1)*len/ threadNum -1; + conn = cm.open(this.url); + if(i== threadNum -1) + { + end =len; + } + Thread thread = new DownloadThread("thread"+i, conn, start, end, fileName, barrier); + threadPool.execute(thread); + } + if (conn != null) { + conn.close(); + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/Connection.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/Connection.java new file mode 100644 index 0000000000..93ad40bc91 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/Connection.java @@ -0,0 +1,24 @@ +package com.github.miniyk2012.coding2017.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + byte[] read(int startPos, int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * @return + */ + int getContentLength(); + + /** + * 关闭连接 + */ + void close(); +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionException.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..5fc42ce667 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.github.miniyk2012.coding2017.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionManager.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..7477455e8c --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/ConnectionManager.java @@ -0,0 +1,13 @@ +package com.github.miniyk2012.coding2017.coderising.download.api; + +import java.io.IOException; +import java.net.ProtocolException; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + Connection open(String url) throws ConnectionException; +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/DownloadListener.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..60c5c38ffe --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.github.miniyk2012.coding2017.coderising.download.api; + +public interface DownloadListener { + void notifyFinished(); +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImpl.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..9076b09e60 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,64 @@ +package com.github.miniyk2012.coding2017.coderising.download.impl; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Arrays; + +import com.github.miniyk2012.coding2017.basic.ArrayList; +import com.github.miniyk2012.coding2017.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + private HttpURLConnection downLoadConn; + private HttpURLConnection getLengthConn; + + public ConnectionImpl(URL urlObject) { + HttpURLConnection conn = null; + try { + downLoadConn = (HttpURLConnection) urlObject.openConnection(); + downLoadConn.setRequestMethod("GET"); + + getLengthConn = (HttpURLConnection) urlObject.openConnection(); + getLengthConn.setRequestMethod("GET"); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + downLoadConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream in = downLoadConn.getInputStream(); + byte[] buf = new byte[endPos-startPos+1]; + byte[] tempBuf = new byte[1024]; + BufferedInputStream bis = new BufferedInputStream(in); + int len = 0; + int totalLen = 0; + while((len=bis.read(tempBuf,0,tempBuf.length))!=-1){ + System.arraycopy(tempBuf, 0, buf, totalLen, len); + totalLen += len; + } + String desc = " bytes=" + startPos + "-" + endPos + " "; + System.out.println(Thread.currentThread().getName()+desc+totalLen); + in.close(); + bis.close(); + return Arrays.copyOf(buf, totalLen); + } + + @Override + public int getContentLength() { + int len = getLengthConn.getContentLength(); + return len; + } + + @Override + public void close() { + downLoadConn.disconnect(); + getLengthConn.disconnect(); + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..6766335baf --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,23 @@ +package com.github.miniyk2012.coding2017.coderising.download.impl; + +import com.github.miniyk2012.coding2017.coderising.download.api.Connection; +import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionException; +import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + URL urlObject; + try { + urlObject = new URL(url); + return new ConnectionImpl(urlObject); + } catch (IOException e) { + e.printStackTrace(); + throw new ConnectionException(); + } + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b0322078ee --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java @@ -0,0 +1,48 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public void setMessage(String message) { + this.message = message; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } + + @Override + public String toString() { + return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..3a8bf9fbc9 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java @@ -0,0 +1,48 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LogoutAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "LogoutAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "logout successful"; + return "success"; + } + this.message = "logout failed,please check your user/pwd"; + return "error"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..523f43b275 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,173 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.logging.Logger; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.dom4j.Element; + + +public class Struts { + + /** dom4j object model representation of a xml document. Note: We use the interface(!) not its implementation */ + private static Document doc; + private static Element aElement; + private static Object object; + private static View view; + private static final Logger logger = Logger.getLogger(Struts.class.getName()); + + public static View runAction(String actionName, Map parameters) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + readXml(); + String retValue = processAction(actionName, parameters); + view = generateView(retValue); + return view; + } + + private static View generateView(String retValue) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + view = new View(); + Map map = getFields(); + String jsp = getJsp(retValue); + view.setParameters(map); + view.setJsp(jsp); + return view; + } + + private static String getJsp(String retValue) { + for (Iterator i = aElement.elementIterator( "result" ); i.hasNext();) { + Element result = (Element) i.next(); + if (result.attributeValue("name").equals(retValue)) { + return result.getText(); + } + } + return ""; + } + + /** + * @return + * @throws IntrospectionException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static Map getFields() + throws IntrospectionException, IllegalAccessException, InvocationTargetException { + Map map = new HashMap<>(); + Class clazz = object.getClass(); + Field[] fields = object.getClass().getDeclaredFields();//获得属性 + for (Field field : fields) { + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), + clazz); + Method getMethod = pd.getReadMethod();//获得get方法 + String value = (String) getMethod.invoke(object);//执行get方法返回一个Object + map.put(field.getName(), value); + } + return map; + } + + private static void readXml() throws DocumentException { + String fileName = Thread.currentThread().getContextClassLoader().getResource("").getPath() + + "com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml"; + File aFile = new File(fileName); + SAXReader xmlReader = new SAXReader(); + doc = xmlReader.read(aFile); + } + + private static String processAction(String actionName, Map parameters) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + generateObject(actionName); + setFields(parameters); + return doExecute(); + } + + /** + * @return + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static String doExecute() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Class c = object.getClass(); + Method method = c.getMethod("execute"); + String ret = (String) method.invoke(object); + return ret; + } + + /** + * @param parameters + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static void setFields(Map parameters) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (Map.Entry entry: parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + key = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Class c = object.getClass(); + Method method = c.getMethod(key, String.class); + method.invoke(object, value); + } + } + + /** + * @param actionName + * @throws InstantiationException + * @throws IllegalAccessException + * @throws ClassNotFoundException + */ + private static void generateObject(String actionName) + throws InstantiationException, IllegalAccessException, ClassNotFoundException { + Element root = doc.getRootElement(); + String className = null; + for ( Iterator i = root.elementIterator(); i.hasNext(); ) { + Element actionElement = (Element) i.next(); + if (actionElement.attributeValue("name").equals(actionName)) { + aElement = actionElement; + className = actionElement.attributeValue("class"); + break; + } + } + if (className == null) throw new InstantiationException("no such className"); + object = Class.forName(className).newInstance(); + } + + + public static void main(String args[]) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException + { + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = runAction("login", params); + logger.info(view.toString()); + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/View.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..7dacd0d9fe --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } +} diff --git a/group02/812350401/src/utils/ArrayUtils.java b/group02/812350401/src/main/java/utils/ArrayUtils.java similarity index 100% rename from group02/812350401/src/utils/ArrayUtils.java rename to group02/812350401/src/main/java/utils/ArrayUtils.java diff --git a/group02/812350401/src/test/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImplTest.java b/group02/812350401/src/test/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImplTest.java deleted file mode 100644 index 6007cf7b94..0000000000 --- a/group02/812350401/src/test/com/github/miniyk2012/coding2017/coderising/download/impl/ConnectionImplTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package test.com.github.miniyk2012.coding2017.coderising.download.impl; - -import com.github.miniyk2012.coding2017.coderising.download.api.Connection; -import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionImpl; -import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionManagerImpl; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import java.net.URL; - -/** -* ConnectionImpl Tester. -* -* @author -* @since
Mar 12, 2017
-* @version 1.0 -*/ -public class ConnectionImplTest { - String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; - ConnectionManagerImpl connectionManager = null; - Connection connection = null; - - @Before - public void before() throws Exception { - connectionManager = new ConnectionManagerImpl(); - connection = connectionManager.open(url); - - } - - @After - public void after() { - connection.close(); - } - /** - * - * Method: read(int startPos, int endPos) - * - */ - @Test - public void testRead() throws Exception { - int length = connection.getContentLength(); - byte[] biz = connection.read(0, length-1); - System.out.println(biz.length); - } - - /** - * - * Method: getContentLength() - * - */ - @Test - public void testGetContentLength() throws Exception { - int length = connection.getContentLength(); - System.out.println(length); - } - -} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ArrayListTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..934ecedcb2 --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ArrayListTest.java @@ -0,0 +1,13 @@ +package com.github.miniyk2012.coding2017.basic; + +import org.junit.Before; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/BinaryTreeNodeTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..205fbe3bb5 --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,70 @@ +package com.github.miniyk2012.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + /** + // 4 + // 1 5 + // 2 3 + */ + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + } + + @Test + public void testBinaryTreeNodeFunctional1() { + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); + } + + @Test + public void testBinaryTreeFunctional2() { + BinaryTreeNode node1 = binaryTreeNode.getLeft(); + assertEquals(new Integer(1), node1.getData()); + assertEquals(new Integer(3), node1.getRight().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testBinaryTreeFunctional3() + { + BinaryTreeNode treeNode = new BinaryTreeNode(100); + treeNode.insert(10); + binaryTreeNode.setRight(treeNode); + // 4 + // 1 100 + // 2 3 10 + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); + assertEquals(new Integer(100), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(10), binaryTreeNode.getRight().getLeft().getData()); + + expectedEx.expect(Exception.class); + binaryTreeNode.getRight().getRight().getRight(); // null exception + binaryTreeNode.getRight().getRight().getLeft(); // null exception + } +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/LinkedListTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..5ee2df36a2 --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/LinkedListTest.java @@ -0,0 +1,401 @@ +package com.github.miniyk2012.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + /** + * 测试iterator不因为各种操作而失效 + */ + @After + public void testIterator2() { + java.util.LinkedList aStandardList = new java.util.LinkedList(); + for (int i=0; i5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + @Test + public void testRemoveFirstHalf() { + aLinkedList.removeFirstHalf(); + assertEquals(0, aLinkedList.size()); + + aLinkedList.add(2); + aLinkedList.add(5); + aLinkedList.add(7); + aLinkedList.add(8); // [2,5,7,8] + + aLinkedList.removeFirstHalf(); // [7,8] + assertEquals(2, aLinkedList.size()); + assertEquals(7, aLinkedList.get(0)); + assertEquals(8, aLinkedList.get(1)); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + * @throws Exception + */ + @Test + public void testRemoveIntInt() throws Exception { + + for (int i=0; i<4; i++) { + aLinkedList.add(i); // [0,1,2,3] + } + + aLinkedList.remove(0, 2); // [2,3] + assertEquals(2, aLinkedList.get(0)); + assertEquals(3, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + + aLinkedList.remove(1, 0); + aLinkedList.remove(0, 0); + assertEquals(2, aLinkedList.size()); + + aLinkedList.remove(1, 1); // [2] + assertEquals(1, aLinkedList.size()); + assertEquals(2, aLinkedList.get(0)); + + aLinkedList.remove(0, 1); // [] + assertEquals(0, aLinkedList.size()); + + expectedEx.expect(Exception.class); + aLinkedList.remove(1, 3); + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + * @throws Exception + */ + @Test + public void testGetElements() throws Exception { + for (int i=0; i<4; i=i+1) { + aLinkedList.add(i*i); // [0,1,4,9] + } + LinkedList bLinkedList = new LinkedList(); + int[] z1 = aLinkedList.getElements(bLinkedList); // [] + assertArrayEquals(new int[0], z1); + + bLinkedList.add(1); + bLinkedList.add(3); // [1, 3] + + z1 = aLinkedList.getElements(bLinkedList); // [1, 9] + assertArrayEquals(new int[] {1,9}, z1); + + bLinkedList.add(1, 2); // bLinkedList = [1, 2, 3] + z1 = aLinkedList.getElements(bLinkedList); // [1, 4, 9] + assertArrayEquals(new int[] {1,4,9}, z1); + + bLinkedList.add(0, 0); // bLinkedList = [0, 1, 2, 3] + z1 = aLinkedList.getElements(bLinkedList); // [0, 1, 4, 9] + assertArrayEquals(new int[] {0,1,4,9}, z1); + + // aLinkedList不应该变化 + assertEquals(4, aLinkedList.size()); + for (int i=0; i<4; i++) { + assertEquals(i*i, aLinkedList.get(i)); // [0,1,4,9] + } + + // Exception + bLinkedList.add(5); // bLinkedList = [0, 1, 2, 3, 5] + expectedEx.expect(Exception.class); + z1 = aLinkedList.getElements(bLinkedList); + } + + @Test + public void TestSubtract() + { + //传进的list为null,什么都不干 + LinkedList list = null; + for (int i=0; i<6; i++) + { + aLinkedList.add(i); //[0,1,2,3,4,5] + } + aLinkedList.subtract(list); + assertEquals(6, aLinkedList.size()); + + //传进的list为空 + list = new LinkedList(); + aLinkedList.subtract(list); + assertEquals(6, aLinkedList.size()); + + aLinkedList.add(1, 1); //[0,1,1,2,3,4,5] + aLinkedList.add(4, 3); //[0,1, 1, 2, 3, 3, 4, 5] + + + // list添加元素[0,1, 6, 3] + list.add(0); + list.add(1); + list.add(6); + list.add(3); + + aLinkedList.subtract(list); //[ 2, 4, 5] + + assertEquals(2, aLinkedList.get(0)); + assertEquals(4, aLinkedList.get(1)); + assertEquals(5, aLinkedList.get(2)); + assertEquals(3, aLinkedList.size()); + + list = new LinkedList(); + list.add(7); + list.add(4); + list.add(2); + list.add(2); // [7,4,2,2] + + aLinkedList.subtract(list); // [5] + + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + list = new LinkedList(); + list.add(5); + list.add(5); + list.add(5); // [5,5,5] + + aLinkedList.subtract(list); // [] + assertEquals(0, aLinkedList.size()); + + aLinkedList.subtract(list); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveDuplicateValues() + { + aLinkedList.add(3); + aLinkedList.add(3); + aLinkedList.add(4); + aLinkedList.add(4); + aLinkedList.add(5); + aLinkedList.add(6); + aLinkedList.add(6); //[3, 3, 4, 4, 5, 6, 6] + assertEquals(7, aLinkedList.size()); + + aLinkedList.removeDuplicateValues(); //[3, 4, 5, 6] + + assertEquals(3, aLinkedList.get(0)); + assertEquals(4, aLinkedList.get(1)); + assertEquals(5, aLinkedList.get(2)); + assertEquals(6, aLinkedList.get(3)); + assertEquals(4, aLinkedList.size()); + + aLinkedList = new LinkedList(); + aLinkedList.removeDuplicateValues(); + assertEquals(0, aLinkedList.size()); + + aLinkedList = new LinkedList(); + aLinkedList.add(0); + aLinkedList.removeDuplicateValues(); + assertEquals(1, aLinkedList.size()); + assertEquals(0, aLinkedList.get(0)); + + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [0,1,2,3] + } + aLinkedList.removeDuplicateValues(); + assertEquals(4, aLinkedList.size()); + assertEquals(0, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.get(2)); + assertEquals(3, aLinkedList.get(3)); + + + } + + @Test + public void testRemoveRange() throws Exception + { + for (int i=0; i<6; i++) + { + aLinkedList.add(i); // [0, 1, 2, 3, 4, 5] //考虑重复元素 + } + aLinkedList.addFirst(0); // [0, 0, 1, 2, 3, 4, 5] + aLinkedList.add(3, 2); // [0, 0, 1, 2, 2, 3, 4, 5] + + aLinkedList.removeRange(1, 4); // 大于1小于4 [0, 0, 1, 4, 5] + + assertEquals(0, aLinkedList.get(0)); + assertEquals(0, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + assertEquals(5, aLinkedList.get(4)); + assertEquals(5, aLinkedList.size()); + + //若出现 min >= max的情况,什么都不做 + aLinkedList.removeRange(4, 1); + assertEquals(0, aLinkedList.get(0)); + assertEquals(0, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + assertEquals(5, aLinkedList.get(4)); + assertEquals(5, aLinkedList.size()); + + // 将整个链表中的元素删除 + aLinkedList.removeRange(-1, 9); + assertEquals(0, aLinkedList.size()); + + } + + @Test + public void testIntersection() { + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); + } + aLinkedList.add(6); + aLinkedList.add(7); // [0, 1, 2, 3, 4, 5, 6, 7] + // list为null + LinkedList list = null; + LinkedList newList1 = aLinkedList.intersection(list); + assertNull(newList1); + + // list为空链表 + list = new LinkedList(); + LinkedList newList2 = aLinkedList.intersection(list); + assertEquals(0, newList2.size()); + + list.add(0); + list.add(3); + list.add(4); + list.add(7); + list.add(8); // [0, 3, 4, 7, 8] + LinkedList newList3 = aLinkedList.intersection(list); + + assertEquals(4, newList3.size()); + assertEquals(0, newList3.get(0)); + assertEquals(3, newList3.get(1)); + assertEquals(4, newList3.get(2)); + assertEquals(7, newList3.get(3)); + } + +} + diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java new file mode 100644 index 0000000000..87a8cdddb3 --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java @@ -0,0 +1,117 @@ +package com.github.miniyk2012.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.basic.Iterator; +import com.github.miniyk2012.coding2017.basic.List; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/QueueTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..132334066a --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.miniyk2012.coding2017.basic; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/StackTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..9fdde63598 --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/StackTest.java @@ -0,0 +1,40 @@ +package com.github.miniyk2012.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.miniyk2012.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/array/ArrayUtilTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..5e72c9ab5d --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/array/ArrayUtilTest.java @@ -0,0 +1,156 @@ +package com.github.miniyk2012.coding2017.coderising.array; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.coderising.array.ArrayUtil; + +public class ArrayUtilTest +{ + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception + { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() throws Exception + { + int[] a = {3, 5, 7, 8, 9}; + int [] oldA = Arrays.copyOf(a, a.length); + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + assertArrayEquals(a, oldA); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 抛出异常 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() + { + //max == 1时返回空数组 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2的时候没有素数,数组为空数组 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //传递空数组时,返回空字符串 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/ConnectionImplTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/ConnectionImplTest.java new file mode 100644 index 0000000000..96892a86df --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/ConnectionImplTest.java @@ -0,0 +1,55 @@ +package com.github.miniyk2012.coding2017.coderising.download; + +import com.github.miniyk2012.coding2017.coderising.download.api.Connection; +import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** +* ConnectionImpl Tester. +* +* @author +* @since
Mar 12, 2017
+* @version 1.0 +*/ +public class ConnectionImplTest { + String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; + ConnectionManagerImpl connectionManager = null; + Connection connection = null; + + @Before + public void before() throws Exception { + connectionManager = new ConnectionManagerImpl(); + connection = connectionManager.open(url); + + } + + @After + public void after() { + connection.close(); + } + /** + * + * Method: read(int startPos, int endPos) + * + */ + @Test + public void testRead() throws Exception { + int length = connection.getContentLength(); + byte[] biz = connection.read(0, length-1); + System.out.println(biz.length); + } + + /** + * + * Method: getContentLength() + * + */ + @Test + public void testGetContentLength() throws Exception { + int length = connection.getContentLength(); + System.out.println(length); + } + +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..57173d687a --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java @@ -0,0 +1,55 @@ +package com.github.miniyk2012.coding2017.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; +import com.github.miniyk2012.coding2017.coderising.download.api.DownloadListener; +import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + private double time = 0; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; +// String url = "https://www.baidu.com/img/bd_logo.png"; + + FileDownloader downloader = new FileDownloader(url, "test.png"); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠0.01秒"); + time += 0.01; + //休眠0.01秒 + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!耗时"+time+"秒"); + + } +} diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..be04c297ad --- /dev/null +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,72 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("logout successful", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + String actionName = "logout"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("logout failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git "a/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/ConnectionException.java" "b/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/ConnectionException.java" index 8dbfe95dda..0b256f8f4a 100644 --- "a/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/ConnectionException.java" +++ "b/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/ConnectionException.java" @@ -2,4 +2,12 @@ public class ConnectionException extends Exception { + /** + * 自定义异常错误 + * @param string + */ + public ConnectionException(String string) { + + } + } diff --git "a/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/DownloadListener.java" "b/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/DownloadListener.java" index 4cd0b3eab1..ee55a8cec0 100644 --- "a/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/DownloadListener.java" +++ "b/group03/1196051822/3\346\234\21013\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/download/api/DownloadListener.java" @@ -1,5 +1,6 @@ package com.coderising.download.api; public interface DownloadListener { + public void notifyFinished(); } diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/DownloadThread.java b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/DownloadThread.java index 115c1fab7e..5b357adff5 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/DownloadThread.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/DownloadThread.java @@ -3,6 +3,11 @@ import rui.study.coding2017.jobs3.download.api.Connection; +import java.io.IOException; +import java.io.RandomAccessFile; + +import static rui.study.coding2017.jobs3.download.FileDownloader.getFilePath; + public class DownloadThread extends Thread{ Connection conn; @@ -10,12 +15,23 @@ public class DownloadThread extends Thread{ int endPos; public DownloadThread(Connection conn, int startPos, int endPos){ - - this.conn = conn; + this.conn = conn; this.startPos = startPos; this.endPos = endPos; } - public void run(){ - + @Override + public void run(){ + try { + byte[] bytes=conn.read(startPos,endPos); + RandomAccessFile randomAccessFile=new RandomAccessFile(getFilePath(),"rw"); + randomAccessFile.seek(startPos); + randomAccessFile.write(bytes); + randomAccessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + }finally { + conn.close(); + } + } } diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/FileDownloader.java b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/FileDownloader.java index 48e982b9a4..433afb863e 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/FileDownloader.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/FileDownloader.java @@ -6,68 +6,121 @@ import rui.study.coding2017.jobs3.download.api.ConnectionManager; import rui.study.coding2017.jobs3.download.api.DownloadListener; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + public class FileDownloader { + + public static int threadNum=5; String url; DownloadListener listener; ConnectionManager cm; - + + private static String filePath; + + static String path="D:\\360downloads"; + + File pathFile; + + public static String getFilePath() { + return filePath; + } public FileDownloader(String _url) { this.url = _url; - + filePath=path+"/"+url.substring(url.lastIndexOf("/")+1); + + pathFile=new File(path); + if(!pathFile.exists()||!pathFile.isDirectory()){ + pathFile.mkdir(); + } } - + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; + Connection conn = null; try { - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - + int length = conn.getContentLength(); + setRandomAccessFile(length); + List list = getDownloadThreads(conn, length); + waitForDownLoad(list); + listener.notifyFinished(); } catch (ConnectionException e) { e.printStackTrace(); - }finally{ + } catch (IOException e) { + e.printStackTrace(); + } finally{ if(conn != null){ conn.close(); } } - - - - - } - - public void setListener(DownloadListener listener) { - this.listener = listener; } - - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - + /** + * 配置 随机访问文件 + * 预分配文件所占的磁盘空间,磁盘中会创建一个指定大小的文件; + * @param length + * @throws IOException + */ + private void setRandomAccessFile(int length) throws IOException { + try { + RandomAccessFile randomAccessFile=new RandomAccessFile(filePath,"rw"); + randomAccessFile.setLength(length); + randomAccessFile.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + /** + * 获得下载线程,为下一步阻塞做准备 + * @param conn 连接 + * @param length 长度 + * @return + */ + private List getDownloadThreads(Connection conn, int length) { + int startPos; + int endPos; + List list=new ArrayList(); + for (int i = 0; i < threadNum; i++) { + //从0开始 + startPos=i*(length/threadNum); + //从length/threadNum开始,最后为所有长度 + endPos=(i==threadNum-1)?length-1:(i+1)*(length/threadNum); + + DownloadThread downloadThread=new DownloadThread(conn,startPos,endPos); + downloadThread.start(); + list.add(downloadThread); + } + return list; + } + + /** + * 阻塞线程列表,等待线程列表全部执行完成 + * @param list 线程列表 + */ + private void waitForDownLoad(List list) { + for (DownloadThread aList : list) { + try { + aList.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } } diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/api/ConnectionManager.java b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/api/ConnectionManager.java index be74c9b5dc..f1f2a7ae3f 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/api/ConnectionManager.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/api/ConnectionManager.java @@ -1,10 +1,13 @@ package rui.study.coding2017.jobs3.download.api; +import java.io.IOException; +import java.net.MalformedURLException; + public interface ConnectionManager { /** * 给定一个url , 打开一个连接 * @param url * @return */ - public Connection open(String url) throws ConnectionException; + public Connection open(String url) throws ConnectionException, IOException; } diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionImpl.java b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionImpl.java index 3b85ecc27b..7e9b2c89f7 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionImpl.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionImpl.java @@ -3,26 +3,55 @@ import rui.study.coding2017.jobs3.download.api.Connection; -import java.io.IOException; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; public class ConnectionImpl implements Connection { + private static int byteSize = 8 * 1024; + + private int contentLength; + + + private URL url; + + public ConnectionImpl(URL url) throws IOException { + this.url = url; + HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + contentLength = httpURLConnection.getContentLength(); + httpURLConnection.disconnect(); + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + BufferedInputStream bufferedInputStream; + + ByteArrayOutputStream outputStream; + + HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream()); + outputStream = new ByteArrayOutputStream(); + int temp; + byte[] bytes = new byte[byteSize]; + while ((temp = bufferedInputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, temp); + } + bufferedInputStream.close(); + outputStream.flush(); + outputStream.close(); + httpURLConnection.disconnect(); + return outputStream.toByteArray(); + } + + @Override + public int getContentLength() { + return contentLength; + } + + @Override + public void close() { + } - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } } diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionManagerImpl.java b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionManagerImpl.java index 89bc3cd0e7..a27d334b3d 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionManagerImpl.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/jobs3/download/impl/ConnectionManagerImpl.java @@ -5,12 +5,14 @@ import rui.study.coding2017.jobs3.download.api.ConnectionException; import rui.study.coding2017.jobs3.download.api.ConnectionManager; +import java.io.IOException; +import java.net.URL; + public class ConnectionManagerImpl implements ConnectionManager { @Override - public Connection open(String url) throws ConnectionException { - - return null; + public Connection open(String url) throws ConnectionException, IOException { + return new ConnectionImpl(new URL(url)); } } diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/FileDownloaderTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/FileDownloaderTest.java deleted file mode 100644 index 683051b778..0000000000 --- a/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/FileDownloaderTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package rui.study.coding2017.jobs3; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import rui.study.coding2017.jobs3.download.FileDownloader; -import rui.study.coding2017.jobs3.download.api.ConnectionManager; -import rui.study.coding2017.jobs3.download.api.DownloadListener; -import rui.study.coding2017.jobs3.download.impl.ConnectionManagerImpl; - - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/download/FileDownloaderTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/download/FileDownloaderTest.java new file mode 100644 index 0000000000..3e95b7b593 --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/download/FileDownloaderTest.java @@ -0,0 +1,56 @@ +package rui.study.coding2017.jobs3.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import rui.study.coding2017.jobs3.download.FileDownloader; +import rui.study.coding2017.jobs3.download.api.ConnectionManager; +import rui.study.coding2017.jobs3.download.api.DownloadListener; +import rui.study.coding2017.jobs3.download.impl.ConnectionManagerImpl; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://sw.bos.baidu.com/sw-search-sp/software/952c9d6e73f50/QQ_8.9.20029.0_setup.exe"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } + +} diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/download/impl/ConnectionImplTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/download/impl/ConnectionImplTest.java new file mode 100644 index 0000000000..02206e79e0 --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/jobs3/download/impl/ConnectionImplTest.java @@ -0,0 +1,39 @@ +package rui.study.coding2017.jobs3.download.impl; + +import org.junit.Test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; + +import static org.junit.Assert.*; + +/** + * 测试链接实现 + * Created by 赵睿 on 2017/3/15. + */ +public class ConnectionImplTest { + private String url="http://sw.bos.baidu.com/sw-search-sp/software/952c9d6e73f50/QQ_8.9.20029.0_setup.exe"; + + private ConnectionImpl connection=new ConnectionImpl(new URL(url)); + + public ConnectionImplTest() throws IOException { + } + + @Test + public void read() throws Exception { + byte[] bs=connection.read(0,connection.getContentLength()); + System.out.println(bs.length); + FileOutputStream fileOutputStream=new FileOutputStream(new File("D://eee.exe")); + fileOutputStream.write(bs); + fileOutputStream.flush(); + fileOutputStream.close(); + } + + @Test + public void getContentLength() throws Exception { + System.out.println(connection.getContentLength()); + } + +} \ No newline at end of file diff --git a/group03/345943980/download-0335/src/main/java/com/coderising/download/DownloadThread.java b/group03/345943980/download-0335/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..e8aa61e5ae --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,47 @@ +package com.coderising.download; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread { + + private Connection conn; + private int startPos; + private int endPos; + private String localFile; + private CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos,String localFile, + CyclicBarrier barrier) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + public void run() { + try { + System.out.println("Begin to read [" + startPos +"-"+endPos+"]"); + + byte[] data = conn.read(startPos, endPos); + + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + + file.seek(startPos); + + file.write(data); + + file.close(); + + conn.close(); + + barrier.await(); //等待别的线程完成 + + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/group03/345943980/download-0335/src/main/java/com/coderising/download/FileDownloader.java b/group03/345943980/download-0335/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..23d9da57ba --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,157 @@ +package com.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +public class FileDownloader { + + private static final int DOWNLOAD_TRHEAD_NUM = 3; + + private String url; + + private String localFile; + + private DownloadListener listener; + + private ConnectionManager cm; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + /**Connection conn = null; + CountDownLatch threadsCnt = new CountDownLatch(threadNum); + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + RandomAccessFile file = new RandomAccessFile("test", "rw"); + file.setLength(length); + new DownloadThread(conn, 0, length - 1, file, threadsCnt).start(); + int clength = length / threadNum; + int off = 0; + for (int i = 0; i < threadNum; i++) { + if (i != threadNum - 1) { + new DownloadThread(cm.open(url), off, off + clength, file,threadsCnt).start(); + } else { + new DownloadThread(cm.open(url), off, length - 1, file,threadsCnt).start(); + } + off = off + clength + 1; + } + threadsCnt.await(); + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + }**/ + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + createPlaceHolderFile(this.localFile, length); + + int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); + + for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){ + + + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + + thread.start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + } + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + + int[][] ranges = new int[threadNum][2]; + + int eachThreadSize = contentLen / threadNum;// 每个线程需要下载的文件大小 + int left = contentLen % threadNum;// 剩下的归最后一个线程来处理 + + for(int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + public int getContentLength() { + + return httpConn.getContentLength(); + } + + public void close() { + + } +} diff --git a/group03/345943980/download-0335/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/group03/345943980/download-0335/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..b44cbd94de --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,29 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + private Connection conn; + + public Connection open(String url) throws ConnectionException { + try { + URL urlObj = new URL(url); + HttpURLConnection httpConn = (HttpURLConnection)urlObj.openConnection(); + conn = new ConnectionImpl(httpConn); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return conn; + } + +} diff --git a/group03/345943980/download-0335/src/main/java/com/coding/basic/ArrayList.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group04/120549547/base/src/com/coding/basic/BinaryTreeNode.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group04/120549547/base/src/com/coding/basic/BinaryTreeNode.java rename to group03/345943980/download-0335/src/main/java/com/coding/basic/BinaryTreeNode.java diff --git a/group04/120549547/base/src/com/coding/basic/Iterator.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/Iterator.java similarity index 100% rename from group04/120549547/base/src/com/coding/basic/Iterator.java rename to group03/345943980/download-0335/src/main/java/com/coding/basic/Iterator.java diff --git a/group03/345943980/download-0335/src/main/java/com/coding/basic/LinkedList.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e7b1387a32 --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,392 @@ +package com.coding.basic; + +import java.util.Stack; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o) { + Node newNode = new Node(o); + if (null == head) { + head = newNode; + size++; + return; + } + Node currNode = head; + while (null != currNode.next) { + currNode = currNode.next; + } + currNode.next = newNode; + size++; + } + + public void add(int index, Object o) { + rangeCheck(index); + if (index == size) { + add(o); + return; + } + if (index == 0) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + return; + } + Node newNode = new Node(o); + Node currNode = head; + for (int i = 0; i < index - 1; i++) { + currNode = currNode.next; + } + newNode.next = currNode.next; + currNode.next = newNode; + size++; + } + + public Object get(int index) { + rangeCheck(index); + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private void rangeCheck(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + public Object remove(int index) { + rangeCheck(index); + if (index == 0) { + return this.removeFirst(); + } + if (index == size - 1) { + return this.removeLast(); + } + Node currNode = head; + for (int i = 0; i < index - 1; i++) { + currNode = currNode.next; + } + Node removeNode = currNode.next; + currNode.next = removeNode.next; + // removeNode = null; + size--; + return removeNode; + } + + public void remove(Object obj) { + if (head == null) { + throw new NullPointerException(); + } + // 如果要删除的结点是第一个,则把下一个结点赋值给第一个结点 + if (head.data.equals(obj)) { + head = head.next; + size--; + } else { + Node pre = head; // 上一节点 + Node cur = head.next; // 当前结点 + while (cur != null) { + if (cur.data.equals(obj)) { + pre.next = cur.next; + size--; + } + pre = pre.next; + cur = cur.next; + } + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (null == head) { + head = new Node(o); + size++; + return; + } + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + this.add(o); + } + + public Object removeFirst() { + if (null == head) { + return null; + } + Node currNode = head; + head = currNode.next; + size--; + return head; + } + + public Object removeLast() { + if (null == head) { + return null; + } + if (null == head.next) { + Node currNode = head; + head = null; + size--; + return currNode; + } + Node currNode = head; + while (null != currNode.next) { + currNode = currNode.next; + } + currNode = null; + size--; + return null; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + Node node = head; + while (node != null) { + sb.append(node.data); + if (node.next != null) { + sb.append(","); + } + node = node.next; + } + sb.append("]"); + return sb.toString(); + } + + public Iterator iterator() { + return new MyIterator(); + } + + private static class Node { + Object data; + Node next; + + Node(Object data) { + this.data = data; + } + + @Override + public String toString() { + return this.data.toString(); + } + } + + private class MyIterator implements Iterator { + int cursor = 0; + Node curNode; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if (curNode == null) { + curNode = head; + } else { + curNode = curNode.next; + } + cursor++; + return curNode; + } + + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (head == null || head.next == null) { + return; + } + java.util.Stack stack = new Stack<>(); + Node curNode = head; + while (curNode != null) { + stack.push(curNode); + Node nextNode = curNode.next; + curNode.next = null; // 断开指向下一个元素的指针 + curNode = nextNode; + } + + head = stack.pop(); + curNode = head; + while (!stack.isEmpty()) { + Node nextNode = stack.pop(); + curNode.next = nextNode; + curNode = nextNode; + } + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int num = size / 2; + for (int i = 0; i < num; i++) { + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException(); + } + + int len = size - i >= length ? length : size - i; + + int k = 0; + while (k < len) { + remove(i); + k++; + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + int[] arr = new int[list.size()]; + + for (int i = 0; i < list.size(); i++) { + arr[i] = Integer.parseInt(get(Integer.parseInt(list.get(i).toString())).toString()); + } + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + for (int i = 0; i < list.size(); i++) { + this.remove(list.get(i).toString()); + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if (head == null || head.next == null) { + throw new RuntimeException("LinkedList is empty!"); + } + + Node pre = head; + Node cur = head; + while (cur.next != null) { + cur = cur.next; + Object data = pre.data; + while (cur.data == data) { + if (cur.next == null) { + pre.next = null; + break; + } + pre.next = cur.next; + size--; + cur = cur.next; + if (cur == null) { + break; + } + } + pre = pre.next; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + if (head == null) { + return; + } + + Node node = head; + int start = -1; + int end = -1; + int i = 0; + while (node != null) { + if ((start == -1) && (int) node.data <= min) { + start = i; + } + if ((int) node.data >= max) { + end = i; + break; + } + node = node.next; + i++; + } + + if (start == -1) { + start = 0; + } + if (end == -1) { + end = size; + } + this.remove(start, end - start); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + if (list == null) { + return null; + } + + LinkedList result = new LinkedList(); + + int i1 = 0; + int i2 = 0; + + while (i1 < this.size && i2 < list.size()) { + + int value1 = Integer.valueOf(this.get(i1).toString()); + int value2 = Integer.valueOf(list.get(i2).toString()); + + if (value1 == value2) { + result.add(value1); + i1++; + i2++; + + } else if (value1 < value2) { + i1++; + + } else { + i2++; + + } + } + return result; + } +} diff --git a/group12/446031103/src/com/coding/basic/List.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/List.java similarity index 100% rename from group12/446031103/src/com/coding/basic/List.java rename to group03/345943980/download-0335/src/main/java/com/coding/basic/List.java diff --git a/group03/345943980/download-0335/src/main/java/com/coding/basic/Queue.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group03/345943980/download-0335/src/main/java/com/coding/basic/Stack.java b/group03/345943980/download-0335/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/345943980/download-0335/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group03/345943980/download-0335/src/test/java/com/coderising/download/ConnectionTest.java b/group03/345943980/download-0335/src/test/java/com/coderising/download/ConnectionTest.java new file mode 100644 index 0000000000..0bf72df7e9 --- /dev/null +++ b/group03/345943980/download-0335/src/test/java/com/coderising/download/ConnectionTest.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class ConnectionTest { + + @Test + public void testContentLength() throws ConnectionException{ + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection con = connMan.open("https://imgsa.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=befb30b3a344ad343ab28fd5b1cb6791/1ad5ad6eddc451daae139eb5b4fd5266d1163282.jpg"); + Assert.assertEquals(90441, con.getContentLength()); + + } +} diff --git a/group03/345943980/download-0335/src/test/java/com/coderising/download/FileDownloaderTest.java b/group03/345943980/download-0335/src/test/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..94a7bcb477 --- /dev/null +++ b/group03/345943980/download-0335/src/test/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,42 @@ +package com.coderising.download; + +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + + private boolean downloadFinished = false; + + @Test + public void testDownloader() { + String url = "https://imgsa.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=befb30b3a344ad343ab28fd5b1cb6791/1ad5ad6eddc451daae139eb5b4fd5266d1163282.jpg"; + FileDownloader downloader = new FileDownloader(url,"D:\\wordtest\\test123.jpg"); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + // 休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } +} diff --git a/group03/345943980/download-0335/src/test/java/com/coderising/download/TestLinkedList.java b/group03/345943980/download-0335/src/test/java/com/coderising/download/TestLinkedList.java new file mode 100644 index 0000000000..f2d9bb59af --- /dev/null +++ b/group03/345943980/download-0335/src/test/java/com/coderising/download/TestLinkedList.java @@ -0,0 +1,245 @@ +package com.coderising.download; + +import org.junit.Assert; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; + +public class TestLinkedList { + + + + @Test + public void testAdd(){ + LinkedList linkedList = new LinkedList(); + linkedList.add("123"); //0 + //1 + //2 + linkedList.add("233"); //3 + linkedList.add("333"); //4 + + linkedList.add("444"); //5 + + //System.out.println(linkedList.get(3)); + //System.out.println(linkedList.get(4)); + + linkedList.add(0,"555"); + linkedList.add(2,"666"); + linkedList.add(5,"777"); + + +// for(int i=0;idom4j 1.6.1 + + + org.jdom + jdom2 + 2.0.5 + diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/Configuration.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/Configuration.java new file mode 100644 index 0000000000..4a882f016a --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/Configuration.java @@ -0,0 +1,105 @@ +package com.coderising.teacher.litestruts; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +public class Configuration { + private static final String LINE = "/"; + private Map actions = new HashMap<>(); + + public Configuration(String fileName) { + + /*String packageName = this.getClass().getPackage().getName(); + packageName = packageName.replace(".", LINE); + InputStream is = this.getClass().getResourceAsStream(LINE + packageName + LINE + fileName); + parseXML(is); + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + e.printStackTrace(); + }*/ + + InputStream is = this.getClass().getResourceAsStream(LINE+fileName); + parseXML(is); + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void parseXML(InputStream is) { + SAXBuilder builder = new SAXBuilder(); + try { + Document doc = builder.build(is); + Element root = doc.getRootElement(); + for (Element actionElement : root.getChildren("action")) { + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + ActionConfig ac = new ActionConfig(actionName, clzName); + for (Element resultElement : actionElement.getChildren("result")) { + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getTextTrim(); + ac.addViewResult(resultName, viewName); + } + this.actions.put(actionName, ac); + } + } catch (JDOMException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; + } + return ac.getClzName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; + } + return ac.getViewName(resultName); + + } + + private static class ActionConfig { + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + + public void addViewResult(String name, String viewName) { + viewResult.put(name, viewName); + } + + public String getViewName(String resultName) { + return viewResult.get(resultName); + } + + public String getClzName() { + return clzName; + } + + } +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/ConfigurationException.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..d4dc218fc4 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/ConfigurationException.java @@ -0,0 +1,23 @@ +package com.coderising.teacher.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/LoginAction.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/LoginAction.java new file mode 100644 index 0000000000..c6dfb515f9 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package com.coderising.teacher.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/ReflectionUtil.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..f6e4d5118f --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/ReflectionUtil.java @@ -0,0 +1,72 @@ +package com.coderising.teacher.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + /*List listMethods = new ArrayList<>(); + Method[] methods = clz.getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().startsWith("set")) { + listMethods.add(method); + } + } + return listMethods;*/ + return getMethod(clz,"set"); + } + + public static void setParameters(Object o, Map params) { + List methods = ReflectionUtil.getSetterMethods(o.getClass()); + for (String name : params.keySet()) { + String methodName = "set" + name; + for (Method method : methods) { + if (method.getName().equalsIgnoreCase(methodName)) { + try { + method.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + + } + } + + public static List getGetterMethods(Class clz) { + return getMethod(clz,"get"); + } + + public static List getMethod(Class clz,String startWithName) { + List methods = new ArrayList<>(); + for(Method m:clz.getDeclaredMethods()){ + if(m.getName().startsWith(startWithName)){ + methods.add(m); + } + } + return methods; + } + + public static Map getParamterMap(Object obj) { + Map params = new HashMap(); + List methods = ReflectionUtil.getGetterMethods(obj.getClass()); + for(Method m:methods){ + try { + Object o = m.invoke(obj); + params.put(m.getName().replace("get","").toLowerCase(), o); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + return params; + } + + + +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/Struts.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/Struts.java new file mode 100644 index 0000000000..cf0c238d67 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/Struts.java @@ -0,0 +1,52 @@ +package com.coderising.teacher.litestruts; + +import java.lang.reflect.Method; +import java.util.Map; + +public class Struts { + + static Configuration config = new Configuration("struts1.xml"); + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + String clzName = config.getClassName(actionName); + if(clzName == null){ + return null; + } + try { + Class clz = Class.forName(clzName); + Object obj = clz.newInstance(); + ReflectionUtil.setParameters(obj, parameters); + Method method = clz.getDeclaredMethod("execute"); + String resultName = (String)method.invoke(obj); + Map params = ReflectionUtil.getParamterMap(obj); + String resultView = config.getResultView(actionName, resultName); + View view = new View(); + view.setJsp(resultView); + view.setParameters(params); + return view; + } catch (Exception e) { + + e.printStackTrace(); + } + return null; + } +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/View.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/View.java new file mode 100644 index 0000000000..bb59a226ed --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/teacher/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coderising.teacher.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ConfigurationTest.java b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..7eec659c28 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,36 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.teacher.litestruts.Configuration; + +public class ConfigurationTest { + Configuration cfg = new Configuration("struts1.xml"); + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.teacher.litestruts.LoginAction", clzName); + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.teacher.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView() { + String jsp = cfg.getResultView("login", "success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login", "fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout", "success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout", "error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } +} diff --git a/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ReflectionUtilTest.java b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..c0306838b6 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,100 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.teacher.litestruts.ReflectionUtil; + +public class ReflectionUtilTest { + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.teacher.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + @Test + public void testSetParameters() throws Exception{ + + String name = "com.coderising.teacher.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + ReflectionUtil.setParameters(o,params); + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + + @Test + public void testGetGetterMethod() throws Exception{ + String name = "com.coderising.teacher.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParamters() throws Exception{ + String name = "com.coderising.teacher.litestruts.LoginAction"; + Class clz = Class.forName(name); + com.coderising.teacher.litestruts.LoginAction action = (com.coderising.teacher.litestruts.LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } + + +} diff --git a/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest.java b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest.java index 505e2afbfa..70618cc246 100644 --- a/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest.java +++ b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -6,33 +6,35 @@ import org.junit.Assert; import org.junit.Test; + public class StrutsTest { + @Test public void testLoginActionSuccess() { - + String actionName = "login"; - - Map params = new HashMap(); - params.put("name", "test"); - params.put("password", "1234"); - - View view = Struts.runAction(actionName, params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); } @Test public void testLoginActionFailed() { String actionName = "login"; - Map params = new HashMap(); - params.put("name", "test"); - params.put("password", "123456"); // 密码和预设的不一致 - - View view = Struts.runAction(actionName, params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", - view.getParameters().get("message")); + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); } } diff --git a/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest1.java b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest1.java new file mode 100644 index 0000000000..70d3eeb2cc --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest1.java @@ -0,0 +1,38 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest1 { + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + com.coderising.teacher.litestruts.View view = com.coderising.teacher.litestruts.Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + com.coderising.teacher.litestruts.View view = com.coderising.teacher.litestruts.Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group03/58555264/src/main/java/com/circle/collection/LinkedListV2.java b/group03/58555264/src/main/java/com/circle/collection/LinkedListV2.java new file mode 100644 index 0000000000..6fbc5c0b14 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/LinkedListV2.java @@ -0,0 +1,414 @@ +package com.circle.collection; + +import java.util.*; + +/** + * Created by keweiyang on 2017/3/13. + */ +public class LinkedListV2 { + + private int size; + private Node first; + + /** + * 从尾部插入数据 + * + * @param e + */ + public void add(E e) { + Node node = new Node(e, null); + if (size == 0) { + first = node; + } else { + Node current = first; + while (current.next != null) { + current = current.next; + } + current.next = node; + } + size++; + } + + public void add(int index, E e) { + rangeCheck(index); + Node node = new Node(e, null); + Node current = first; + Node prev = null; + if (index == 0) { + node.next = first; + first = node; + } else { + int i = 1; + prev = current; + current = current.next; + while (current != null) { + + if (i == index) { + break; + } + i++; + prev = current; + current = current.next; + + } + node.next = current; + prev.next = node; + + } + size++; + } + + public E get(int index) { + rangeCheck(index); + Node current = first; + + int i = 0; + if (current == null) { + throw new NoSuchElementException("链表为空"); + } else { + while (current.next != null) { + + if (i == index) { + break; + } + i++; + current = current.next; + } + } + return (E) current.item; + } + + private void rangeCheck(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("索引越界"); + } + } + + public int size() { + return this.size; + } + + + public E removeFirst() { + if (first == null) { + throw new IllegalStateException("链表为空"); + } else { + Node current = first; + first = first.next; + size--; + return (E) current.item; + } + } + + public E removeLast() { + if (first == null) { + throw new IllegalStateException("链表为空"); + } else { + Node current = first; + while (current.next != null) { + current = current.next; + } + size--; + return (E) current.item; + + } + } + + /** + * 把该链表逆置 + * 例如链表为3->7->10,逆置后变为 10->7->3 + */ + public void reverse() { + Node current = first; + LinkedListV2 list = new LinkedListV2(); + while (current != null) { + + list.add(0, current.item); + current = current.next; + } + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list=2->5->7->8,删除以后的值为 7->8 + * 如果 list=2->5->7->8 ->10,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + Node stepByOne = first; + Node stepByTwo = first; + + while (stepByTwo.next != null && stepByTwo.next.next != null) { + + stepByTwo = stepByTwo.next.next; + stepByOne = stepByOne.next; + } + + if (stepByTwo.next != null) { + stepByOne = stepByOne.next; + } + + //打印单链表的前半部分 + while (stepByOne != null) { + System.out.println(String.valueOf(stepByOne.item)); + stepByOne = stepByOne.next; + + } + + + } + + /** + * 从第i个元素开始,删除length个元素,注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + rangeCheck(i); + if (length <= 0) { + throw new IllegalStateException("请输入正确的个数"); + } + Node current = first; + Node prev = null; + int a = 0; + while (current != null) { + prev = current; + current = current.next; + a++; + if (a == i) { + break; + } + } + + if ((size - i + 1) <= length) { + prev.next = null; + size -= (size - i); + } else { + Node node = prev; + int temp = length; + while (temp > 0) { + current = current.next; + temp--; + } + prev.next = current; + size -= length; + + } + + + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出哪些list所指定的元素 + * 例如当前链表=11->101->201->301->401->501->601->701 + * listB =1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + * @return + */ + public E[] getElements(LinkedListV2 list) { + + Iterator it = list.iterator(); + LinkedListV2 getElementsList = new LinkedListV2(); + while (it.hasNext()) { + Integer integer = (Integer) it.next(); + E e = get(integer); + getElementsList.add(e); + } + + + return (E[]) getElementsList.toArray(); + } + + public Object[] toArray() { + Object[] result = new Object[size]; + + int i = 0; + for (Node x = first; x != null; x = x.next) { + result[i] = x.item; + i++; + } + + return result; + } + + /** + * 已知链表的元素以值递增有序排列,并以单链表做存储结构 + * 从当前链表中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedListV2 list) { + Iterator it = list.iterator(); + Node current = first; + Node prev = null; + + while (it.hasNext()) { + Integer integer = (Integer) it.next(); + + + while (integer > (Integer) (current.item) && current != null) { + prev = current; + current = current.next; + } + + if (current != null && integer.equals(current.item)) { + if (current == first) { + first = first.next; + current = first; + } else { + prev.next = current.next; + current = current.next; + } + size--; + } else { + System.out.println("该链表中没有该元素"); + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表做存储结构 + * 删除表中所有值相同的多余元素 + */ + public void removeDuplicateValues() { + Node current = first; + Node innerCurrent = null; + while (current != null) { + + innerCurrent = current.next; + while (innerCurrent != null) { + if (!(innerCurrent.item).equals(current.item)) { + break; + } + innerCurrent = innerCurrent.next; + size--; + } + + current.next = innerCurrent; + current = current.next; + + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作为存储结构 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Node current = first; + Node prev = first; + while (current != null) { + if ((Integer)(current.item) > min && (Integer)(current.item) < max) { + if (current == first) { + first = first.next; + current = first; + prev = first; + }else{ + prev.next = current.next; + + } + size--; + } + prev = current; + current = current.next; + } + + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C的元素有依值递增有序排列 + * + * @param list + * @return + */ + public LinkedListV2 intersection(LinkedListV2 list) { + + Iterator it = list.iterator(); + Node current = first; + + LinkedListV2 newList = new LinkedListV2(); + + if (list.size == 0 || this.size == 0) { + return null; + } + + if ((Integer) this.first.item > (Integer) list.get(list.size() - 1)) { + return null; + } + + if ((Integer) this.removeLast() < (Integer) list.get(0)) { + return null; + } + + while (it.hasNext()) { + Integer integer = (Integer) it.next(); + + while (current != null && integer > (Integer) (current.item)) { +// prev = current; + current = current.next; + } + + if (current != null && integer.equals(current.item)) { + newList.add(current.item); + + } + } + + + return newList; + } + + + private static class Node { + E item; + Node next; + + public Node(E item, Node next) { + this.item = item; + this.next = next; + } + } + + public Iterator iterator() { + return new Iterator() { + int nextIndex = 0; + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public E next() { + if (!this.hasNext()) { + throw new NoSuchElementException(); + } + E e = get(nextIndex); + nextIndex++; + + return e; + } + }; + } + + + public static void main(String[] args) { + java.util.LinkedList list; + + } +} diff --git a/group03/58555264/src/main/java/com/circle/download/DownloadThread.java b/group03/58555264/src/main/java/com/circle/download/DownloadThread.java new file mode 100644 index 0000000000..1f3b996dde --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/DownloadThread.java @@ -0,0 +1,54 @@ +package com.circle.download; + +import com.circle.download.api.Connection; +import com.circle.download.api.ConnectionException; +import com.circle.download.api.DownloadListener; + +import java.io.*; + +/** + * Created by keweiyang on 2017/3/10. + */ +public class DownloadThread extends Thread { + + private Connection conn; + private int startPos; + private int endPos; + static int threadFinished = 0; + + private DownloadListener listener; + private int threadNum; + + + public DownloadThread(Connection conn, int startPos, int endPos, DownloadListener listener, int threadNum) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.listener = listener; + this.threadNum = threadNum; + } + + @Override + public void run() { + try { + this.conn.read(startPos, endPos); + } catch (IOException e) { + e.printStackTrace(); + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + synchronized (this) { + threadFinished++; + if (threadFinished == threadNum) { + listener.notifyFinished(); + } + + } + + } + System.out.println("线程:" + Thread.currentThread().getId() + " , startPos:" + startPos + ",endPos:" + endPos); + + } +} + diff --git a/group03/58555264/src/main/java/com/circle/download/FileDownloader.java b/group03/58555264/src/main/java/com/circle/download/FileDownloader.java new file mode 100644 index 0000000000..ac7e3e3ef3 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/FileDownloader.java @@ -0,0 +1,106 @@ +package com.circle.download; + +import com.circle.download.api.Connection; +import com.circle.download.api.ConnectionException; +import com.circle.download.api.ConnectionManager; +import com.circle.download.api.DownloadListener; +import com.circle.download.impl.ConnectionManagerFactory; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * Created by keweiyang on 2017/3/10. + */ +public class FileDownloader { + private String url; + private DownloadListener listener; + private ConnectionManager cm; + private int threadNum; + + public FileDownloader(String url, int threadNum) { + this.threadNum = threadNum; + this.url = url; + } + + public DownloadListener getListener() { + return listener; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + /** + * 具体的实现思路: + * 1、需要调用ConnectionManager的open方法打开连接,然后通过Connection.getConnection.getContentLength方法获得文件的长度 + * 2、至少启动3个线程下载,注意每个线程需要调用ConnectionManager的open方法 + * 然后调用read方法,read方法中有读取文件的开始位置和结束位置的参数,返回值是byte[]数组 + * 3、把byte数组写入到文件中 + * 4、所有的线程都下载完成以后,需要调用listener的notifiedFinished方法 + */ + public void execute() { + Connection conn = null; + int[] startPos = new int[threadNum]; + int[] endPos = new int[threadNum]; + RandomAccessFile raf = null; + + try { + String[] ss = url.split("/"); + Thread[] threads = new Thread[threadNum]; + + File file = new File(ss[ss.length - 1]); + + + cm = ConnectionManagerFactory.getManager(file); + conn = cm.open(this.url); + int length = conn.getContentLength(); + System.out.println("length:" + length); + + + raf = new RandomAccessFile(file, "rwd"); + raf.setLength(length); + + for (int i = 0; i < threadNum; i++) { + int size = i * (length / threadNum); + startPos[i] = size; + + if (i == threadNum - 1) { + endPos[i] = length; + } else { + size = (i + 1) * (length / threadNum); + endPos[i] = size - 1; + } + + threads[i] = new DownloadThread(cm.open(this.url), startPos[i], endPos[i],listener,threadNum); + threads[i].start(); + } + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + if (raf != null) { + try { + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public static void main(String[] args) { + String url2 = "http://hiphotos.baidu.com/240728057/pic/item/6a50e38242aad8f60cf4d2b3.jpg"; + String url = "http://bcbang.oss-cn-qingdao.aliyuncs.com/TLAB-in-Eden-memory.png"; + String url3 = "http://www.cnblogs.com/iwideal/p/6045118.html"; + FileDownloader downloader = new FileDownloader(url2, 2); + downloader.execute(); + + } + +} diff --git a/group03/58555264/src/main/java/com/circle/download/api/Connection.java b/group03/58555264/src/main/java/com/circle/download/api/Connection.java new file mode 100644 index 0000000000..518f87f5c5 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/api/Connection.java @@ -0,0 +1,31 @@ +package com.circle.download.api; + +import java.io.IOException; + +/** + * Created by keweiyang on 2017/3/10. + */ +public interface Connection { + + /** + * 给定开始和结束位置,读取数据,返回值是字节数组 + * + * @param startPos 开始位置,从0开始 + * @param endPos 结束位置 + * @return + * @throws IOException + */ + public void read(int startPos, int endPos) throws IOException, ConnectionException; + + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); + +} diff --git a/group03/58555264/src/main/java/com/circle/download/api/ConnectionException.java b/group03/58555264/src/main/java/com/circle/download/api/ConnectionException.java new file mode 100644 index 0000000000..b0f013308d --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/api/ConnectionException.java @@ -0,0 +1,24 @@ +package com.circle.download.api; + +/** + * Created by keweiyang on 2017/3/10. + */ +public class ConnectionException extends Exception { + + public ConnectionException() { + + } + + /** + * 描述异常 + * @param msg + */ + public ConnectionException(String msg) { + super(msg); + + } +} + + + + diff --git a/group03/58555264/src/main/java/com/circle/download/api/ConnectionManager.java b/group03/58555264/src/main/java/com/circle/download/api/ConnectionManager.java new file mode 100644 index 0000000000..8de4d600ba --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/api/ConnectionManager.java @@ -0,0 +1,12 @@ +package com.circle.download.api; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.sql.*; + +/** + * Created by keweiyang on 2017/3/10. + */ +public interface ConnectionManager { + public Connection open(String url) throws ConnectionException, IOException; +} diff --git a/group03/58555264/src/main/java/com/circle/download/api/DownloadListener.java b/group03/58555264/src/main/java/com/circle/download/api/DownloadListener.java new file mode 100644 index 0000000000..758b233d3c --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/api/DownloadListener.java @@ -0,0 +1,8 @@ +package com.circle.download.api; + +/** + * Created by keweiyang on 2017/3/10. + */ +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group03/58555264/src/main/java/com/circle/download/impl/ConnectionImpl.java b/group03/58555264/src/main/java/com/circle/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..c528d3b335 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/impl/ConnectionImpl.java @@ -0,0 +1,77 @@ +package com.circle.download.impl; + +import com.circle.download.api.Connection; +import com.circle.download.api.ConnectionException; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Created by keweiyang on 2017/3/10. + */ +class ConnectionImpl implements Connection { + private String url; + private HttpURLConnection conn; + private File file; + + public ConnectionImpl(String url, File file) throws IOException, ConnectionException { + this.file = file; + this.url = url; + this.conn = init(); + } + + private HttpURLConnection init() throws IOException, ConnectionException { + URL httpURL = new URL(url); + this.conn = (HttpURLConnection) httpURL.openConnection(); + + this.conn.setRequestMethod("GET"); + this.conn.setRequestProperty("Charset", "UTF-8"); + return conn; + } + + @Override + public void read(int startPos, int endPos) throws IOException, ConnectionException { + URL httpURL = new URL(url); + this.conn = (HttpURLConnection) httpURL.openConnection(); + + this.conn.setRequestMethod("GET"); + this.conn.setRequestProperty("Charset", "UTF-8"); + this.conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + if (this.getContentLength() < 0) { + throw new ConnectionException("连接内容小于0"); + } + + int code = conn.getResponseCode(); + RandomAccessFile raf = null; + try{ + InputStream is = conn.getInputStream(); + raf = new RandomAccessFile(this.file, "rwd"); + raf.seek(startPos); + + byte[] bs = new byte[1024]; + int len = -1; + + while ((len = is.read(bs)) != -1) { + raf.write(bs, 0, len); + } + }finally { + raf.close(); + } + + + + } + + @Override + public int getContentLength() { + return this.conn.getContentLength(); + } + + @Override + public void close() { +// this.conn. + + } +} diff --git a/group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerFactory.java b/group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerFactory.java new file mode 100644 index 0000000000..b879683ba9 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerFactory.java @@ -0,0 +1,31 @@ +package com.circle.download.impl; + +import com.circle.download.api.Connection; +import com.circle.download.api.ConnectionManager; + +import java.io.File; + +/** + * Created by keweiyang on 2017/3/11. + */ +public class ConnectionManagerFactory { + + private static volatile ConnectionManager manager = null; + + private ConnectionManagerFactory() { + + } + + public static ConnectionManager getManager(File file) { + + if (manager == null) { + synchronized (ConnectionManagerFactory.class) { + if (manager == null) { + manager = new ConnectionManagerImpl(file); + } + } + } + + return manager; + } +} diff --git a/group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerImpl.java b/group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..6013f8878b --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,31 @@ +package com.circle.download.impl; + +import com.circle.download.api.Connection; +import com.circle.download.api.ConnectionException; +import com.circle.download.api.ConnectionManager; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +/** + * Created by keweiyang on 2017/3/10. + */ +class ConnectionManagerImpl implements ConnectionManager { + private File file; + + public ConnectionManagerImpl(File file) { + + this.file = file; + + } + + @Override + public Connection open(String url) throws ConnectionException, IOException { + return new ConnectionImpl(url, this.file); + } + + +} diff --git a/group03/58555264/src/test/java/com/circle/collection/LinkedListV2Test.java b/group03/58555264/src/test/java/com/circle/collection/LinkedListV2Test.java new file mode 100644 index 0000000000..68ff190ed2 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/collection/LinkedListV2Test.java @@ -0,0 +1,245 @@ +package com.circle.collection; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/3/14. + */ +public class LinkedListV2Test { + + private LinkedListV2 list = new LinkedListV2(); + + @Test + public void add() throws Exception { + list.add(null); + list.add(1); + list.add(2); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + @Test + public void add1() throws Exception { + list.add(0, 1); + list.add(1, 3); + list.add(2, 5); + list.add(6); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + + } + + @Test + public void get() throws Exception { + list.add(0, 1); + list.add(1, 3); + list.add(2, 5); + list.add(6); + Assert.assertEquals(6, list.get(2)); + + } + + @Test + public void size() throws Exception { + list.add(0, 1); + list.add(1, 3); + list.add(2, 5); + list.add(6); + Assert.assertEquals(4, list.size()); + + } + + @Test + public void removeFirst() throws Exception { + list.add(0, 1); + list.add(1, 3); + list.add(2, 5); + list.add(6); + + Assert.assertEquals(1, list.removeFirst()); + + } + + @Test + public void removeLast() throws Exception { + list.add(0, 1); + list.add(1, 3); + list.add(2, 5); + list.add(6); + + Assert.assertEquals(6,list.removeLast()); + + } + + @Test + public void reverse() throws Exception { + list.add(0, 3); + list.add(1, 7); + list.add(2, 10); + + list.reverse(); + + } + + @Test + public void removeFirstHalf() throws Exception { + list.add(0, 2); + list.add(1, 5); + list.add(2, 7); + list.add(3, 8); + list.add(4, 10); + list.removeFirstHalf(); + + } + + @Test + public void remove() throws Exception { + list.add(0, 2); + list.add(1, 5); + list.add(2, 7); + list.add(3, 8); + list.add(4, 10); + list.remove(2, 3); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + } + + @Test + public void getElements() throws Exception { + list.add(0, 11); + list.add(1, 101); + list.add(2, 201); + list.add(3, 301); + list.add(4, 401); + list.add(5, 501); + list.add(6, 601); + + LinkedListV2 listV2 = new LinkedListV2(); + listV2.add(0, 1); + listV2.add(1, 3); + listV2.add(2, 4); + listV2.add(3, 6); + + Object[] results = list.getElements(listV2); + System.out.println(results.length); + + assertEquals(4, results.length); + + for (Object object : results) { + System.out.println(object); + + } + + } + + @Test + public void toArray() throws Exception { + + } + + @Test + public void subtract() throws Exception { + list.add(0, 100); + list.add(1, 101); + list.add(2, 201); + list.add(3, 301); + list.add(4, 401); + list.add(5, 501); + list.add(6, 601); + + LinkedListV2 listV2 = new LinkedListV2(); + listV2.add(0, 100); + listV2.add(1, 301); + listV2.add(2, 401); + listV2.add(3, 601); + list.subtract(listV2); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + + } + + @Test + public void removeDuplicateValues() throws Exception { + + list.add(0, 100); + list.add(1, 100); + list.add(2, 201); + list.add(3, 301); + list.add(4, 401); + list.add(5, 401); + list.add(6, 401); + list.removeDuplicateValues(); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + @Test + public void removeRange() throws Exception { + + list.add(0, 100); + list.add(1, 110); + list.add(2, 201); + list.add(3, 211); + list.add(4, 151); + list.add(5, 131); + list.add(6, 171); + list.removeRange(110, 150); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + } + + @Test + public void intersection() throws Exception { + list.add(0, 100); + list.add(1, 110); + list.add(2, 201); + list.add(3, 211); + list.add(4, 351); + list.add(5, 431); + + LinkedListV2 listV2 = new LinkedListV2(); + listV2.add(0, 100); + listV2.add(1, 351); + listV2.add(2, 431); + listV2.add(3, 600); + + LinkedListV2 integerLinkedListV2 = list.intersection(listV2); + + Iterator it = integerLinkedListV2.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/download/FileDownloaderTest.java b/group03/58555264/src/test/java/com/circle/download/FileDownloaderTest.java new file mode 100644 index 0000000000..e22edd2ba9 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/download/FileDownloaderTest.java @@ -0,0 +1,49 @@ +package com.circle.download; + +import com.circle.download.api.ConnectionManager; +import com.circle.download.api.DownloadListener; +import com.circle.download.impl.ConnectionManagerFactory; +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/3/13. + */ +public class FileDownloaderTest { + + private boolean downloadFinished = false; + private int threadNum = 3; + + + @Test + public void execute() throws Exception { + String url = "http://hiphotos.baidu.com/240728057/pic/item/6a50e38242aad8f60cf4d2b3.jpg"; + FileDownloader downloader = new FileDownloader(url, threadNum); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + + } + }); + + downloader.execute(); + + + while (!downloadFinished) { + System.out.println("还没有下载完成,休眠5秒"); + + Thread.sleep(5000); + + } + + System.out.println("下载完成!"); + + + } + +} \ No newline at end of file diff --git a/group03/619224754/src/com/coderising/lru/LRU.java b/group03/619224754/src/com/coderising/lru/LRU.java new file mode 100644 index 0000000000..358646e087 --- /dev/null +++ b/group03/619224754/src/com/coderising/lru/LRU.java @@ -0,0 +1,75 @@ +package com.coderising.lru; + +public class LRU { + private static final int N = 5; + Object[] arr = new Object[N]; + + private int size; + + public boolean isEmpty() { + if(size == 0) { + return true; + } + else { + return false; + } + } + + public boolean isOutOfBoundary() { + if(size >= N) { + return true; + } + else { + return false; + } + } + + public int indexOfElement(Object o) { + for(int i = 0; i < size; i++){ + if(o == arr[i]) { + return i; + } + } + return -1; + } + + public Object push(Object o) { + Object popObject = null; + if(!isOutOfBoundary() && indexOfElement(o) == -1) { + arr[size] = o; + size++; + } + else if(isOutOfBoundary() && indexOfElement(o) == -1) { + popObject = arr[0]; + System.arraycopy(arr, 1, arr, 0, size -1); + arr[size - 1] = o; + } + else { + int index = indexOfElement(o); + System.arraycopy(arr, index + 1, arr, index, size - index - 1); + arr[size -1] = o; + } + + return popObject; + } + + public void showMemoryBlock() { + for(int i=0; i 0) { + for(int i = 0; i < size; i++) { + retStr += "," + arr[i]; + } + retStr = retStr.substring(1); + } + return retStr; + + } + +} diff --git a/group03/619224754/src/com/coding/basic/Dequeue.java b/group03/619224754/src/com/coding/basic/Dequeue.java new file mode 100644 index 0000000000..d80a07838a --- /dev/null +++ b/group03/619224754/src/com/coding/basic/Dequeue.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class Dequeue { + private int size = 0; + private LinkedList list = new LinkedList(); + + public void enHeadQueue(Object o){ + list.addFirst(o); + } + + public Object deHeadQueue(){ + Object ret = list.removeFirst(); + return ret; + } + + public void enLastQueue(Object o){ + list.addLast(o); + } + + public Object deLastQueue(){ + Object ret = list.removeLast(); + return ret; + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group03/619224754/src/test/LRUTest.java b/group03/619224754/src/test/LRUTest.java new file mode 100644 index 0000000000..c777482e38 --- /dev/null +++ b/group03/619224754/src/test/LRUTest.java @@ -0,0 +1,23 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coderising.lru.LRU; + +public class LRUTest { + + @Test + public void test() { + + Integer iter[] = {4,7,0,7,1,0,1,2,1,2,6}; + LRU lru = new LRU(); + for(int i=0; i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node headNode = head; + Node newNode = null; + + reverseNode(headNode, newNode); + } + + private void reverseNode(Node headNode, Node newHead){ + if(headNode != null){ + return ; + } + + Node next = headNode.next; + headNode.next = newHead; + + if(next == null){ + return ; + } + + reverseNode(next, headNode); + } + + private void checkLinkedListSize(){ + if(this.size() <= 0){ + throw new IndexOutOfBoundsException(); + } + } + + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + //check empty + checkLinkedListSize(); + int count = 1; + Node pNode = head; + for (int i = 0; i < size() / 2 - 1; i++) { + pNode = pNode.next; + count++; + } + head = pNode.next; + size = size() - count; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + //check empty + // check i and length are validate + checkLinkedListSize(); + checkLinkedListIndex(i); + checkLinkedListIndex(i+length); + + if(i == 0){ + Node pNode = getNode(length - 1); + head = pNode.next; + } else { + Node pNode = getNode(i - 1); + Node tempNode = getNode(i + length - 1); + pNode.next = tempNode.next; + } + size = size - length; + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] newArray = new int[list.size()]; + ArrayList newArrayList = new ArrayList(); + for(int i = 0; i < list.size(); i++){ + newArray[i] = (int)this.get((int)list.get(i)); + } + + return newArray; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * @param list + */ + + public void subtract(LinkedList list){ + ArrayList arrayList = new ArrayList(); + for(int i = 0; i < this.size(); i++){ + for(int j = 0; j < list.size(); j++){ + if(this.get(i) == list.get(j)){ + arrayList.add(i); + break; + } + } + } + + for(int k = 0; k < arrayList.size(); k++){ + this.remove((int)arrayList.get(k) - k); + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + ArrayList arrayList = new ArrayList(); + for(int i = 0; i < this.size() - 1; i++){ + if(this.get(i) == this.get(i+1)){ + arrayList.add(i); + } + } + + for(int k = 0; k < arrayList.size(); k++){ + this.remove((int)arrayList.get(k) - k); + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + ArrayList newArrayList = new ArrayList(); + int count = 0; + int start = 0; + int end = 0; + for (int i = 0; i < this.size(); i++) { + if(min >= (int)this.get(i)){ + start = i+1; + } + if(max >= (int)this.get(i)){ + end = i; + } + } + this.remove(start, end-start); + } + + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList newLinkedList = new LinkedList(); + int l1 = 0; + int l2 = 0; + while(l1 < this.size() && l2 < list.size()){ + if(this.get(l1) == list.get(l2)){ + newLinkedList.add(this.get(l1)); + l1 ++; + l2 ++; + } else if ((int)this.get(l1) > (int)list.get(l2)){ + l1 ++; + } else { + l2 ++; + } + } + + return newLinkedList; + } + + private boolean contains(Object obj){ + for(int i = 0; i < this.size(); i++){ + if(this.get(i) == obj){ + return true; + } + } + return false; + } + } diff --git a/group03/664269713/DataStructure/src/com/ace/coding/LinkedListTest.java b/group03/664269713/DataStructure/src/com/ace/coding/LinkedListTest.java new file mode 100644 index 0000000000..913a8e222c --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/LinkedListTest.java @@ -0,0 +1,33 @@ +package com.ace.coding; + +/** + * Created by ace on 2017/3/11. + */ +public class LinkedListTest { + public static void showLinkedList(LinkedList linkedList){ + for (int i = 0; i < linkedList.size(); i++){ + System.out.println(linkedList.get(i)); + } + } + public static void main(String[] args){ + LinkedList linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(3); + linkedList.add(3); + linkedList.add(3); + linkedList.add(10); + + LinkedList numberLink = new LinkedList(); + numberLink.add(2); + numberLink.add(3); + numberLink.add(5); + + /*int[] newArray = linkedList.getElements(numberLink); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + }*/ + numberLink.reverse(); +// linkedList.removeRange(3,8); + showLinkedList(numberLink); + } +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/DownloadThread.java b/group03/664269713/DataStructure/src/com/ace/download/DownloadThread.java new file mode 100644 index 0000000000..cbba41791b --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/DownloadThread.java @@ -0,0 +1,46 @@ +package com.ace.download; + +import com.ace.download.api.Connection; +import com.ace.download.api.DownloadListener; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + private Connection conn; + private int startPos; + private int endPos; + CyclicBarrier barrier; + String filePath; + + public DownloadThread(Connection conn, int startPos, int endPos, String filePath, CyclicBarrier barrier){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.filePath = filePath; + this.barrier = barrier; + } + public void run(){ + try { + byte[] bytes = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile(filePath, "rwd"); + raf.seek(startPos); + raf.write(bytes); + raf.close(); + conn.close(); + barrier.await(); + + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } + } +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/FileDownloader.java b/group03/664269713/DataStructure/src/com/ace/download/FileDownloader.java new file mode 100644 index 0000000000..edf3cf6a22 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/FileDownloader.java @@ -0,0 +1,99 @@ +package com.ace.download; + + +import com.ace.download.api.Connection; +import com.ace.download.api.ConnectionException; +import com.ace.download.api.ConnectionManager; +import com.ace.download.api.DownloadListener; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + String filePath; + + + public FileDownloader(String _url, String filePath) { + this.url = _url; + this.filePath = filePath; + } + + private String generateFileName(String url){ + String fileName = url.substring(url.lastIndexOf("/")); + return fileName; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + CyclicBarrier barrier = new CyclicBarrier(3 , new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + + RandomAccessFile raf = new RandomAccessFile(filePath, "rwd"); + + conn = cm.open(this.url); + raf.setLength(conn.getContentLength()); + + int length = conn.getContentLength(); + int partSize = (length % 3 == 0) ? length / 3 : (length / 3 + 1); + + for(int i = 0; i < 3; i++) { + int startPos = partSize * i; + int endPos = partSize * (i + 1) - 1; + new DownloadThread(conn, startPos, endPos, filePath, barrier).start(); + } + + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/api/Connection.java b/group03/664269713/DataStructure/src/com/ace/download/api/Connection.java new file mode 100644 index 0000000000..6807142b28 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.ace.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength() throws IOException; + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/api/ConnectionException.java b/group03/664269713/DataStructure/src/com/ace/download/api/ConnectionException.java new file mode 100644 index 0000000000..b311dfb1c6 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.ace.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/api/ConnectionManager.java b/group03/664269713/DataStructure/src/com/ace/download/api/ConnectionManager.java new file mode 100644 index 0000000000..7516cdcf42 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/api/ConnectionManager.java @@ -0,0 +1,13 @@ +package com.ace.download.api; + +import java.io.IOException; +import java.net.MalformedURLException; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException, IOException; +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/api/DownloadListener.java b/group03/664269713/DataStructure/src/com/ace/download/api/DownloadListener.java new file mode 100644 index 0000000000..0ac2adb6c8 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.ace.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/impl/ConnectionImpl.java b/group03/664269713/DataStructure/src/com/ace/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..273da2a995 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/impl/ConnectionImpl.java @@ -0,0 +1,57 @@ +package com.ace.download.impl; + +import com.ace.download.api.Connection; +import com.ace.download.api.ConnectionManager; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URLConnection; +import java.util.Arrays; + + +public class ConnectionImpl implements Connection { + private URL urlObj; + private InputStream inputStream; + private ByteArrayOutputStream byteArrayOutputStream; + + public ConnectionImpl(URL urlObj){ + this.urlObj = urlObj; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + URLConnection urlConnection = urlObj.openConnection(); + urlConnection.setRequestProperty("Range","bytes=" + startPos + "-" + endPos); + inputStream = urlConnection.getInputStream(); + byteArrayOutputStream = new ByteArrayOutputStream(); + + byte[] bytes = new byte[1024]; + int length = 0; + while((length = inputStream.read(bytes)) != -1){ + byteArrayOutputStream.write(bytes, 0, length); + } + byte[] data = byteArrayOutputStream.toByteArray(); + int contentLen = endPos - startPos + 1; + if(byteArrayOutputStream.size() > contentLen){ + data = Arrays.copyOf(data, contentLen); + } + + return data; + } + + @Override + public int getContentLength() throws IOException { + URLConnection urlConnection = urlObj.openConnection(); + return urlConnection.getContentLength(); + } + + @Override + public void close() { + + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/download/impl/ConnectionManagerImpl.java b/group03/664269713/DataStructure/src/com/ace/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..0509bed745 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,28 @@ +package com.ace.download.impl; + + +import com.ace.download.api.Connection; +import com.ace.download.api.ConnectionException; +import com.ace.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + private URL urlObj; + private Connection connection; + + @Override + public Connection open(String url) { + try { + urlObj = new URL(url); + connection = new ConnectionImpl(urlObj); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + return connection; + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/homework2/Struts.java b/group03/664269713/DataStructure/src/com/ace/homework2/Struts.java index 175ce8a10c..e2db085b2b 100644 --- a/group03/664269713/DataStructure/src/com/ace/homework2/Struts.java +++ b/group03/664269713/DataStructure/src/com/ace/homework2/Struts.java @@ -10,25 +10,21 @@ import java.util.List; import java.util.Map; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; public class Struts { /* - * 0. ȡļstruts.xml + * 0. ��ȡ�����ļ�struts.xml * - * 1. actionNameҵӦclass LoginAction, ͨʵ - * parametersеݣösetter parametersе ("name"="test" , - * "password"="1234") , ǾӦõ setNamesetPassword + * 1. ����actionName�ҵ����Ӧ��class �� ����LoginAction, ͨ������ʵ�������������� + * ��parameters�е����ݣ����ö����setter������ ����parameters�е������� ("name"="test" , + * "password"="1234") , �Ǿ�Ӧ�õ��� setName��setPassword���� * - * 2. ͨöexectue ÷ֵ"success" + * 2. ͨ��������ö����exectue ������ ����÷���ֵ������"success" * - * 3. ͨҵgetter getMessage, ͨã ֵγһHashMap , - * {"message": "¼ɹ"} , ŵViewparameters + * 3. ͨ�������ҵ����������getter���������� getMessage��, ͨ�����������ã� ��ֵ�������γ�һ��HashMap , ���� + * {"message": "��¼�ɹ�"} , �ŵ�View�����parameters * - * 4. struts.xmlе ,Լexecuteķֵ ȷһjsp ŵViewjspֶС + * 4. ����struts.xml�е� ����,�Լ�execute�ķ���ֵ�� ȷ����һ��jsp�� �ŵ�View�����jsp�ֶ��С� */ private static final String STRUTS_XML = "struts.xml"; private static final String NAME = "name"; @@ -38,7 +34,7 @@ public class Struts { private static final String SET = "set"; private static List getStrutsList() { - List strutsObjList = new ArrayList(); + /*List strutsObjList = new ArrayList(); URL path = Struts.class.getResource(STRUTS_XML); File f = new File(path.getFile()); SAXReader saxReader = new SAXReader(); @@ -66,7 +62,8 @@ private static List getStrutsList() { e.printStackTrace(); } - return strutsObjList; + return strutsObjList;*/ + return null; }; public static View runAction(String actionName, Map parameters) { diff --git a/group04/1020483199/1020483199Learning/.classpath b/group04/1020483199/1020483199Learning/.classpath index 3e0fb272a8..04cc82dc42 100644 --- a/group04/1020483199/1020483199Learning/.classpath +++ b/group04/1020483199/1020483199Learning/.classpath @@ -1,7 +1,7 @@ - + diff --git a/group04/1020483199/FourthHomeWork/src/com/coderising/jvm/loader/ClassFileLoader.java b/group04/1020483199/FourthHomeWork/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..ba70c84329 --- /dev/null +++ b/group04/1020483199/FourthHomeWork/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.loader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + /** + * class文件存储位置 + */ + String location = clzPaths.get(0); + File file = new File(location); + File[] files = file.listFiles(); + InputStream in = null; + byte[] bt = null; + + int size = 0; + for(File fileSon:files){ + /** + * 判断出为class文件时 + */ + if(fileSon.isFile() && fileSon.getName().endsWith("EmployeeV1.class")){ + try { + long length = fileSon.length(); + bt = new byte[(int) length]; + byte[] context = new byte[1024]; + in = new FileInputStream(fileSon); + int tempbyte; + while((tempbyte = in.read(context)) != -1){ + for(int i = 0;i < context.length;i++){ + System.arraycopy(context, 0, bt, size, tempbyte); + } + size = tempbyte; + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally{ + if(in != null){ + try { + in.close(); + } catch (IOException e) { + } + } + } + + } + } + return bt; + } + + + public void addClassPath(String path) { + + clzPaths.add(path); + + } + + + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + for(int i = 0;i < clzPaths.size();i++){ + if(i == clzPaths.size() - 1){ + sb.append(clzPaths.get(i)); + break; + } + sb.append(clzPaths.get(i)).append(";"); + + } + return sb.toString(); + } + + + + + +} diff --git a/group04/1020483199/FourthHomeWork/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group04/1020483199/FourthHomeWork/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..7c7d1bb15a --- /dev/null +++ b/group04/1020483199/FourthHomeWork/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,91 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "F:/myGithub/coding2017/group04/1020483199/FourthHomeWork/bin/com/coderising/jvm/test"; + static String path2 = "C:/temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i 2 > 1 > 5 > 4 > 5改变之前 + //0 1 2 3 4 5index + //3 > 2 > 1 > x > 5 > 4 > 5插入之后 + public void add(int index , Object o){ + if(head != null){ + int k = 0; + Node p = head; + while(k < index - 1 && p.next != null){ + k++; + p = p.next;//当前p为要插入位置的前一个节点 + } + + if(p != null){ + Node nd = new Node(o); + nd.next = p.next; + p.next = nd; + } + + size++; + + } + } + public Object get(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + Node p = head; + int k = 0; + while(k < index && p.next !=null){ + k++; + p = p.next; + } + return p.data; + } + //3 > 2 > 1 > 5 > 4 > 5改变之前 + //0 1 2 3 4 5index + //3 > 2 > 1 > 4 > 5插入之后 + public Object remove(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + if(head == null){ + return null; + } + if(index == 0){ + head = head.next; + size--; + return head.data; + }else{ + if(head != null){ + int k = 0; + Node p = head; + while(k < index - 1 && p != null){ + k++; + p = p.next; + } + Node pn = p.next; + if(pn != null){ + p.next = pn.next; + size--; + return pn.data; + } + + } + } + return null; + } + + public int size(){ + + return size; + } + + public void addFirst(Object o){ + if(head != null){ + Node nd = new Node(o); + Node first = head; + head = nd; + first = nd.next; + } + } + public void addLast(Object o){ + if(head != null){ + int k = 0; + Node p = head; + while(p.next != null && k < size - 1){ + p = p.next; + k++; + } + Node newNode = new Node(o); + p.next = newNode; + } + } + public Object removeFirst(){ + Node node = head; + if(head != null){ + head = head.next; + } + return node.data; + } + public Object removeLast(){ + Node p = head; + int k = 0; + while(p.next != null && k < size - 2){ + k++; + p = p.next; + } + + p.next = null; + return p.next; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + private Node(Object o){ + this.data = o; + this.next = null; + } + } + + /** + * 把该链表逆置 + * head head + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + /** + * 长度超过1的单链表需要逆转 + */ + if(head == null || head.next == null){ + return; + } + Stack st = new Stack(); + Node currentNode = head; + while(currentNode != null){ + st.push(currentNode); + Node nextNode = currentNode.next; + currentNode.next = null;//断开连接 + currentNode = nextNode; + } + + head = (Node) st.pop(); + currentNode = head; + while(!st.isEmpty()){ + Node nextNode = (Node) st.pop(); + currentNode.next = nextNode; + currentNode = nextNode; + } + } + + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int num = size / 2; + for(int i = 0; i < num; i++){ + removeFirst(); + } + } + + /** + * 从第i个元素开始,删除length个元素 ,注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(i < 0 || i >= size){ + throw new IndexOutOfBoundsException(); + } + int len = size - i >= length ? length : size - i; + int k = 0; + while(k < len){ + remove(i); + k++; + } + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] newList = new int[list.size()]; + for(int i = 0;i < list.size(); i++){ + newList[i] = Integer.parseInt(this.get(Integer.parseInt(list.get(i).toString())).toString()); + } + return newList; + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + for(int j = 0;j < list.size();j++){ + this.remove(list.get(j)); + } + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(head == null){ + throw new RuntimeException("LinkedList is null"); + + } + Node currentNode = head; + Node preNode = head; + while(currentNode.next != null){ + currentNode = currentNode.next; + Object data = preNode.data; + while(currentNode.data == data){ + if(currentNode.next == null){ + preNode.next = null; + break; + } + preNode.next = currentNode.next; + size--; + currentNode = currentNode.next; + if(currentNode == null){ + break; + } + } + preNode = preNode.next; + } + } + /** + * 传入删除数据节点 + */ + public void remove(Object obj){ + if(head == null){ + throw new RuntimeException("linkedlist is nuull"); + + } + if(head.data.equals(obj)){ + head = head.next; + size--; + }else{ + Node pre = head; + Node currentNode = head.next; + while(currentNode != null){ + if(currentNode.data.equals(obj)){ + pre.next = currentNode.next; + size--; + } + pre = pre.next; + currentNode = currentNode.next; + } + } + } + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + Node node = head; + int start = -1; + int end = -1; + int i = 0; + while(node != null){ + if((Integer)node.data <= min){ + start = i; + } + if((Integer)node.data >= max){ + end = i; + break; + } + node = node.next; + i++; + } + if(start == -1){ + start = 0; + } + if(end == -1){ + end = size; + } + this.remove(start+1, end-start-1); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + if(list == null){ + return null; + } + + LinkedList newList = new LinkedList(); + int fi = 0; + int se = 0; + while(fi < this.size && se < list.size()){ + int val1 = (Integer) this.get(fi); + int val2 = (Integer) list.get(se); + if(val1 == val2){ + newList.add(val1); + fi++; + se++; + }else if(val1 < val2){ + fi++; + }else{ + se++; + } + + } + return newList; + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(22); + linkedList.add(33); + linkedList.add(44); + linkedList.add(55); + linkedList.reverse(); + for(int i = 0; i < linkedList.size; i++){ + System.out.println(linkedList.get(i)); + } + } +} diff --git a/group04/1020483199/FourthHomeWork/src/com/coding/basic/linkedList/List.java b/group04/1020483199/FourthHomeWork/src/com/coding/basic/linkedList/List.java new file mode 100644 index 0000000000..55ff205b5d --- /dev/null +++ b/group04/1020483199/FourthHomeWork/src/com/coding/basic/linkedList/List.java @@ -0,0 +1,9 @@ +package com.coding.basic.linkedList; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/array/ArrayUtil.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..e5ddb476a6 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/DownloadThread.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..d16eca2319 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,42 @@ +package com.coderising.download; + + + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + + private Connection conn; + private int startPos; + private int endPos; + private String filePath; + private CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos,String filePath,CyclicBarrier barrier){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.filePath = filePath; + this.barrier = barrier; + } + public void run(){ + try { + System.out.println("线程" + this.getName() + "开始下载. startPos:" + startPos + "; endPos:" + endPos); + byte[] buffer = conn.read(startPos, endPos); + RandomAccessFile ra = new RandomAccessFile(filePath, "rw"); + ra.seek(startPos); + ra.write(buffer); + //关闭流 + ra.close(); + conn.close(); + barrier.await(); + System.out.println("线程" + this.getName() + "下载完成."); + }catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/FileDownloader.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c6cdf3fc4a --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,139 @@ +package com.coderising.download; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CyclicBarrier; + +import org.junit.internal.runners.statements.RunAfters; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + private String url; + + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + private final static int thread_count = 3; + + public FileDownloader(String _url,String localFile) { + this.url = _url; + this.localFile = localFile; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接, + //通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, + //调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, + //这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + //然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, + //返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + CyclicBarrier barrier = new CyclicBarrier(thread_count, new Runnable() { + + @Override + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + createPlaceHolderFile(this.localFile,length); + + int[] [] ranges = allowcateDownloadRange(thread_count,length); + + for(int i = 0; i < thread_count; i++){ + + DownloadThread thread= new DownloadThread(conn, ranges[i][0], ranges[i][1], localFile, barrier); + + thread.start(); + + } + + } catch (Exception e) { + e.printStackTrace(); + + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + private int[][] allowcateDownloadRange(int threadCount, int length) { + int[][] ranges = new int[thread_count][2]; + + int eachThreadSize = length / threadCount; + + int left = length % threadCount; + + for(int i = 0 ; i < thread_count;i++){ + int startPos = i * eachThreadSize; + + int endPos = (i+1) * eachThreadSize - 1; + + if((i==thread_count -1)){ + endPos += left; + } + + ranges[i][0] = startPos; + ranges[i][1] = endPos; + } + + return ranges; + } + + private void createPlaceHolderFile(String localfile, int length) throws IOException { + RandomAccessFile file = new RandomAccessFile(localfile, "rw"); + for(int i = 0;i < length ; i++){ + file.write(0); + } + file.close(); + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/FileDownloaderTest.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..7393f57040 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,90 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testContentLength() throws Exception{ + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception{ + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + + // 测试不充分,没有断言内容是否正确 + } + + @Test + public void testDownload() { + + String url = "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + + FileDownloader downloader = new FileDownloader(url, "F:/picture/a.png"); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/Connection.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/ConnectionException.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..105f00f03a --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,13 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + + /** + * 自定义异常错误 + * @param string + */ + public ConnectionException(Exception e) { + super(e); + } + +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/ConnectionManager.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/DownloadListener.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..7b51663ff9 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,6 @@ +package com.coderising.download.api; + +public interface DownloadListener { + + public void notifyFinished(); +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/impl/ConnectionImpl.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..dcaf5883fa --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,89 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; + +class ConnectionImpl implements Connection { + + URL url; + static final int BUFFER_SIZE = 1024; + + ConnectionImpl(String _url) throws ConnectionException{ + try { + url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + + HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); + + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + + endPos); + + InputStream is = httpConn.getInputStream(); + + //is.skip(startPos); + + byte[] buff = new byte[BUFFER_SIZE]; + + int totalLen = endPos - startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while(baos.size() < totalLen){ + + int len = is.read(buff); + if (len < 0) { + break; + } + baos.write(buff,0, len); + } + + + if(baos.size() > totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + + + } + + @Override + public void close() { + + + } + +} \ No newline at end of file diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..3af9b21485 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,17 @@ +package com.coderising.download.impl; + +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/LoginAction.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/LoginAction.java similarity index 100% rename from group15/1502_1617273078/src/com/coderising/litestruts/LoginAction.java rename to group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/LoginAction.java diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/Struts.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..6df190d484 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.Map; + + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/StrutsTest.java similarity index 100% rename from group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java rename to group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/StrutsTest.java diff --git a/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/View.java b/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..3fe0a702d4 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + + private Map parameters; + + public String getJsp() { + return jsp; + } + + public void setJsp(String jsp) { + this.jsp = jsp; + } + + public Map getParameters() { + return parameters; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } + + +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coding/basic/ArrayList.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/BinaryTreeNode.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/BinaryTreeNode.java rename to group04/1020483199/ThirdHomeWork/src/com/coding/basic/BinaryTreeNode.java diff --git a/group12/446031103/src/com/coding/basic/Iterator.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Iterator.java similarity index 100% rename from group12/446031103/src/com/coding/basic/Iterator.java rename to group04/1020483199/ThirdHomeWork/src/com/coding/basic/Iterator.java diff --git a/group04/1020483199/ThirdHomeWork/src/com/coding/basic/LinkedList.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..7b10ffb8b6 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/LinkedList.java @@ -0,0 +1,374 @@ +package com.coding.basic; + +import java.util.Stack; + +public class LinkedList implements List { + private int size; + + private Node head; + + public LinkedList(){ + size = 0; + + head = null; + } + + public void add(Object o){ + Node nd = new Node(o); + if(head == null){ + head = nd; + }else{ + Node p = head; + while(p.next != null){ + p = p.next; + } + p.next = nd; + } + size ++; + + } + //3 > 2 > 1 > 5 > 4 > 5改变之前 + //0 1 2 3 4 5index + //3 > 2 > 1 > x > 5 > 4 > 5插入之后 + public void add(int index , Object o){ + if(head != null){ + int k = 0; + Node p = head; + while(k < index - 1 && p.next != null){ + k++; + p = p.next;//当前p为要插入位置的前一个节点 + } + + if(p != null){ + Node nd = new Node(o); + nd.next = p.next; + p.next = nd; + } + + size++; + + } + } + public Object get(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + Node p = head; + int k = 0; + while(k < index && p.next !=null){ + k++; + p = p.next; + } + return p.data; + } + //3 > 2 > 1 > 5 > 4 > 5改变之前 + //0 1 2 3 4 5index + //3 > 2 > 1 > 4 > 5插入之后 + public Object remove(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + if(head == null){ + return null; + } + if(index == 0){ + head = head.next; + size--; + return head.data; + }else{ + if(head != null){ + int k = 0; + Node p = head; + while(k < index - 1 && p != null){ + k++; + p = p.next; + } + Node pn = p.next; + if(pn != null){ + p.next = pn.next; + size--; + return pn.data; + } + + } + } + return null; + } + + public int size(){ + + return size; + } + + public void addFirst(Object o){ + if(head != null){ + Node nd = new Node(o); + Node first = head; + head = nd; + first = nd.next; + } + } + public void addLast(Object o){ + if(head != null){ + int k = 0; + Node p = head; + while(p.next != null && k < size - 1){ + p = p.next; + k++; + } + Node newNode = new Node(o); + p.next = newNode; + } + } + public Object removeFirst(){ + Node node = head; + if(head != null){ + head = head.next; + } + return node.data; + } + public Object removeLast(){ + Node p = head; + int k = 0; + while(p.next != null && k < size - 2){ + k++; + p = p.next; + } + + p.next = null; + return p.next; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + private Node(Object o){ + this.data = o; + this.next = null; + } + } + + /** + * 把该链表逆置 + * head head + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + /** + * 长度超过1的单链表需要逆转 + */ + if(head == null || head.next == null){ + return; + } + Stack st = new Stack(); + Node currentNode = head; + while(currentNode != null){ + st.push(currentNode); + Node nextNode = currentNode.next; + currentNode.next = null;//断开连接 + currentNode = nextNode; + } + + head = (Node) st.pop(); + currentNode = head; + while(!st.isEmpty()){ + Node nextNode = (Node) st.pop(); + currentNode.next = nextNode; + currentNode = nextNode; + } + } + + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int num = size / 2; + for(int i = 0; i < num; i++){ + removeFirst(); + } + } + + /** + * 从第i个元素开始,删除length个元素 ,注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(i < 0 || i >= size){ + throw new IndexOutOfBoundsException(); + } + int len = size - i >= length ? length : size - i; + int k = 0; + while(k < len){ + remove(i); + k++; + } + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] newList = new int[list.size()]; + for(int i = 0;i < list.size(); i++){ + newList[i] = Integer.parseInt(this.get(Integer.parseInt(list.get(i).toString())).toString()); + } + return newList; + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + for(int j = 0;j < list.size();j++){ + this.remove(list.get(j)); + } + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(head == null){ + throw new RuntimeException("LinkedList is null"); + + } + Node currentNode = head; + Node preNode = head; + while(currentNode.next != null){ + currentNode = currentNode.next; + Object data = preNode.data; + while(currentNode.data == data){ + if(currentNode.next == null){ + preNode.next = null; + break; + } + preNode.next = currentNode.next; + size--; + currentNode = currentNode.next; + if(currentNode == null){ + break; + } + } + preNode = preNode.next; + } + } + /** + * 传入删除数据节点 + */ + public void remove(Object obj){ + if(head == null){ + throw new RuntimeException("linkedlist is nuull"); + + } + if(head.data.equals(obj)){ + head = head.next; + size--; + }else{ + Node pre = head; + Node currentNode = head.next; + while(currentNode != null){ + if(currentNode.data.equals(obj)){ + pre.next = currentNode.next; + size--; + } + pre = pre.next; + currentNode = currentNode.next; + } + } + } + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + Node node = head; + int start = -1; + int end = -1; + int i = 0; + while(node != null){ + if((Integer)node.data <= min){ + start = i; + } + if((Integer)node.data >= max){ + end = i; + break; + } + node = node.next; + i++; + } + if(start == -1){ + start = 0; + } + if(end == -1){ + end = size; + } + this.remove(start+1, end-start-1); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + if(list == null){ + return null; + } + + LinkedList newList = new LinkedList(); + int fi = 0; + int se = 0; + while(fi < this.size && se < list.size()){ + int val1 = (Integer) this.get(fi); + int val2 = (Integer) list.get(se); + if(val1 == val2){ + newList.add(val1); + fi++; + se++; + }else if(val1 < val2){ + fi++; + }else{ + se++; + } + + } + return newList; + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(22); + linkedList.add(33); + linkedList.add(44); + linkedList.add(55); + linkedList.reverse(); + for(int i = 0; i < linkedList.size; i++){ + System.out.println(linkedList.get(i)); + } + } +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/List.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/List.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/List.java rename to group04/1020483199/ThirdHomeWork/src/com/coding/basic/List.java diff --git a/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Queue.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Stack.java b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a25d5b8024 --- /dev/null +++ b/group04/1020483199/ThirdHomeWork/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group04/120549547/base/buil.bat b/group04/120549547/base/buil.bat deleted file mode 100644 index 7164a42ac0..0000000000 --- a/group04/120549547/base/buil.bat +++ /dev/null @@ -1,5 +0,0 @@ -javac -sourcepath src -d classes -encoding utf-8 src\com\coding\basic\*.java - -java -cp classes com.coding.basic.Main - -pause \ No newline at end of file diff --git a/group04/120549547/base/src/com/coding/basic/ArrayList.java b/group04/120549547/base/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 56e7aa4c26..0000000000 --- a/group04/120549547/base/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.coding.basic; -import java.util.Arrays; - -public class ArrayList implements List { - /*默认数组容量*/ - public static final int DEFAULT_CAPCATITY = 10; - - /*数组元素个数*/ - private int size; - - private Object[] elementData; - - public ArrayList(){ - this(DEFAULT_CAPCATITY); - } - - public ArrayList(int capacity){ - if (capacity<0 || capacity>Integer.MAX_VALUE) - throw new IllegalArgumentException("非法容量: " + capacity); - elementData = new Object[capacity]; - } - - - public void add(Object o){ - /*判断是否需要扩容*/ - ensureCapacity(size + 1); - elementData[size++] = o; - } - - public void add(int index, Object o){ - checkIndex(index); - /*判断是否需要扩容*/ - ensureCapacity(size + 1); - /*将index->size的元素依次向右移一个位置*/ - System.arraycopy(elementData, index, elementData, index+1, size-index); - /*插入o的值*/ - elementData[index] = o; - size++; - } - - - public Object get(int index){ - - checkIndex(index); - return elementData[index]; - } - - public Object remove(int index){ - - checkIndex(index); - Object value = elementData[index]; - /*将index+1 -> size的元素依次向左移一个位置*/ - System.arraycopy(elementData,index+1, elementData, index, size-index-1); - elementData[--size]=null; //释放最后一个元素,同时size减一; - return value; - } - - public int size(){ - return this.size; - } - - public Iterator iterator(){ - return null; - } - /* 确认容量是否足够,也可以直接调用手动扩容*/ - public void ensureCapacity(int minCapacity){ - int oldCapacity = elementData.length; - /*容量不足需要扩容*/ - if(oldCapacity < minCapacity){ - int newCapacity = oldCapacity * 2; //扩大2倍 - if (newCapacity < minCapacity){ - newCapacity = minCapacity; //扩容后仍不足,取最大值 - } - System.out.println("数据扩容至: " + newCapacity); - elementData = Arrays.copyOf(elementData, newCapacity); - } - - } - - private void checkIndex(int index){ - if (index<0 || index>= size) - throw new IndexOutOfBoundsException ("index非法: " + index); - } - @Override - public String toString(){ - - StringBuffer sb = new StringBuffer(""); - for(int i=0; i "); - } - return sb.toString(); - } -} diff --git a/group04/120549547/base/src/com/coding/basic/LinkedList.java b/group04/120549547/base/src/com/coding/basic/LinkedList.java deleted file mode 100644 index edfbcc1007..0000000000 --- a/group04/120549547/base/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private int size; - /*创建一个带头节点的单链表*/ - public LinkedList(){ - head = new Node(null); - } - - /*添加一个元素(尾插法)*/ - public void add(Object o){ - - Node node = new Node(o); - Node pos = head; - //找到最后一个元素位置 - while(pos.next != null){ - pos = pos.next; - } - pos.next = node; - size++; - - } - - /*在index位置插入*/ - public void add(int index , Object o){ - //判断索引是否合法 - checkIndex(index); - Node node = new Node(o); - Node pos = head; - //找到插入位置 - while(index-- != 0){ - pos = pos.next; - } - node.next = pos.next; - pos.next = node; - size++; - - } - public Object get(int index){ - checkIndex(index); - if(this.isEmpty()){ - throw new IllegalArgumentException ("链表为空"); - } - Node pos = head.next; //pos指向要获取的节点 - while(index-- !=0){ - pos = pos.next; - } - - return pos.data; - } - public Object remove(int index){ - checkIndex(index); - if(this.isEmpty()){ - throw new IllegalArgumentException ("链表为空"); - } - - Node pos = head; //pos指向要删除的前一个结点 - while(index-- != 0){ - pos = pos.next; - } - Node value = pos.next; //要删除的节点 - pos.next = value.next; - size--; - return value.data; - - - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - this.add(o); - } - public Object removeFirst(){ - if(this.isEmpty()){ - throw new IllegalArgumentException ("链表为空"); - } - return remove(0); - } - public Object removeLast(){ - if(this.isEmpty()){ - throw new IllegalArgumentException ("链表为空"); - } - return remove(size-1); - } - - public boolean isEmpty(){ - return size == 0; - } - public Iterator iterator(){ - return null; - } - - private void checkIndex(int index){ - if (index<0 || index>=size ){ - throw new IllegalArgumentException ("index不合法: " + index ); - } - } - - @Override - public String toString(){ - - StringBuffer sb = new StringBuffer(""); - for(int i=0; i "); - } - return sb.toString(); - } - - private static class Node{ - public Object data; - public Node next; - - public Node(Object data){ - this.data = data; - this.next = null; - } - - } -} diff --git a/group04/120549547/base/src/com/coding/basic/List.java b/group04/120549547/base/src/com/coding/basic/List.java deleted file mode 100644 index d47df585d9..0000000000 --- a/group04/120549547/base/src/com/coding/basic/List.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coding.basic; - -public interface List { - - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/group04/120549547/base/src/com/coding/basic/Main.java b/group04/120549547/base/src/com/coding/basic/Main.java deleted file mode 100644 index de8a0b0990..0000000000 --- a/group04/120549547/base/src/com/coding/basic/Main.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.coding.basic; -import com.coding.basic.*; - -class Main{ - - public static void main(String[] args){ - System.out.println("数组测试开始"); - ArrayListTest(); - System.out.println("----------------分割线----------------"); - System.out.println("链表测试开始"); - LinkedListTest(); - System.out.println("----------------分割线----------------"); - System.out.println("栈测试开始"); - StatckTest(); - System.out.println("----------------分割线----------------"); - System.out.println("队测试开始"); - QueueTest(); - } - - public static void ArrayListTest(){ - ArrayList list = new ArrayList(2); - list.add("HelloBobi0"); - list.add("HelloBobi1"); - list.add("HelloBobi2"); - list.add("HelloBobi3"); - list.add("HelloBobi4"); - list.add("HelloBobi5"); - System.out.println((String)list.get(0)); - System.out.println((String)list.get(4)); - list.add(3, "Hei Man"); - list.remove(5); - System.out.println(list); - System.out.println("size:=" + list.size()); - } - - public static void LinkedListTest(){ - LinkedList ll = new LinkedList(); - - - ll.add("SingleDog0"); - ll.add("SingleDog1"); - ll.add("SingleDog2"); - ll.add("SingleDog3"); - ll.add("SingleDog4"); - ll.add("SingleDog5"); - - System.out.println((String)(ll.get(1))); - System.out.println(ll); - System.out.println("size:=" + ll.size()); - ll.remove(0); - ll.removeFirst(); - ll.removeLast(); - System.out.println(ll); - } - - public static void StatckTest(){ - Stack stack = new Stack(); - stack.push("虾师傅0"); - stack.push("虾师傅1"); - stack.push("虾师傅2"); - stack.push("虾师傅3"); - stack.push("虾师傅4"); - - stack.pop(); - System.out.println(stack.peek()); - System.out.println(stack); - - } - public static void QueueTest(){ - Queue queue = new Queue(); - queue.enQueue("龙师傅0"); - queue.enQueue("龙师傅1"); - queue.enQueue("龙师傅2"); - queue.enQueue("龙师傅3"); - queue.enQueue("龙师傅4"); - - System.out.println(queue.deQueue()); - System.out.println(queue.deQueue()); - System.out.println(queue.deQueue()); - System.out.println(queue.deQueue()); - System.out.println(queue.size()); - - - } -} \ No newline at end of file diff --git a/group04/120549547/base/src/com/coding/basic/Queue.java b/group04/120549547/base/src/com/coding/basic/Queue.java deleted file mode 100644 index 8f86f7492c..0000000000 --- a/group04/120549547/base/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.coding.basic; -import com.coding.basic.*; -public class Queue { - private LinkedList list; - - public Queue(){ - list = new LinkedList(); - } - /*入队*/ - public void enQueue(Object o){ - list.addLast(o); - } - - /*出队*/ - public Object deQueue(){ - if (isEmpty()){ - System.out.println("队空"); - } - return list.removeFirst(); - } - - public boolean isEmpty(){ - return list.isEmpty(); - } - - public int size(){ - return list.size(); - } -} diff --git a/group04/120549547/base/src/com/coding/basic/Stack.java b/group04/120549547/base/src/com/coding/basic/Stack.java deleted file mode 100644 index f7d3bba3db..0000000000 --- a/group04/120549547/base/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.coding.basic; -import com.coding.basic.LinkedList; - -public class Stack { - private LinkedList list; - - public Stack(){ - list = new LinkedList(); - } - - public void push(Object o){ - list.addLast(o); - } - - public Object pop(){ - if(isEmpty()){ - System.out.println("栈空"); - } - return list.removeLast(); - - } - - public Object peek(){ - return list.get(list.size()-1); - } - public boolean isEmpty(){ - return list.isEmpty(); - } - public int size(){ - return list.size(); - } - - @Override - public String toString(){ - return list.toString(); - } -} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/BinaryTreeNode.java b/group04/120549547/code2017/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/Iterator.java b/group04/120549547/code2017/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..31d6470bef --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + + T next(); + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/List.java b/group04/120549547/code2017/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..70a783968e --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/List.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public interface List{ + int size(); + + boolean isEmpty(); + + boolean contains(Object o); + + Object[] toArray(); + + boolean add(T o); + + boolean remove(T o); + + void clear(); + + T get(int index); + + T set(int index, T o); + + void add(int index, T o); + + T remove(int index); + + int indexOf(T o); + + Iterator iterator(); + + void printf(); +} diff --git a/liuxin/data-structure/src/com/coding/basic/Stack.java b/group04/120549547/code2017/src/main/java/com/coding/basic/Stack.java similarity index 100% rename from liuxin/data-structure/src/com/coding/basic/Stack.java rename to group04/120549547/code2017/src/main/java/com/coding/basic/Stack.java diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/array/ArrayList.java b/group04/120549547/code2017/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..6c392a3618 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,155 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +import java.util.Objects; + +public class ArrayList implements List { + + private int size; + private Object[] dataArray = new Object[0]; + + public int size() { + return this.size; + } + + public boolean isEmpty() { + return this.size == 0; + } + + public boolean contains(Object o) { + for (Object obj : dataArray) { + if (Objects.equals(obj, o)){ + return true; + } + } + return false; + } + + public Object[] toArray() { + Object[] array = new Object[size]; + System.arraycopy(dataArray, 0, array, 0, size); + return array; + } + + public boolean add(T o) { + ensureCapacity(size+1); + dataArray[size] = o; + size++; + return true; + } + + + + public boolean remove(T o) { + int index = indexOf(o); + if (index < 0){ + return false; + } + + System.arraycopy(dataArray, index + 1, dataArray, index, size - 1 - index); + dataArray[size - 1] = null; + size--; + return true; + } + + public void clear() { + for (int i = 0; i < size; i++) { + dataArray[i] = null; + } + size = 0; + } + @SuppressWarnings("unchecked") + public T get(int index) { + if (index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + + return (T) dataArray[index]; + } + + public T set(int index, T o) { + if (index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + dataArray[index] = o; + return o; + } + + public void add(int index, T o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + ensureCapacity(size + 1); + System.arraycopy(dataArray, index, dataArray, index + 1, size - index); + + dataArray[index] = o; + + size++; + } + + public T remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + T removeData = (T) dataArray[index]; + System.arraycopy(dataArray, index + 1, dataArray, index, size - index -1 ); + dataArray[size - 1] = null; + size--; + return removeData; + } + + public int indexOf(T o) { + for (int i = 0; i < size; i++) { + if (Objects.equals(o, dataArray[i])){ + return i; + } + } + return -1; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + @Override + public void printf() { + for (int i = 0; i < size; i++) { + if (i == size - 1){ + System.out.print(dataArray[i]); + }else { + System.out.print(dataArray[i] + "->"); + } + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > dataArray.length){ + int newCapacity = Math.max(minCapacity, dataArray.length * 2); + Object[] newDataArray = new Object[newCapacity]; + System.arraycopy(dataArray, 0, newDataArray, 0, dataArray.length); + dataArray = newDataArray; + } + + } + + + private class ArrayListIterator implements Iterator { + + private int pos; + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public T next() { + if (hasNext()){ + return get(pos++); + } + return null; + } + } +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/array/ArrayUtil.java b/group04/120549547/code2017/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..411b60990b --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,269 @@ +package com.coding.basic.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + + if (origin == null || origin.length == 0){ + return; + } + + int temp ; + for (int i = 0; i < (origin.length / 2); i++) { + temp = origin[i]; + origin[i] = origin[(origin.length - 1) - i]; + origin[(origin.length - 1)- i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if (oldArray == null ){ + return null; + } + + if (oldArray.length == 0){ + return oldArray; + } + + int count = 0; + int[] temp = new int[oldArray.length]; + + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0){ + temp[count++] = oldArray[i]; + } + } + + int[] result = new int[count]; + + System.arraycopy(temp, 0, result, 0, count); + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null){ + return null; + } + + if (array1 == null){ + return array2; + } + + if (array2 == null){ + return array1; + } + + int length1 = array1.length; + int length2 = array2.length; + int[] temp = new int[length1 + length2]; + int i,j,k; + for ( i = 0, j = 0, k = 0; i < length1 && j < length2 ;) { + + if (array1[i] < array2[j]){ + temp[k++] = array1[i++]; + }else if (array1[i] > array2[j]){ + temp[k++] = array2[j++]; + }else { + temp[k++] = array1[i]; + i++; + j++; + } + } + + while (i < length1){ + temp[k++] = array1[i++]; + } + + while (j < length2){ + temp[k++] = array2[j++]; + } + int[] result = new int[k]; + System.arraycopy(temp, 0, result, 0, k); + return result; + + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + + int oldSize = oldArray.length; + int[] newArray = new int[oldSize + size]; + + System.arraycopy(oldArray, 0, newArray, 0, oldSize); + + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci1(int max){ + if (max == 1){ + return new int[0]; + } + + int[] temp = new int[max]; + int count = 0; +// for (int i = 1; i < max; i++) { +// int num = fibonacci(i); +// if (num < max){ +// temp[count++] = num; +// }else { +// break; +// } +// } + temp[0] = 1; + temp[1] = 2; + for (int i = 2; i < max; i++) { + temp[i] = temp[i-1] + temp[i-2]; //直接在数组中实现斐波那契数列 + if (temp[i] >= max){ + break; + }else { + count++; + } + } + + + + return Arrays.copyOf(temp, count); + } + + private static int fibonacci(int n){ + if (n <= 0){ + throw new IllegalArgumentException(); + } + + if (n == 1 || n == 2) { + return 1; + } + + return fibonacci(n - 1) + fibonacci(n - 2); + + } + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + + if (max <= 2){ + return new int[0]; + } + + int[] temp = new int[max]; + + int count = 0; + for (int i = 2; i < max; i++) { + int j; + for ( j = 2; j * j < i; j++) { + if (i % j == 0){ + break; + } + } + + if (j * j >= i){ + temp[count++] = i; + } + } + + return Arrays.copyOf(temp, count); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + if ( max <= 1){ + return new int[0]; + } + + ArrayList array = new ArrayList<>(); + for (int i = 2; i < max; i++) { + int sum = 0; //存储因子和 + for (int j = 1; j < i; j++) { + if (i % j == 0){ + sum += j; + } + } + + if (sum == i){ + array.add(i); + } + } + int[] result = new int[array.size()]; + + for (int i = 0; i < array.size(); i++) { + result[i] = array.get(i); + } + return result; + + + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + + if (array == null || array.length == 0){ + return null; + } + + StringBuilder sb = new StringBuilder(""); + for (int i = 0; i < array.length-1; i++) { + sb.append(array[i]).append(seperator); + } + + sb.append(array[array.length-1]); + + return sb.toString(); + } + + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..a4f2c14606 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,60 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..67cf36067b --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LinkedList.java b/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..eb128c4b24 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,369 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +import java.util.Objects; + +public class LinkedList implements List { + private int size; + + private Node head; + + private Node last; + + + + private static class Node { + T data; + Node pre; + Node next; + + Node(T data) { + this.data = data; + } + + + } + + public LinkedList(){ + this.head = new Node<>(null); + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return this.size == 0; + } + + @Override + public boolean contains(Object o) { + Node node = this.head; + while (node.next != null){ + if (Objects.equals(node.data, o)){ + + return true; + } + node = node.next; + } + return false; + } + + @Override + public Object[] toArray() { + Object[] dataArray = new Object[size]; + Node node = head.next; + for (int i = 0; i < size&& node != null; i++, node = node.next) { + dataArray[i] = node.data; + + } + return dataArray; + } + + @Override + public boolean add(T o) { + if (this.last == null){ + this.last = new Node<>(o); + this.last.pre = this.head; + this.head.next = this.last; + }else { + Node oldLast = this.last; + this.last = new Node<>(o); + this.last.pre = oldLast; + oldLast.next = this.last; + + } + this.size++; + return true; + } + @SuppressWarnings("unchecked") + @Override + public boolean remove(T o) { + + + Node curNode = head.next; + Node preNode; + while (curNode != null){ + preNode = curNode.pre; //指向前一个节点 + + if (Objects.equals(curNode.data, o)){ + removeNode(preNode, curNode); + return true; + } + curNode = curNode.next; + } + + return false; + } + + private void removeNode(Node preNode, Node node) { + + if (this.last == node){ //如果删除的是last节点的情况 + if (preNode == this.head){ //如果只有一个节点的情况 + this.last = null; + }else { + this.last = preNode; + } + }else { + node.next.pre = node.pre; + + } + preNode.next = node.next; + + + + + this.size--; + } + + @Override + @SuppressWarnings("unchecked") + public void clear() { + for (Node x = head; x != null;){ + Node next = x.next; + x.data = null; + x.pre = null; + x.next = null; + x = next; + } + head = last = null; + size = 0; + + } + + @Override + @SuppressWarnings("unchecked") + public T get(int index) { + return (T) getNode(index).data; + } + + + + @Override + public T set(int index, T o) { + Node node = getNode(index); + node.data = o; + return o; + } + + @SuppressWarnings("unchecked") + @Override + public void add(int index, T o) { + ensureIndex(index); + Node newNode = new Node<>(o); + Node curNode = getNode(index); + Node pre = curNode.pre; + + newNode.next = curNode; + newNode.pre = pre; + curNode.pre = newNode; + pre.next = newNode; + + size++; + + } + + @Override + public T remove(int index) { + Node node = getNode(index); + Node pre = node.pre; + if (node == last){ //如果是最后一个节点 + if (pre != head){ //如果是唯一节点 + last = null; + }else { + last = node.pre; + } + } + + pre.next = node.next; + if (node.next != null){ + node.next.pre = pre; + } + size--; + return (T) node.data; + } + + @Override + public int indexOf(T o) { + Node node = head; + int index = 0; + + while (node.next != null){ + node = node.next; + if (Objects.equals(node.data, o)){ + return index; + } + index ++; + } + return index; + } + + @Override + public Iterator iterator() { + return new LinkedListIterator(); + } + + @Override + public void printf() { + Node node = head.next; + while (node != null){ + if (node.next != null) { + System.out.print(node.data + " -> "); + }else { + System.out.print(node.data); + } + node = node.next; + } + System.out.println(); + System.out.println("head = " + head); + System.out.println("last = " + last); + + } + + + private Node getNode(int index) { + ensureIndex(index); + Node node = this.head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + return node; + } + + private void ensureIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if (head.next == null && head.next.next == null){ //如果链表为空或者只有一个元素,不做变换 + return; + } + + last = head.next; + + Node pre = head.next; + Node cur = pre.next; + Node next; + pre.next = null; + while (cur != null){ + next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + } + head.next = pre; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if (isEmpty()){ + return; + } + int halfIndex = size / 2; + + if (halfIndex >= 0){ + head.next = getNode(halfIndex); + } + + size = size - halfIndex; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + ensureIndex(i); + ensureIndex(i + length - 1); + + for (int j = i; j < i + length; j++) { + remove(i); + } + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + private class LinkedListIterator implements Iterator { + private Node node; + + LinkedListIterator(){ + node = head; + } + @Override + public boolean hasNext() { + return node.next != null; + } + + @Override + public T next() { + if (hasNext()){ + node = node.next; + return (T) node.data; + } + return null; + } + } +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/queue/ArrayQueue.java b/group04/120549547/code2017/src/main/java/com/coding/basic/queue/ArrayQueue.java new file mode 100644 index 0000000000..db2047f096 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/queue/ArrayQueue.java @@ -0,0 +1,97 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class ArrayQueue implements Queue { + + private int size; + private Object[] dataArray; + private int front; + private int rear; + + public ArrayQueue(int Capacity) { + this.dataArray = new Object[Capacity]; + this.front = 0; + this.rear = 0; + this.size = 0; + } + + @Override + public boolean add(T t) { + if (offer(t)){ + return true; + }else { + throw new IllegalStateException(); + } + } + + @Override + public boolean offer(T t) { + if (size >= dataArray.length){ + return false; + } + + dataArray[rear++] = t; + + if (rear == dataArray.length){ + rear = 0; + } + + size++; + return true; + } + + @Override + public T remove() { + T value = poll(); + if (value != null){ + return value; + }else { + throw new NoSuchElementException(); + } + } + + @Override + public T poll() { + if (size == 0){ + return null; + } + + + T value = (T) dataArray[front++]; + if (front == dataArray.length){ + front = 0; + } + + size--; + return value; + + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + + @Override + public T peek() { + if (size == 0){ + return null; + }else { + return (T) dataArray[front]; + } + } + + public void printf() { + for (int i = 0; i < size; i++) { + + System.out.print(dataArray[(front + i) % dataArray.length] + " "); + } + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/basic/queue/Queue.java b/group04/120549547/code2017/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..64f86616f4 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,16 @@ +package com.coding.basic.queue; + +public interface Queue { + + boolean add(T t); + + boolean offer(T t); + + T remove(); + + T poll(); + + boolean isEmpty(); + + T peek(); +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/DownloadThread.java b/group04/120549547/code2017/src/main/java/com/coding/download/DownloadThread.java new file mode 100644 index 0000000000..9fc2c860d2 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/DownloadThread.java @@ -0,0 +1,21 @@ +package com.coding.download; + + +import com.coding.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/FileDownloader.java b/group04/120549547/code2017/src/main/java/com/coding/download/FileDownloader.java new file mode 100644 index 0000000000..ec975a9aae --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coding.download; + + +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.api.ConnectionManager; +import com.coding.download.api.DownloadListener; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/FileDownloaderTest.java b/group04/120549547/code2017/src/main/java/com/coding/download/FileDownloaderTest.java new file mode 100644 index 0000000000..84fbc8afa2 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coding.download; + +import com.coding.download.api.ConnectionManager; +import com.coding.download.api.DownloadListener; +import com.coding.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/api/Connection.java b/group04/120549547/code2017/src/main/java/com/coding/download/api/Connection.java new file mode 100644 index 0000000000..65f3dae9c5 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coding.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/api/ConnectionException.java b/group04/120549547/code2017/src/main/java/com/coding/download/api/ConnectionException.java new file mode 100644 index 0000000000..1db8b093ec --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coding.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/api/ConnectionManager.java b/group04/120549547/code2017/src/main/java/com/coding/download/api/ConnectionManager.java new file mode 100644 index 0000000000..1d1a83caf2 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coding.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/api/DownloadListener.java b/group04/120549547/code2017/src/main/java/com/coding/download/api/DownloadListener.java new file mode 100644 index 0000000000..c41045b0e8 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coding.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/impl/ConnectionImpl.java b/group04/120549547/code2017/src/main/java/com/coding/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..0ddbf36e1b --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/impl/ConnectionImpl.java @@ -0,0 +1,28 @@ +package com.coding.download.impl; + +import com.coding.download.api.Connection; + +import java.io.IOException; + + +public class ConnectionImpl implements Connection { + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/download/impl/ConnectionManagerImpl.java b/group04/120549547/code2017/src/main/java/com/coding/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..0f1c658a60 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package com.coding.download.impl; + + +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/litestruts/Configuration.java b/group04/120549547/code2017/src/main/java/com/coding/litestruts/Configuration.java new file mode 100644 index 0000000000..26799a83a4 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/litestruts/Configuration.java @@ -0,0 +1,124 @@ +package com.coding.litestruts; + + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class Configuration { + Map actions = new HashMap<>(); + + + + public Configuration(String path) throws IOException { + + URL pathName = Configuration.class.getClassLoader().getResource(path); + + assert pathName != null; +// InputStream is = this.getClass().getClassLoader().getResourceAsStream(path); 默认则是从ClassPath根下获取,path不能以’/'开头 +// InputStream is = this.getClass().getResourceAsStream("/" + path); // path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取 +// InputStream is = pathName.openStream(); + InputStream is = new FileInputStream(pathName.getPath()); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void parseXML(InputStream is) { + + SAXReader reader = new SAXReader(); + + try { + + Document doc = reader.read(is); + Element root = doc.getRootElement(); + Iterator it = root.elementIterator("action"); + + while (it.hasNext()){ + + Element ActionElement = (Element) it.next(); + + String actionName = ActionElement.attributeValue("name"); + String className = ActionElement.attributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, className); + + Iterator it2 = ActionElement.elementIterator(); + + while (it2.hasNext()){ + + Element resultElement = (Element) it2.next(); + + String resultName = resultElement.attributeValue("name"); + String viewName = resultElement.getTextTrim(); + + ac.addViewResult(resultName, viewName); + } + + this.actions.put(actionName, ac); + } + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + public String getClassName(String actionName) { + ActionConfig ac = actions.get(actionName); + if (ac == null) { + return null; + } + return ac.getClassName(); + } + + public String getResultView(String actionName, String resultName) { + ActionConfig ac = actions.get(actionName); + if (ac == null) { + return null; + } + return ac.getViewName(resultName); + + } + + + private static class ActionConfig{ + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String name, String clzName) { + this.name = name; + this.clzName = clzName; + } + + public String getClassName() { + return clzName; + } + + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + + } + + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/litestruts/LoginAction.java b/group04/120549547/code2017/src/main/java/com/coding/litestruts/LoginAction.java new file mode 100644 index 0000000000..39354518df --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/litestruts/ReflectionUtil.java b/group04/120549547/code2017/src/main/java/com/coding/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..6b761355b1 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/litestruts/ReflectionUtil.java @@ -0,0 +1,85 @@ +package com.coding.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz, "set"); + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for (String name : params.keySet()) { + + String methodName = "set" + name; + + for (Method method : methods) { + + if (method.getName().equalsIgnoreCase(methodName)){ + try { + method.invoke(o,params.get(name)); + + + + + + + } catch (IllegalAccessException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + } + } + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz, "get"); + } + + private static List getMethods(Class clz, String startWithName) { + List methods = new ArrayList<>(); + + for (Method method : clz.getDeclaredMethods()) { + + if (method.getName().startsWith(startWithName)) { + methods.add(method); + } + } + return methods; + } + + public static Map getParamterMap(Object o) { + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); //获得"getXXX"方法 + + for (Method method : methods) { + + String methodName = method.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); //获得属性名 + + try { + params.put(name,method.invoke(o)); //将属性名 和属性值添加进去 + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + return params; + + } +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/litestruts/Struts.java b/group04/120549547/code2017/src/main/java/com/coding/litestruts/Struts.java new file mode 100644 index 0000000000..f446bec31c --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/litestruts/Struts.java @@ -0,0 +1,67 @@ +package com.coding.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + + + +public class Struts { + + + + public static View runAction(String actionName, Map parameters) throws NoSuchMethodException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + try { + Configuration cfg = new Configuration("struts.xml"); + + String clzName = cfg.getClassName(actionName); + if (clzName == null) { + return null; + } + String classPath = Struts.class.getClassLoader().getResource("").getPath(); + + System.out.println("clzName = " + clzName); + System.out.println("classPath = " + classPath); + Class clz = Class.forName(clzName); + + Object o = clz.newInstance(); + ReflectionUtil.setParameters(o, parameters); //将参数注入属性 + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String) m.invoke(o); //执行execute方法 + + Map params = ReflectionUtil.getParamterMap(o); //根据action获取model参数 + + String jsp = cfg.getResultView(actionName, resultName); //通过返回结果去拿视图名 + + View view = new View(); + view.setJsp(jsp); + view.setParameters(params); + return view; + } catch (IOException | InstantiationException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException | IllegalAccessException e) { + e.printStackTrace(); + throw e; + } + } + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/litestruts/StrutsTest.java b/group04/120549547/code2017/src/main/java/com/coding/litestruts/StrutsTest.java new file mode 100644 index 0000000000..041fa4d889 --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/litestruts/StrutsTest.java @@ -0,0 +1,47 @@ +package com.coding.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws IllegalAccessException, InvocationTargetException, IOException, InstantiationException, NoSuchMethodException, ClassNotFoundException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws IllegalAccessException, InvocationTargetException, IOException, InstantiationException, NoSuchMethodException, ClassNotFoundException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + +} diff --git a/group04/120549547/code2017/src/main/java/com/coding/litestruts/View.java b/group04/120549547/code2017/src/main/java/com/coding/litestruts/View.java new file mode 100644 index 0000000000..b6e795aa4a --- /dev/null +++ b/group04/120549547/code2017/src/main/java/com/coding/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group04/120549547/code2017/src/test/java/com/coding/basic/array/ArrayListTest.java b/group04/120549547/code2017/src/test/java/com/coding/basic/array/ArrayListTest.java new file mode 100644 index 0000000000..abd49e98e3 --- /dev/null +++ b/group04/120549547/code2017/src/test/java/com/coding/basic/array/ArrayListTest.java @@ -0,0 +1,122 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class ArrayListTest { + + private ArrayList arrayList; + @Before + public void init() { + arrayList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + arrayList.add(i); + } + } + + @Test + public void size() throws Exception { + Assert.assertEquals(5, arrayList.size()); + arrayList.add(6); + Assert.assertEquals(6, arrayList.size()); + + } + + @Test + public void isEmpty() throws Exception { + arrayList.clear(); + Assert.assertEquals(0, arrayList.size()); + Assert.assertTrue(arrayList.isEmpty()); + } + + @Test + public void contains() throws Exception { + Assert.assertTrue(arrayList.contains(0)); + Assert.assertTrue(arrayList.contains(4)); + Assert.assertFalse(arrayList.contains(5)); + } + + @Test + public void toArray() throws Exception { + Integer[] integers = new Integer[]{0, 1, 2, 3, 4}; + Assert.assertArrayEquals(integers, arrayList.toArray()); + } + + @Test + public void add() throws Exception { + arrayList.add(5); + Assert.assertEquals(5, arrayList.get(arrayList.size() - 1).intValue()); + arrayList.add(0, 6); + arrayList.add(3, 7); + arrayList.add(arrayList.size(), 8); + + Assert.assertEquals(9, arrayList.size()); + Assert.assertEquals(6, arrayList.get(0).intValue()); + Assert.assertEquals(8, arrayList.get(arrayList.size()-1).intValue()); + Assert.assertEquals(7, arrayList.get(3).intValue()); + } + + @Test + public void remove() throws Exception { + arrayList.remove(0); + arrayList.remove(3); + arrayList.add(5); + Assert.assertArrayEquals(arrayList.toArray(), new Integer[]{1,2,3,5}); + + arrayList.remove(new Integer(1)); + arrayList.remove(new Integer(2)); + arrayList.remove(new Integer(5)); + arrayList.add(6); + arrayList.add(7); + + Assert.assertArrayEquals(arrayList.toArray(), new Integer[]{3,6,7}); + + } + + + + @Test + public void get() throws Exception { + Assert.assertEquals(0, arrayList.get(0).intValue()); + Assert.assertEquals(3, arrayList.get(3).intValue()); + arrayList.add(5); + arrayList.remove(0); + Assert.assertEquals(5, arrayList.get(4).intValue()); + } + + @Test + public void set() throws Exception { + arrayList.set(0,100); + arrayList.set(arrayList.size() - 1, 50); + Assert.assertEquals(100, arrayList.get(0).intValue()); + Assert.assertEquals(50, arrayList.get(arrayList.size() - 1).intValue()); + + } + + + + + + @Test + public void indexOf() throws Exception { + Assert.assertEquals(0, arrayList.indexOf(0)); + Assert.assertEquals(1, arrayList.indexOf(1)); + } + + @Test + public void iterator() throws Exception { + Iterator iterator = arrayList.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertEquals(0, iterator.next()); + Assert.assertEquals(1, iterator.next()); + } + +} \ No newline at end of file diff --git a/group04/120549547/code2017/src/test/java/com/coding/basic/array/ArrayUtilTest.java b/group04/120549547/code2017/src/test/java/com/coding/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..f16a7ab5ab --- /dev/null +++ b/group04/120549547/code2017/src/test/java/com/coding/basic/array/ArrayUtilTest.java @@ -0,0 +1,94 @@ +package com.coding.basic.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +/** + * Created by bobi on 2017/4/4. + * at code2017 + */ +public class ArrayUtilTest { + + private static int[] arr; + + @Before + public void setUp() throws Exception { + + arr = new int[10]; + + for (int i = 0; i < arr.length; i++) { + arr[i] = i; + } + + } + + private static void arrPrint(int[] arr){ + for (int i : arr) { + System.out.print(i + " "); + } + System.out.println(); + } + + @Test + public void reverseArray() throws Exception { + ArrayUtil.reverseArray(arr); + for (int i = 0; i < arr.length; i++) { + Assert.assertEquals(9-i, arr[i]); + } + } + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + oldArr = ArrayUtil.removeZero(oldArr); + + Assert.assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, oldArr); + + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7} ; + + int[] a3 = ArrayUtil.merge(a1, a2); + Assert.assertArrayEquals(new int[]{3,4,5,6,7,8}, a3); + } + + @Test + public void grow() throws Exception { + int[] a1 = {3, 5, 7,8}; + a1 = ArrayUtil.grow(a1, 3); + + Assert.assertArrayEquals(new int[]{3,5,7,8,0,0,0}, a1); + } + + @Test + public void fibonacci() throws Exception { + int[] arr = ArrayUtil.fibonacci1(100); + arrPrint(arr); + } + + @Test + public void getPrimes() throws Exception { + int[] a1 = ArrayUtil.getPrimes(100); + arrPrint(a1); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] a1 = ArrayUtil.getPerfectNumbers(10000); + arrPrint(a1); + } + + @Test + public void join() throws Exception { + int[] a1 = {3,4,5,6,7}; + String str = ArrayUtil.join(a1, "-"); + System.out.println("str = " + str); + + } + +} \ No newline at end of file diff --git a/group04/120549547/code2017/src/test/java/com/coding/basic/linklist/LinkedListTest.java b/group04/120549547/code2017/src/test/java/com/coding/basic/linklist/LinkedListTest.java new file mode 100644 index 0000000000..f020e64828 --- /dev/null +++ b/group04/120549547/code2017/src/test/java/com/coding/basic/linklist/LinkedListTest.java @@ -0,0 +1,155 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +/** + * Created by bobi on 2017/3/31. + * at code2017 + */ +public class LinkedListTest { + + + @Before + public void init() { + linkedList = new LinkedList<>(); + for (int i = 0; i < 6; i++) { + linkedList.add(1< linkedList; + + @Test + public void remove() throws Exception { + //测试添加删除 + { + linkedList.printf(); + linkedList.remove(new Integer(1)); + linkedList.remove(new Integer(8)); + linkedList.remove(new Integer(32)); + linkedList.add(50); + linkedList.add(0, 100); + Assert.assertArrayEquals(linkedList.toArray(), new Integer[]{ 100, 2, 4, 16, 50}); + +// + linkedList.remove(new Integer(16)); + linkedList.add(linkedList.size() - 1, 25); + Assert.assertArrayEquals(linkedList.toArray(), new Integer[]{ 100,2, 4, 25, 50}); + } + + } + + + @Test + public void removeFirstHalf() throws Exception { + linkedList.removeFirstHalf(); + linkedList.removeFirstHalf(); + linkedList.printf(); + } + + @Test + public void getElements() throws Exception { + + } + + @Test + public void subtract() throws Exception { + + } + + @Test + public void removeDuplicateValues() throws Exception { + linkedList.removeFirstHalf(); + LinkedList list = new LinkedList(); + list.add(8); + list.add(16); + list.add(32); + Assert.assertArrayEquals(linkedList.toArray(), list.toArray()); + linkedList.removeFirstHalf(); + list.remove(0); + Assert.assertArrayEquals(linkedList.toArray(), list.toArray()); + + } + + @Test + public void removeByLength() throws Exception { + // 测试删除开始节点 + { + linkedList.remove(0, 2); + Assert.assertEquals(linkedList.size(), 4); + for (int i = 0; i < 3; i++) { + Assert.assertEquals(linkedList.get(i).intValue(), 1<<(i+2)); + } + } + + // 测试删除中间节点 + { + init(); + linkedList.remove(1, 2); + Assert.assertEquals(linkedList.size(), 4); + Assert.assertEquals(linkedList.get(0).intValue(), 1); + Assert.assertEquals(linkedList.get(1).intValue(), 8); + Assert.assertEquals(linkedList.get(2).intValue(), 16); + } +// + // 测试删除末尾节点 + { + init(); + linkedList.remove(4, 2); + Assert.assertEquals(linkedList.size(), 4); + Assert.assertEquals(linkedList.get(0).intValue(), 1); + Assert.assertEquals(linkedList.get(1).intValue(), 2); + Assert.assertEquals(linkedList.get(2).intValue(), 4); + Assert.assertEquals(linkedList.get(3).intValue(), 8); + } +// + // 测试删除全部 + { + init(); + linkedList.remove(0, 6); + Assert.assertEquals(linkedList.size(), 0); + } + } + + @Test + public void intersection() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/120549547/code2017/src/test/java/com/coding/basic/queue/ArrayQueueTest.java b/group04/120549547/code2017/src/test/java/com/coding/basic/queue/ArrayQueueTest.java new file mode 100644 index 0000000000..cd0d8abcc8 --- /dev/null +++ b/group04/120549547/code2017/src/test/java/com/coding/basic/queue/ArrayQueueTest.java @@ -0,0 +1,68 @@ +package com.coding.basic.queue; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class ArrayQueueTest { + private ArrayQueue arrayQueue; + + @Before + public void init(){ + arrayQueue = new ArrayQueue<>(6); + for (int i = 0; i < 5; i++) { + arrayQueue.add(i); + } + + } + @Test + public void add() throws Exception { + Assert.assertTrue(arrayQueue.add(5)); + + + Assert.assertEquals(0, arrayQueue.peek().intValue()); + Assert.assertEquals(0, arrayQueue.poll().intValue()); + Assert.assertEquals(1, arrayQueue.poll().intValue()); + + for (int i = 0; i < 4; i++) { + arrayQueue.remove(); + } + Assert.assertTrue(arrayQueue.isEmpty()); + } + + @Test + public void offer() throws Exception { + Assert.assertTrue(arrayQueue.offer(5)); + Assert.assertFalse(arrayQueue.offer(6)); + } + + @Test + public void remove() throws Exception { + arrayQueue.remove(); + arrayQueue.remove(); + arrayQueue.remove(); + arrayQueue.remove(); + + arrayQueue.add(5); + arrayQueue.add(6); + arrayQueue.add(7); + arrayQueue.add(8); + arrayQueue.add(9); + + + Assert.assertEquals(4, arrayQueue.remove().intValue()); + Assert.assertEquals(5, arrayQueue.remove().intValue()); + Assert.assertEquals(6, arrayQueue.remove().intValue()); + Assert.assertEquals(7, arrayQueue.remove().intValue()); + Assert.assertEquals(8, arrayQueue.remove().intValue()); + Assert.assertEquals(9, arrayQueue.remove().intValue()); + } + + +} \ No newline at end of file diff --git a/group04/120549547/code2017/src/test/java/com/coding/litestruts/ConfigurationTest.java b/group04/120549547/code2017/src/test/java/com/coding/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..40a3013741 --- /dev/null +++ b/group04/120549547/code2017/src/test/java/com/coding/litestruts/ConfigurationTest.java @@ -0,0 +1,45 @@ +package com.coding.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class ConfigurationTest { + private Configuration config; + @Before + public void setUp() throws Exception { + config = new Configuration("struts.xml"); + } + + @After + public void tearDown() throws Exception { + + } + + + @Test + public void testGetClassName(){ + String clzName = config.getClassName("login"); + Assert.assertEquals("com.coding.litestruts.LoginAction", clzName); + clzName = config.getClassName("logout"); + Assert.assertEquals("com.coding.litestruts.LogoutAction", clzName); + + } + + @Test + public void testGetResultView(){ + String jsp = config.getResultView("login", "success"); + Assert.assertEquals("jsp/homepage.jsp", jsp); + + jsp = config.getResultView("login", "fail"); + Assert.assertEquals("jsp/showLogin.jsp", jsp); + } + +} \ No newline at end of file diff --git a/group04/120549547/code2017/src/test/java/com/coding/litestruts/ReflectionUtilTest.java b/group04/120549547/code2017/src/test/java/com/coding/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..d66188988b --- /dev/null +++ b/group04/120549547/code2017/src/test/java/com/coding/litestruts/ReflectionUtilTest.java @@ -0,0 +1,124 @@ +package com.coding.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * Created by bobi on 2017/4/1. + * at code2017 + */ +public class ReflectionUtilTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testGetSetterMethod() throws ClassNotFoundException { + String name = "com.coding.litestruts.LoginAction"; + + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + List acctualNames = new ArrayList<>(); + for (Method method : methods) { + acctualNames.add(method.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + + } + + @Test + public void testSetParameters() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException { + String name = "com.coding.litestruts.LoginAction"; + + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap<>(); + params.put("name", "test"); + params.put("password", "1234"); + + ReflectionUtil.setParameters(o, params); + + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + } + + + @Test + public void testGetGetterMethod() throws ClassNotFoundException { + String name = "com.coding.litestruts.LoginAction"; + + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + expectedNames.add("getMessage"); + + List acctualNames = new ArrayList<>(); + + for (Method method : methods) { + acctualNames.add(method.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + + } + + @Test + public void testGetParameters() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException { + String name = "com.coding.litestruts.LoginAction"; + Class clz = Class.forName(name); + + LoginAction o = (LoginAction) clz.newInstance(); + o.setName("test"); + o.setPassword("123456"); + + Map params; + params = ReflectionUtil.getParamterMap(o); + + Assert.assertEquals(3, params.size()); + Assert.assertEquals(null, params.get("message")); + Assert.assertEquals("test", params.get("name")); + Assert.assertEquals("123456", params.get("password")); + + + } + + @Test + public void testDouble(){ + double d = 6.02e23; + long i = (long) d; + System.out.println(i); + } +} \ No newline at end of file diff --git a/group04/120549547/my.txt b/group04/120549547/my.txt deleted file mode 100644 index 3da1ec26e9..0000000000 --- a/group04/120549547/my.txt +++ /dev/null @@ -1 +0,0 @@ -HelloWorld diff --git a/group04/1299310140/src/com/coderising/download/DownloadThread.java b/group04/1299310140/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..a9e0d41dfa --- /dev/null +++ b/group04/1299310140/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,38 @@ +package com.coderising.download; + +import java.io.FileOutputStream; +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + byte[] result; + + public DownloadThread( Connection conn, int startPos, int endPos, byte[] result){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.result = result; + + } + public void run(){ + try { + byte[] download = this.conn.read(this.startPos, this.endPos); + //synchronized(this.result){ + System.arraycopy(download, 0, this.result, this.startPos, download.length); + System.out.println(this.startPos+" "+this.endPos); + //} + FileOutputStream fos = new FileOutputStream("C:\\b.jpg"); + fos.write(this.result); + fos.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/group04/1299310140/src/com/coderising/download/FileDownloader.java b/group04/1299310140/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c094717293 --- /dev/null +++ b/group04/1299310140/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,83 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm = new ConnectionManagerImpl(); + + public FileDownloader(String _url) { + this.url = _url; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection connOne = null; + Connection connTwo = null; + Connection connThree = null; + try { + + connOne = cm.open(this.url); + connTwo = cm.open(this.url); + connThree = cm.open(this.url); + + int length = connOne.getContentLength(); + + byte[] result = new byte[length]; + new DownloadThread(connOne,0,length/3,result).start(); + new DownloadThread(connTwo,length/3+1,length/2,result).start(); + new DownloadThread(connThree,length/2+1,length-1,result).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(connOne != null){ + connOne.close(); + } + if(connTwo != null){ + connTwo.close(); + } + if(connThree != null){ + connThree.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + + public static void main(String[] args){ + new FileDownloader("http://img1.gtimg.com/17/1724/172495/17249563_980x1200_281.jpg").execute(); + } + +} diff --git a/group04/1299310140/src/com/coderising/download/impl/ConnectionImpl.java b/group04/1299310140/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..52884cfd2f --- /dev/null +++ b/group04/1299310140/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,59 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + private URLConnection urlconn; + private InputStream fis; + + public ConnectionImpl(URLConnection urlconn) { + super(); + this.urlconn = urlconn; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException{ + this.fis = this.urlconn.getURL().openStream(); + byte[] buffer = new byte[512]; + int count = 0;//某次read的字节数 + int sum = 0;//read的总字节数 + int length = endPos - startPos + 1;//当前线程需读取的字节数 + byte[] download = new byte[length]; + fis.skip(startPos); + while((count = fis.read(buffer)) != -1){ + if(sum + count >= length){ + System.arraycopy(buffer, 0, download, sum, length - sum); + sum = length; + break; + }else{ + System.arraycopy(buffer, 0, download, sum, count); + sum = sum + count; + } + } + return download; + } + + @Override + public int getContentLength() { + return this.urlconn.getContentLength(); + } + + @Override + public void close() { + if(this.fis != null){ + try { + this.fis.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + +} diff --git a/group04/1299310140/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group04/1299310140/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..4af847dc88 --- /dev/null +++ b/group04/1299310140/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,31 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + try { + URL myurl = new URL("http://img1.gtimg.com/17/1724/172495/17249563_980x1200_281.jpg"); + URLConnection urlconn = myurl.openConnection(); + ConnectionImpl conn = new ConnectionImpl(urlconn); + return conn; + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + +} diff --git a/group04/1299310140/src/com/coderising/jvm/loader/ClassFileLoader.java b/group04/1299310140/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..4b85fa7ed1 --- /dev/null +++ b/group04/1299310140/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,56 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws Exception{ + //com.coderising.jvm.test.EmployeeV1 + //"com\\coderising\\jvm\\test\\EmployeeV1" + String clzFileName = this.getClassPath() + "\\" + className.replace(".", "\\") + ".class"; + //FileInputStream BufferedInputStream ByteArrayOutputStream + File file = new File(clzFileName); + + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + byte[] buffer = new byte[1024]; + + int length = -1; + + while((length = bis.read(buffer)) != -1){ + bos.write(buffer,0,length); + } + + byte[] result = bos.toByteArray(); + bis.close(); + bos.close(); + + return result; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + String result = ""; + for(int i = 0;i < clzPaths.size();i++){ + result = result + clzPaths.get(i); + if(i == clzPaths.size() - 1){ + break; + } + result = result + ";"; + } + return result; + } + +} diff --git a/group04/1299310140/src/com/coding/basic/LinkedList.java b/group04/1299310140/src/com/coding/basic/LinkedList.java index 4636bbd279..3e908613db 100644 --- a/group04/1299310140/src/com/coding/basic/LinkedList.java +++ b/group04/1299310140/src/com/coding/basic/LinkedList.java @@ -208,4 +208,285 @@ public String toString(){ return result; } } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(this.size <= 1){ + return; + } + Node before = null; + Node pres = this.head; + Node after = pres.next; + while(after != null){ + pres.next = before; + before = pres; + pres = after; + after = after.next; + } + //此时pres指向最后一个节点 + pres.next = before; + this.head = pres; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + if(this.size <= 1){ + return; + } + Node pres = this.head; + Node temp = pres; + for(int i = 0;i < this.size / 2;i++){ + temp = pres; + pres = pres.next; + temp.data = null; + temp.next = null; + } + this.head = pres; + this.size = this.size - this.size / 2; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param index + * @param length + */ + public void remove(int index, int length){//若length太大,size不够,则取到末尾 + if(index < 0 || index >= this.size || length <= 0){//index小于0or大于等于size,length小于等于0,参数错误 + return; + } + if(this.size <= 0){ + return; + } + if(index == 0){ + //此时index=0&&length>0&&size>0 + Node temp = this.head; + for(int i = 0;i < length;i++){ + temp = temp.next; + if(temp == null){ + break; + } + } + this.head = temp; + if(temp == null){ + this.size = 0; + }else{ + this.size = this.size - length; + } + }else{ + //此时00&&size>0 + Node start = this.head; + for(int j = 0;j < index-1;j++){ + start = start.next; + }//start指向index-1 + + Node end = start; + for(int l = 0;l <= length;l++){ + end = end.next; + if(end == null){ + break; + } + }//end指向index+length + start.next = end; + if(end == null){ + this.size = index; + }else{ + this.size = this.size - length; + } + } + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){//若listB中的index不在0~this.size-1之中,则返回-1 + if(this.size <= 0 || list == null || list.size <= 0){ + return null; + } + + int[] result = new int[list.size()]; + Node presIndex = list.head; + int index = (int)presIndex.data; + for(int i = 0;i < list.size();i++){ + if(index < 0 || index >= this.size){ + result[i] = -1; + }else{//index:0~this.size-1 + result[i] = (int)this.get(index); + } + presIndex = presIndex.next; + if(presIndex != null){ + index = (int)presIndex.data; + } + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * @param list + */ + public void subtract(LinkedList list){//当前链表以及参数列表均递增有序,可有重复值 + if(this.size == 0 || list == null || list.size() == 0){ + return; + } + + //头节点的删除比较特殊,先不予考虑 + Node thisPres = this.head.next;//指向被删除节点 + Node thisPresBefore = this.head;//指向被删除节点的前一个节点 + Node listPres = list.head; + while(thisPres != null && listPres != null){ + if((int)thisPres.data > (int)listPres.data){ + listPres = listPres.next; + }else if((int)thisPres.data < (int)listPres.data){ + thisPresBefore = thisPresBefore.next; + thisPres = thisPres.next; + }else{//(int)thisPres.data == (int)listPres.data + thisPresBefore.next = thisPres.next; + thisPres = thisPres.next; + this.size--; + } + } + + //最后考虑头节点的删除情况 + Node first = this.head; + Node listPresTwo = list.head; + while((int)first.data > (int)listPresTwo.data){ + listPresTwo = listPresTwo.next; + } + if((int)first.data == (int)listPresTwo.data){//删除头节点 + this.head = this.head.next; + this.size--; + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(this.size <= 1){ + return; + } + + Node start = this.head; + Node end = start.next; + while(end != null){ + if((int)start.data != (int)end.data){ + start = end; + end = end.next; + }else{//start.data == end.data + while((int)start.data == (int)end.data){ + end = end.next; + if(end == null){ + break; + } + } + start.next = end; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(this.size == 0 || max - min < 2 || (int)this.head.data >= max){ + return; + } + + //this.size >= 1 && max - min >= 2 && this.head.data < max + int thisHeadData = (int)this.head.data; + if(thisHeadData > min && thisHeadData < max){ + //this.size >= 1 && max - min >= 2 && + //min < this.head.data < max + //找到新的头节点即可,this.size减小 + Node notSmallToMax = this.head.next; + int sizeDec = 1; + while(notSmallToMax != null){ + if((int)notSmallToMax.data >= max){ + break; + } + notSmallToMax = notSmallToMax.next; + sizeDec++; + } + this.head = notSmallToMax; + this.size = this.size - sizeDec; + }else{ + //this.size >= 1 && max - min >= 2 && + //this.head.data <= min + Node startBefore = this.head;//第一个>min节点的前一个节点 + Node start = startBefore.next;//第一个>min的节点 + while(start != null){ + if((int)start.data > min){ + break; + } + startBefore = start; + start = start.next; + } + if(start == null || (int)start.data >= max){ + //链表中不存在满足删除条件的元素 + return; + } + + //至少有一个元素需要被删除 + int sizeDec = 1; + Node end = start;//最后一个= max){ + break; + } + end = endAfter; + endAfter = endAfter.next; + sizeDec++; + } + startBefore.next = endAfter; + this.size = this.size - sizeDec; + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + if(list == null || list.size() == 0){ + return new LinkedList(); + } + if(this.size == 0){ + return new LinkedList(); + } + LinkedList result = new LinkedList(); + Node thisPres = this.head; + Node listPres = list.head; + while(thisPres != null && listPres != null){ + if((int)thisPres.data < (int)listPres.data){ + thisPres = thisPres.next; + }else if((int)thisPres.data > (int)listPres.data){ + listPres = listPres.next; + }else{ + //(int)thisPres.data == (int)listPres.data + result.add(thisPres.data); + thisPres = thisPres.next; + listPres = listPres.next; + } + } + return result; + } } diff --git a/group04/1299310140/src/com/coding/basic/linklist/LRUPageFrame.java b/group04/1299310140/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..8a39813de0 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,136 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node(int pageNum) { + this.pageNum = pageNum; + } + + } + + private int capacity; + private int size = 0; + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + removeElement(pageNum); + addFirst(pageNum); + if(this.size > this.capacity){ + removeLast(); + } + } + + //删除链表的尾节点 + private void removeLast(){ + if(this.size == 0){ + return; + } + if(this.size == 1){ + this.first = null; + this.last = null; + this.size--; + } + Node curr = this.last; + this.last = curr.prev; + this.last.next = null; + curr.prev = null; + this.size--; + } + + //根据参数删除双向链表中的某个节点 + private void removeElement(int pageNum){ + if(this.size == 0){ + return; + } + Node curr = this.first; + while(curr.pageNum != pageNum){ + if(curr.next == null){ + break; + } + curr = curr.next; + } + //此时curr指向被删除节点or链表最后一个节点 + if(curr.pageNum == pageNum){ + if(this.first == this.last){//size为1,且该节点需要被删除 + this.first = null; + this.last = null; + this.size--; + return; + } + if(curr == this.first){//删除头节点 + this.first = curr.next; + this.first.prev = null; + curr.next = null; + this.size--; + return; + } + if(curr == this.last){//删除尾节点 + this.last = curr.prev; + this.last.next = null; + curr.prev = null; + this.size--; + return; + } + + //删除中间节点 + //此时size至少为3 + curr.next.prev = curr.prev; + curr.prev.next = curr.next; + curr.prev = null; + curr.next = null; + this.size--; + } + } + + //向双向链表的头部添加节点 + private void addFirst(int pageNum){ + Node curr = new Node(pageNum); + if(this.size == 0){ + this.first = curr; + this.last = curr; + this.size++; + }else{ + curr.next = this.first; + this.first.prev = curr; + this.first = curr; + this.size++; + } + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group04/1299310140/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group04/1299310140/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..3e88027af5 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,73 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(7); + frame.access(0); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + + LRUPageFrame frameFive = new LRUPageFrame(5); + frameFive.access(7);//7 + frameFive.access(7);//7 + frameFive.access(0);//0 7 + frameFive.access(7);//7 0 + frameFive.access(0);//0 7 + frameFive.access(1);//1 0 7 + Assert.assertEquals("1,0,7", frameFive.toString()); + frameFive.access(2);//2 1 0 7 + Assert.assertEquals("2,1,0,7", frameFive.toString()); + frameFive.access(0);//0 2 1 7 + Assert.assertEquals("0,2,1,7", frameFive.toString()); + frameFive.access(0);//0 2 1 7 + Assert.assertEquals("0,2,1,7", frameFive.toString()); + frameFive.access(3);//3 0 2 1 7 + Assert.assertEquals("3,0,2,1,7", frameFive.toString()); + frameFive.access(0);//0 3 2 1 7 + Assert.assertEquals("0,3,2,1,7", frameFive.toString()); + frameFive.access(4);//4 0 3 2 1 + Assert.assertEquals("4,0,3,2,1", frameFive.toString()); + } + +// @Test +// public void testAddFirst(){ +// LRUPageFrame frame = new LRUPageFrame(3); +// frame.addFirst(1); +// frame.addFirst(2); +// Assert.assertEquals("2,1", frame.toString()); +// frame.addFirst(3); +// Assert.assertEquals("3,2,1", frame.toString()); +// frame.addFirst(4); +// Assert.assertEquals("4,3,2,1", frame.toString()); +// frame.removeElement(3); +// Assert.assertEquals("4,2,1", frame.toString()); +// frame.removeElement(1); +// Assert.assertEquals("4,2", frame.toString()); +// frame.removeElement(4); +// Assert.assertEquals("2", frame.toString()); +// frame.removeElement(2); +// Assert.assertEquals("", frame.toString()); +// } + +} diff --git a/group04/1796244932/learn01/1.png b/group04/1796244932/learn01/1.png new file mode 100644 index 0000000000..a25be7d057 Binary files /dev/null and b/group04/1796244932/learn01/1.png differ diff --git a/group04/1796244932/learn01/pom.xml b/group04/1796244932/learn01/pom.xml index 06c26f00c8..f62b63ec4b 100644 --- a/group04/1796244932/learn01/pom.xml +++ b/group04/1796244932/learn01/pom.xml @@ -21,17 +21,29 @@ dom4j 1.6 + + + + + org.jdom + jdom + 2.0.2 + + + junit junit - 4.11 - test + 4.12 - - org.junit.jupiter - junit-jupiter-api - RELEASE - + + + + io.netty + netty + 4.0.0.Alpha8 + + diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java index 60254997aa..a425f54e81 100644 --- a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java @@ -1,6 +1,13 @@ package com.dudy.learn01.base; +import java.util.LinkedList; + +/** + * 单链表: + * 因为没有尾节点 + * 存放时 add 1 2 3 4 实际是 4 3 2 1 + */ public class MyLinkedList implements MyList { private int size = 0; @@ -24,6 +31,7 @@ public void add(int index, Object o) { } private Node getCurrentNode(int index) {// 获取当前节点 + checkRange(index); Node current = head; for(int i = 0; i< size-index -1; i++){ current = current.next; @@ -56,7 +64,7 @@ public Object remove(int index) { return node.data; } - public int size() { + public int size() { return size; } @@ -108,6 +116,7 @@ public Object next() { } } + private static class Node { Object data; Node next; @@ -118,15 +127,107 @@ public Node(Object data) { } } - - private void displayLink() {// 自己调试使用 - Node current = head; - while(current != null){ - System.out.print(current.data); - current = current.next; + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + if(head == null || head.next == null){ + return ; + } + + Node current = head; // 当前节点 我的头节点是有数据的 + + while (current.next != null){ + Node p = current.next; // 当前节点的下一个 + current.next = p.next; // 当前节点的next -> current.next.next (p.next) + p.next = head; // current.next(p) -> head.next (插入到 head 和 第一个数据之间) + head = p; } - System.out.println(""); } - + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + int base = size; + Node currentNode = getCurrentNode(base / 2); + currentNode = null; + size = size - base/2; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + Node preNode = getCurrentNode(size - i -1); + Node nextNode = getCurrentNode(size - i - length-1); + nextNode.next = preNode; + size = size -length; + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param src + */ + public Object[] getElements(int[] src){ + Object des[] = new Object[src.length]; + + for (int i = 0; i < src.length; i++){ + des[i] = getCurrentNode(size - 1 - i).data; + } + + return des; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } } \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java new file mode 100644 index 0000000000..84e9bd5d2c --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java @@ -0,0 +1,40 @@ +package com.dudy.learn01.designPattern.singleton; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Created by dudy on 2017/3/6. + */ +public enum EnumSingleton { + + SINGLETON; + private EnumSingleton(){} + + + public static void main(String[] args) { + + ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 6000 * 10, TimeUnit.MILLISECONDS, + new ArrayBlockingQueue(5)); + + + for (int i = 0; i< 16; i++){ + executor.execute(new EnumSingletonTest()); + } + + executor.shutdown(); + + + } +} + + +class EnumSingletonTest implements Runnable{ + + @Override + public void run() { + EnumSingleton singleton = EnumSingleton.SINGLETON; + System.out.println(singleton.hashCode()); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java new file mode 100644 index 0000000000..8de831c354 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java @@ -0,0 +1,66 @@ +package com.dudy.learn01.designPattern.singleton; + +import java.io.Serializable; +import java.util.concurrent.*; + +/** + * Created by dudy on 2017/3/6. + * 双检索 方式 + * jdk1.5 以后 其实是线程安全的。 + * 序列化会 破坏 单利 + * + */ +public class SingletonDemo1 implements Serializable{ + + private static volatile SingletonDemo1 singleton = null; // 加 volatile 是为了 可见性,另一个就是 避免重排序 + + private SingletonDemo1(){} + + public static SingletonDemo1 getIntance(){ + + if (singleton == null){// 第一个避免 在 synchronized 中 一直排队 + synchronized (SingletonDemo1.class){ + + if (singleton == null){// 如果对象为空,才被创建 + singleton = new SingletonDemo1(); + } + + } + } + + return singleton; + } + + + /** + * 解决 反序列化的问题 + * @return + */ + private Object readResolve() { + return singleton; + } + + public static void main(String[] args) { + + ExecutorService threadPool = Executors.newFixedThreadPool(10); + + for (int i= 0 ;i < 5; i++){ + threadPool.execute(new TestRunable()); + } + + threadPool.shutdown(); + + //new ThreadPoolExecutor(10,20,1000*2,new BlockingQueue(),) + + + } + +} + + class TestRunable implements Runnable{ + + public void run() { + SingletonDemo1 intance = SingletonDemo1.getIntance(); + System.out.println(intance.hashCode()); + } + } diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java new file mode 100644 index 0000000000..d8b484cd15 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java @@ -0,0 +1,63 @@ +package com.dudy.learn01.designPattern.singleton; + +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Created by dudy on 2017/3/6. + * + * 静态内部类方式 实现 + * + * 这种方式同样利用了classloder的机制来保证初始化instance时只有一个线程, + * 它跟饿汉式不同的是(很细微的差别):饿汉式是只要Singleton类被装载了, + * 那么instance就会被实例化(没有达到lazy loading效果),而这种方式是Singleton类被装载了, + * instance不一定被初始化。因为SingletonHolder类没有被主动使用, + * 只有显示通过调用getInstance方法时,才会显示装载SingletonHolder类, + * 从而实例化instance。想象一下,如果实例化instance很消耗资源,我想让他延迟加载, + * 另外一方面,我不希望在Singleton类加载时就实例化, + * 因为我不能确保Singleton类还可能在其他的地方被主动使用从而被加载, + * 那么这个时候实例化instance显然是不合适的。这个时候,这种方式相比饿汉式更加合理 + * + */ +public class StaticClassInnerSingleton { + + // 构造器 私有化 + private StaticClassInnerSingleton(){} + + private static class SingletonHolder{ + private static final StaticClassInnerSingleton INSTANCE = new StaticClassInnerSingleton(); + } + + + public static StaticClassInnerSingleton getInstance(){ + return SingletonHolder.INSTANCE; + } + + + public static void main(String[] args) { + + + ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 10, + 6000 * 10, TimeUnit.MILLISECONDS, + new LinkedBlockingDeque()); + + for (int i= 0; i<20; i++){ + pool.execute(new StaicSingletonTest()); + //System.out.println(StaticClassInnerSingleton.getInstance().hashCode()); + } + + pool.shutdown(); + } + + + +} + +class StaicSingletonTest implements Runnable{ + + public void run() { + StaticClassInnerSingleton intance = StaticClassInnerSingleton.getInstance(); + System.out.println(intance.hashCode()); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/DownloadThread.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/DownloadThread.java new file mode 100644 index 0000000000..97ca01e13b --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/DownloadThread.java @@ -0,0 +1,59 @@ +package com.dudy.learn01.download; + +import com.dudy.learn01.download.api.Connection; + +import java.io.*; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + private Connection conn; + private int startPos; + private int endPos; + private RandomAccessFile raf; + //private CyclicBarrier cb; + private CountDownLatch downLatch; + + + public DownloadThread(Connection conn, int startPos, int endPos, + /*CyclicBarrier cb*/ + CountDownLatch downLatch){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; +// this.cb = cb; + this.downLatch = downLatch; + } + public void run(){ + try { + byte[] read = conn.read(startPos, endPos); + + System.out.println("read length: -> "+read.length); + //这里要注意新创建一个RandomAccessFile对象,而不能重复使用download方法中创建的 + raf = new RandomAccessFile(new File("/Users/dudy/Desktop/1.png"), "rw"); + //将写文件的指针指向下载的起始点 + raf.seek(startPos); + raf.write(read, 0, read.length); + + downLatch.countDown(); +// cb.await(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (raf != null){ + raf.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + if (conn != null){ + conn.close(); + } + + } + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/FileDownloader.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/FileDownloader.java new file mode 100644 index 0000000000..eee5825b84 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/FileDownloader.java @@ -0,0 +1,93 @@ +package com.dudy.learn01.download; + +import com.dudy.learn01.download.api.Connection; +import com.dudy.learn01.download.api.ConnectionManager; +import com.dudy.learn01.download.api.DownloadListener; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; + +public class FileDownloader { + + private static final int THREAD_NUM = 3; + + private String url; + + private DownloadListener listener; + + private ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() throws IOException { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + //CyclicBarrier cb = new CyclicBarrier(THREAD_NUM); + CountDownLatch downLatch = new CountDownLatch(THREAD_NUM); + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + for (int i = 0;i < THREAD_NUM; i++){ + + int start=i*length/THREAD_NUM; + int end = (i+1)*length/THREAD_NUM-1; + if(i==THREAD_NUM-1) + { + end =length; + } + + new DownloadThread(cm.open(url),start,end,downLatch).start(); + } + + //cb.await(); + downLatch.await(); + getListener().notifyFinished(); + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/Connection.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/Connection.java new file mode 100644 index 0000000000..513c0004e9 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.dudy.learn01.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/ConnectionException.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/ConnectionException.java new file mode 100644 index 0000000000..71af9bf06d --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.dudy.learn01.download.api; + +public class ConnectionException extends Exception { + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/ConnectionManager.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/ConnectionManager.java new file mode 100644 index 0000000000..5f4777f6e0 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/ConnectionManager.java @@ -0,0 +1,13 @@ +package com.dudy.learn01.download.api; + +import java.io.IOException; +import java.net.MalformedURLException; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException, IOException; +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/DownloadListener.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/DownloadListener.java new file mode 100644 index 0000000000..fa3b5bead0 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.dudy.learn01.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/impl/ConnectionImpl.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..5eb6b45d41 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/impl/ConnectionImpl.java @@ -0,0 +1,89 @@ +package com.dudy.learn01.download.impl; + +import com.dudy.learn01.download.api.Connection; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ConnectionImpl implements Connection { + + + private HttpURLConnection connection; + + public ConnectionImpl(String url) { + try { + this.connection = (HttpURLConnection) new URL(url).openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream in = connection.getInputStream(); + byte buffer[] = new byte[endPos-startPos+1]; + byte result[] = new byte[endPos-startPos+1]; + int count = 0; // 记录已经读取的数据 + int length = -1 ; + + while ((length = in.read(buffer)) > 0){ + System.arraycopy(buffer,0,result,count,length); + count += length; + } + return result; + } + + @Override + public int getContentLength() { + return connection.getContentLength(); + } + + @Override + public void close() { + if (connection != null){ + connection.disconnect(); + } + } + + public static void main(String[] args) throws Exception{ + //String PATH = "http://demo2.yun.myuclass.com/upload/demo2.yun.myuclass.com/winshare/pagelogo/250617391.png"; + String PATH = "http://www.lgstatic.com/www/static/mycenter/modules/common/img/tou_42952f6.png"; + + URL url = new URL(PATH); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + //conn.setConnectTimeout(5000); + //conn.setRequestMethod("GET"); + //设置头部的参数,表示请求服务器资源的某一部分 + //conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + //设置了上面的头信息后,响应码为206代表请求资源成功,而不再是200 + int code = conn.getResponseCode(); + System.out.println(conn.getContentLength()); + if(code == 200){ + + InputStream is = conn.getInputStream(); + int hasRead = 0; + byte[] buf = new byte[conn.getContentLength()]; + System.out.println(buf.length); + //这里要注意新创建一个RandomAccessFile对象,而不能重复使用download方法中创建的 + RandomAccessFile raf = new RandomAccessFile(new File("/Users/dudy/Desktop/1.png"), "rw"); + //将写文件的指针指向下载的起始点 + raf.seek(0); + + while((hasRead = is.read(buf,0,conn.getContentLength())) > 0) { + System.out.println("hasRead = " + hasRead); + raf.write(buf, 0, hasRead); + } + is.close(); + raf.close(); + conn.disconnect(); + } + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/impl/ConnectionManagerImpl.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..809f98d91b --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,25 @@ +package com.dudy.learn01.download.impl; + +import com.dudy.learn01.download.api.Connection; +import com.dudy.learn01.download.api.ConnectionException; +import com.dudy.learn01.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + + private Connection connection = null; + + @Override + public Connection open(String url) throws ConnectionException, IOException { + + connection = new ConnectionImpl(url); + + return connection; + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/juc/ThreadLocalTest.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/juc/ThreadLocalTest.java new file mode 100644 index 0000000000..e4239ae521 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/juc/ThreadLocalTest.java @@ -0,0 +1,64 @@ +package com.dudy.learn01.juc; + +/** + * Created by dudy on 2017/3/9. + * + * 4. ThreadLocal 这个类实现原理和用途,在哪里用到了 + * 每个ThreadLocal可以放一个线程级别的变量,但是它本身可以被多个线程共享变量,而且又可以达到线程安全的目的,且绝对线程安全 + * spring的事物管理Session, 连接池管理 Connection + * https://my.oschina.net/huangyong/blog/159725 + * 数据库事物的前提是: 必须是同一个连接 + */ +public class ThreadLocalTest { + + static class Resource{ + public static final ThreadLocal RESOURCE1 = new ThreadLocal(); + public static final ThreadLocal RESOURCE2 = new ThreadLocal(); + } + + static class A { + public void setOne(String str){ + Resource.RESOURCE1.set(str); + } + + public void setTwo(String str){ + Resource.RESOURCE2.set(str); + } + } + + static class B { + public void display(){ + System.out.println(Resource.RESOURCE1.get() + +":" + Resource.RESOURCE2.get()); + } + } + + public static void main(String[] args) { + + final A a = new A(); + final B b = new B(); + + for (int i = 0; i< 5 ;i++){ + + final String resource1 = "Thread_" + i; + final String resource2 = "value " + i; + + new Thread(new Runnable() { + @Override + public void run() { + try { + a.setOne(resource1); + a.setTwo(resource2); + b.display(); + }finally { + Resource.RESOURCE2.remove(); + Resource.RESOURCE1.remove(); + } + } + }).start(); + } + + } + + +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/Struts.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/Struts.java index bbe8c108ca..c08ae7ae49 100644 --- a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/Struts.java +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/Struts.java @@ -46,6 +46,7 @@ public static View runAction(String actionName, Map parameters) { for (Map.Entry entry : parameters.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); // 这里 只能传递object吧 + //actionClass.gett Method method = actionClass.getDeclaredMethod(methodNameconversion(entry.getKey()), String.class); method.setAccessible(true); method.invoke(base,entry.getValue()); diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/Configuration.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/Configuration.java new file mode 100644 index 0000000000..fb2fb74fad --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/Configuration.java @@ -0,0 +1,113 @@ +package com.dudy.learn01.litestruts.format; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +public class Configuration { + + Map actions = new HashMap<>(); + + public Configuration(String fileName) { + + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is){ + + SAXBuilder builder = new SAXBuilder(); + + try { + + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for(Element actionElement : root.getChildren("action")){ + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for(Element resultElement : actionElement.getChildren("result")){ + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + + } + + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig{ + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } + +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ConfigurationException.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ConfigurationException.java new file mode 100644 index 0000000000..a9d3048491 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.dudy.learn01.litestruts.format; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ConfigurationTest.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ConfigurationTest.java new file mode 100644 index 0000000000..fc553fc0fc --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ConfigurationTest.java @@ -0,0 +1,50 @@ +package com.dudy.learn01.litestruts.format; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ConfigurationTest { + + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView(){ + String jsp = cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } + +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/LoginAction.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/LoginAction.java new file mode 100644 index 0000000000..3b49e8dec3 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/LoginAction.java @@ -0,0 +1,39 @@ +package com.dudy.learn01.litestruts.format; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ReflectionUtil.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ReflectionUtil.java new file mode 100644 index 0000000000..a85157ace5 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ReflectionUtil.java @@ -0,0 +1,123 @@ +package com.dudy.learn01.litestruts.format; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for(String name : params.keySet() ){ + + String methodName = "set" + name; + + for(Method m: methods){ + + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + private static List getMethods(Class clz, String startWithName){ + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith(startWithName)){ + + methods.add(m); + + } + + } + + return methods; + } + + public static Map getParamterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for(Method m : methods){ + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + + return params; + } + + ////////////////////////Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("get")){ + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("set")){ + + methods.add(m); + + } + + } + + return methods; + + } + + + + +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ReflectionUtilTest.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ReflectionUtilTest.java new file mode 100644 index 0000000000..a2d4ab945a --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/ReflectionUtilTest.java @@ -0,0 +1,113 @@ +package com.dudy.learn01.litestruts.format; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception{ + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + ReflectionUtil.setParameters(o,params); + + + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + @Test + public void testGetGetterMethod() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParamters() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + + + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/Struts.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/Struts.java new file mode 100644 index 0000000000..3615fae5f2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/Struts.java @@ -0,0 +1,68 @@ +package com.dudy.learn01.litestruts.format; + +import java.lang.reflect.Method; +import java.util.Map; + + + +public class Struts { + + private final static Configuration cfg = new Configuration("struts.xml"); + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + + String clzName = cfg.getClassName(actionName); + + if(clzName == null){ + return null; + } + + try { + + Class clz = Class.forName(clzName); + Object action = clz.newInstance(); + + ReflectionUtil.setParameters(action, parameters); + + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String)m.invoke(action); + + Map params = ReflectionUtil.getParamterMap(action); + String resultView = cfg.getResultView(actionName, resultName); + View view = new View(); + view.setParameters(params); + view.setJsp(resultView); + return view; + + + + } catch (Exception e) { + + e.printStackTrace(); + } + return null; + } + +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/StrutsTest.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/StrutsTest.java new file mode 100644 index 0000000000..0053461c01 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/StrutsTest.java @@ -0,0 +1,43 @@ +package com.dudy.learn01.litestruts.format; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/View.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/View.java new file mode 100644 index 0000000000..07278f2519 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/litestruts/format/View.java @@ -0,0 +1,23 @@ +package com.dudy.learn01.litestruts.format; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java new file mode 100644 index 0000000000..2763af6d55 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java @@ -0,0 +1,104 @@ +package com.dudy.learn01.utils; + +import java.util.Arrays; + +/** + * Created by dudy on 2017/3/6. + * 练习数组的各种排序 + * 参考:http://wiki.jikexueyuan.com/project/java-special-topic/sort.html + * http://www.cnblogs.com/liuling/p/2013-7-24-01.html + * + * 内排序有可以分为以下几类: + +   (1)、插入排序:直接插入排序、二分法插入排序、希尔排序。 + +   (2)、选择排序:简单选择排序、堆排序。 + +   (3)、交换排序:冒泡排序、快速排序。 + +   (4)、归并排序 + +   (5)、基数排序 + * + */ +public class ArraySortDemo { + + + /** + * 二分法查找 插入 + * 和 直接插入排序不同的是: 查找 要插入的位置的方式不同 + * 二分法前提是有序的** + */ + public static void dichotomySort(int src[]){ + + for (int i = 0; i< src.length ; i++){ + int temp = src[i]; + int right = i - 1; + int mid = 0; + int left = 0; + while (left <= right){ + mid = (left + right)/2; + if (temp > src[mid]){ + left = mid + 1; + } else { + right = mid - 1; + } + } + + for (int j = i-1;j>=left ; j--){ + src[j+1] = src[j]; + } + + System.out.println("left = " + left +" ,mid = " + mid + " ,right = " + right); + src[left] = temp; + + } + } + + + + + /** + * 直接插入排序 + * 思想:假定前边是有序地部分, 后边无序的插入到前边部分 + * 可以转变思想: 从后往前遍历, 将有序部分大于当前的值 往后移 + * @param src + */ + public static void directInsertSort(int[] src){ + + for (int i = 1;i < src.length ; i++){ + // 待插入的元素 + int temp = src[i]; + int j; + for ( j = i -1; j >= 0; j--){ + // 大于 temp的往后移动 + if (src[j] > temp){ + src[j+1] = src[j]; + } else { + break; + } + }// 此时遍历完 j+1 为要插入的位置 + src[j+1] = temp; + } + + } + + + + public static void main(String[] args) { + int a[] = new int[]{46,89,14,44,90,32,25,67,23}; + // 14,23,25,32,44,46,67,89,90 + //Arrays.sort(a); + + + //directInsertSort(a); + + dichotomySort(a); + + for (int i = 0; i< a.length ; i++){ + System.out.print(a[i] + ","); + } + + } + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java index c6ebb096ec..cce7a1c163 100644 --- a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java @@ -2,10 +2,69 @@ import java.util.LinkedList; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class MyLinkedListTest { + MyLinkedList list = new MyLinkedList(); + + @Before + public void init(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void after(){ + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + } + + @Test + public void reverse() throws Exception { + list.reverse(); + } + + @Test + public void removeFirstHalf() throws Exception { + list.removeFirstHalf(); + } + + @Test + public void remove() throws Exception { + list.remove(0,2); + } + + @Test + public void getElements() throws Exception { + + } + + @Test + public void subtract() throws Exception { + + } + + @Test + public void removeDuplicateValues() throws Exception { + + } + + @Test + public void removeRange() throws Exception { + + } + + @Test + public void intersection() throws Exception { + + } @Test diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/download/FileDownloaderTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/download/FileDownloaderTest.java new file mode 100644 index 0000000000..fc427e2171 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/download/FileDownloaderTest.java @@ -0,0 +1,53 @@ +package com.dudy.learn01.download; + +import com.dudy.learn01.download.api.ConnectionManager; +import com.dudy.learn01.download.api.DownloadListener; +import com.dudy.learn01.download.impl.ConnectionManagerImpl; +import org.junit.Test; + + +import java.io.IOException; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + + + + @Test + public void testDownload() throws IOException { + + //String url = "http://www.lgstatic.com/www/static/mycenter/modules/common/img/tou_42952f6.png"; + String url = "http://img.lanrentuku.com/img/allimg/1606/14665573271238.jpg"; + FileDownloader downloader = new FileDownloader(url); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} \ No newline at end of file diff --git a/group04/349184132/Study/.classpath b/group04/349184132/Study/.classpath deleted file mode 100644 index 2a9fa78d5e..0000000000 --- a/group04/349184132/Study/.classpath +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/group04/349184132/Study/.settings/org.eclipse.jdt.core.prefs b/group04/349184132/Study/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index a698e59674..0000000000 --- a/group04/349184132/Study/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,12 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group04/349184132/Study/bin/com/second/struts.xml b/group04/349184132/Study/bin/com/second/struts.xml deleted file mode 100644 index 554dbbe227..0000000000 --- a/group04/349184132/Study/bin/com/second/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group04/349184132/Study/bin/com/test/student2.xml b/group04/349184132/Study/bin/com/test/student2.xml deleted file mode 100644 index e21862ec75..0000000000 --- a/group04/349184132/Study/bin/com/test/student2.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - hello text - world text - diff --git a/group04/349184132/Study/bin/com/test/students.xml b/group04/349184132/Study/bin/com/test/students.xml deleted file mode 100644 index 0a503c9f6c..0000000000 --- a/group04/349184132/Study/bin/com/test/students.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - hello Text1 - hello Text2 - hello Text3 - world text1 - world text2 - world text3 - \ No newline at end of file diff --git a/group04/349184132/Study/src/com/coderising/download/DownloadThread.java b/group04/349184132/Study/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..2b806a7f44 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,49 @@ +package com.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread { + Connection conn; + int startPos; + int endPos; + int threadId = 0; + CyclicBarrier barrier; + + public DownloadThread(CyclicBarrier barrier, Connection conn, int threadId, + int startPos, int endPos) { + this.barrier = barrier; + this.conn = conn; + this.threadId = threadId; + this.startPos = startPos; + this.endPos = endPos; + } + + public void run() { + RandomAccessFile raf = null; + try { + raf = new RandomAccessFile("yunpan.exe", "rwd"); + + raf.seek(startPos); + + byte[] buffer = conn.read(startPos, endPos); + + raf.write(buffer, 0, buffer.length); + raf.close(); + barrier.await(); + System.out.println("threadId" + threadId +"download success !"); + + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } + + } +} diff --git a/group04/349184132/Study/src/com/coderising/download/FileDownloader.java b/group04/349184132/Study/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..281dafde96 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,112 @@ +package com.coderising.download; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.net.URL; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +public class FileDownloader { + + boolean isFinished = false; + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int THREAD_NUM = 3; + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + // 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM,new Runnable(){ + + @Override + public void run() { + listener.notifyFinished(); + } + + }); + + Connection conn = null; + try { + conn = cm.open(url); + + int length = conn.getContentLength(); + System.out.println("----文件总长度---- :" + length); + RandomAccessFile raf = new RandomAccessFile("yunpan.exe","rwd"); + + raf.setLength(length); + + int block = length / THREAD_NUM; + + for(int threadId = 0; threadId < THREAD_NUM; threadId++){ + int startPos = (threadId) * block; + int endPos = (threadId + 1 ) * block -1; + if(threadId-1 == THREAD_NUM){ + endPos = length; + } + System.out.println("---threadId--- :" + threadId + + "---startIndex---" + startPos + + "---endIndex---" + endPos); + //开启 线程 + URL u = new URL(url); + new DownloadThread(barrier,cm.open(url),threadId,startPos,endPos).start(); + } + + + } catch (ConnectionException e) { + + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally{ + if(conn!=null){ + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + + +} diff --git a/group04/349184132/Study/src/com/coderising/download/FileDownloaderTest.java b/group04/349184132/Study/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..604712d2a9 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,58 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://down.360safe.com/yunpan/360wangpan_setup.exe"; + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + +// 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group04/349184132/Study/src/com/coderising/download/api/Connection.java b/group04/349184132/Study/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group04/349184132/Study/src/com/coderising/download/api/ConnectionException.java b/group04/349184132/Study/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1599be1296 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + + public ConnectionException(String string) { + // TODO 自动生成的构造函数存根 + } + + +} diff --git a/group04/349184132/Study/src/com/coderising/download/api/ConnectionManager.java b/group04/349184132/Study/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group04/349184132/Study/src/com/coderising/download/api/DownloadListener.java b/group04/349184132/Study/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group04/349184132/Study/src/com/coderising/download/impl/ConnectionImpl.java b/group04/349184132/Study/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..3ad903146b --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,52 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + private HttpURLConnection conn ; + public ConnectionImpl(HttpURLConnection conn) { + this.conn = conn; + } + @Override + public byte[] read(int startPos, int endPos) throws IOException { + conn.setRequestMethod("GET"); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + conn.setConnectTimeout(5000); + + InputStream is = conn.getInputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int length = 0; + byte[] buffer = new byte[1024]; + while(-1 != ( length = is.read(buffer))){ + bos.write(buffer,0,length); + } + bos.flush(); + is.close(); + bos.close(); + + + return bos.toByteArray(); + } + + @Override + public int getContentLength() { + + return conn.getContentLength(); + } + + @Override + public void close() { + if(conn!=null){ + conn = null; + } + } + +} diff --git a/group04/349184132/Study/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group04/349184132/Study/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..9132787cf8 --- /dev/null +++ b/group04/349184132/Study/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,37 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + @Override + public Connection open(String url) throws ConnectionException { + + URL u; + HttpURLConnection hc ; + try { + u = new URL(url); + hc = (HttpURLConnection) u.openConnection(); + Connection conn = new ConnectionImpl(hc);; + return conn; + } catch (MalformedURLException e) { + e.printStackTrace(); + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + + + + + + } + +} diff --git a/group04/349184132/Study/src/com/linked/Iterator.java b/group04/349184132/Study/src/com/linked/Iterator.java new file mode 100644 index 0000000000..b2397b9aa7 --- /dev/null +++ b/group04/349184132/Study/src/com/linked/Iterator.java @@ -0,0 +1,7 @@ +package com.linked; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group04/349184132/Study/src/com/linked/LinkedList.java b/group04/349184132/Study/src/com/linked/LinkedList.java new file mode 100644 index 0000000000..44aba236b4 --- /dev/null +++ b/group04/349184132/Study/src/com/linked/LinkedList.java @@ -0,0 +1,388 @@ +package com.linked; + +import java.util.Objects; + + + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public LinkedList(){ + this.head = new Node(null,null); + } + + public boolean add(T o){ + + if(head.next == null){ + Node element = new Node(o,null); + head.next = element; + size++; + return true; + } + + Node current = head.next; + while(current != null){ + if(current.next==null){ + Node element = new Node(o,null); + current.next = element; + size++; + return true; + } + current = current.next; + } + + return false; + } + + + private void rangeCheck(int index) { + if (index < -1 || index > size - 1) + throw new IndexOutOfBoundsException(" index "); + } + public boolean add(int index , T o){ + rangeCheck(index); + + Node node = getNode(index); + Node pre = getNode(index-1); + Node newNode = new Node(o,node); + pre.next = newNode; + size++; + return true; + } + + + private Node getNode(int index){ + rangeCheck(index); + Node current = head.next; + int count = 0; + while(current!=null){ + if(count==index){ + return current; + } + count++; + current = current.next; + } + return null; + } + + public T get(int index){ + Node node = getNode(index); + return (T)node.data; + } + public T remove(int index){ + rangeCheck(index); + + Node pre = getNode(index-1); + Node cur = getNode(index); + Node next = cur.next; + pre.next = next; + cur.next = null; + size--; + return (T)cur.data; + } + + + public T remove(T o) { + int index = 0; + for (Node x = head.next; x != null; x = x.next) { + if (Objects.deepEquals(x.data, o)) { + return remove(index); + } + index++; + } + size--; + + return null; + } + + @Override + public T set(int index, T element) { + Node node = getNode(index); + node.data = element; + + return (T)node.data; + } + + @Override + public boolean contains(Object o) { + + return indexOf(o)!=-1; + } + + @Override + public int indexOf(Object o) { + int index = 0; + + for (Node x = head.next; x != null; x = x.next) { + if (Objects.deepEquals(x.data, o)) + return index; + index++; + } + return -1; + } + + @Override + public Object[] toArray() { + Object[] result = new Object[size]; + int i = 0; + for(Node x = head.next; x != null; x = x.next){ + result[i++] = x.data; + } + return null; + } + + @Override + public void clear() { + for(Node cur = head.next;cur!=null;cur = cur.next){ + Node x = cur; + x.data = null; + x.next = null; + } + head = null; + size = 0; + } + + + public int size(){ + return size; + } + public boolean isEmpty() { + return size == 0; + } + public void addFirst(Object o){ + Node newFirst = new Node(o,null); + Node oldFirst = head.next; + head.next = newFirst; + newFirst.next = oldFirst; + size++; + } + public void addLast(Object o){ + Node last = getNode(size-1); + Node newLast = new Node(o,null); + last.next = newLast; + size++; + } + public T removeFirst(){ + Node oldFirst = head.next; + Node nextNode = oldFirst.next; + head.next = nextNode; + size--; + return (T)oldFirst; + } + public T removeLast(){ + Node x = getNode(size-2);//倒数第二个结点 + Node last = x.next; + x.next = null; + size--; + return (T)last; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int pos = 0; + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public Object next() { + if (pos > size) + throw new IllegalArgumentException(); + return get(pos++); + } + } + + + + private static class Node{ + Object data; + Node next; + private Node(Object data, Node next) { + this.data = data; + this.next = next; + + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public Node reverseFor(Node head){ + if(head==null){ + return null; + } + Node pre = null; + Node curr = head; + Node next = head.next; + while(curr.next!=null){ + + head.next = pre; + pre = curr; + curr = curr.next; + next = next.next; + head = curr; + } + pre = null; + curr = null; + return head; + } + /** + * 递归写法 + * @param node + * @return + */ + public Node reverseRecursion(Node current){ + if(current == null || current.next == null){ + return current; + } + Node nextNode = current.next; + current = null; + Node reverseNode = reverseRecursion(current.next); + nextNode.next = current; + + + return reverseNode; + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int delectLength = size/2; + for(int i=0;isize-length){ + throw new IllegalArgumentException(i +" or "+length +" error"); + } + for(int j=i;j101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(list==null){ + throw new NullPointerException("List is null"); + } + int[] result = new int[list.size()]; + int index = 0; + for(Iterator iter = list.iterator();iter.hasNext();){ + int LinkIndex = (int)iter.next(); + result[index] = (int)get(LinkIndex); + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + if(list == null){ + throw new NullPointerException("List is null"); + } + int index = 0; + for(Node cur = head.next ; cur !=null ; cur = cur.next){ + for(Node newList = list.head.next ; newList != null; newList = newList.next ){ + if(Objects.deepEquals(cur.data, newList.data)){ + remove(index); + } + } + index++; + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + for(Node current=head.next;current!=null;current=current.next){ + Node nextNode = current.next; + + if(current.data.equals(nextNode.data)){ + Node nextNodeNext = nextNode.next; + if(nextNodeNext==null){ + current.next = null; + }else{ + current.next = nextNodeNext; + nextNode.next = null; + } + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max) { + if (min + max > size && min == max) { + throw new IndexOutOfBoundsException("Arguement is Illegal"); + } + int index = 0; + for (Node curr = head.next; curr != null; curr = curr.next) { + if(((int)curr.data>min) && ((int)curr.data { + public boolean add(T o); + + public boolean add(int index, T o); + + public T get(int index); + + T set(int index, T element); + + public T remove(int index); + + public T remove(T o); + + public int size(); + + public boolean isEmpty(); + + public Iterator iterator(); + + public boolean contains(Object o); + + int indexOf(Object o); + + + Object[] toArray(); + + void clear(); + +} diff --git a/group04/349184132/Study/src/com/second/Array/ArrayUtilTest.java b/group04/349184132/Study/src/com/second/Array/ArrayUtilTest.java index 06944cd8b1..4acbbf8880 100644 --- a/group04/349184132/Study/src/com/second/Array/ArrayUtilTest.java +++ b/group04/349184132/Study/src/com/second/Array/ArrayUtilTest.java @@ -64,7 +64,7 @@ public void testFibonacci() { public void testGetPrimes() { int[] primes = {2,3,5,7,11,13,17,19}; int max = 23; - Assert.assertArrayEquals(primes, ArrayUtil.getPrimes(max)); + Assert.assertArrayEquals(primes, ArrayUtil.getPrimes(27)); } @Test diff --git a/group04/351121278/src/com/coding/download/DownloadThread.java b/group04/351121278/src/com/coding/download/DownloadThread.java new file mode 100644 index 0000000000..e0b396a5e6 --- /dev/null +++ b/group04/351121278/src/com/coding/download/DownloadThread.java @@ -0,0 +1,34 @@ +package com.coding.download; + +import com.coding.download.api.Connection; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + File file; + + public DownloadThread(File file, Connection conn, int startPos, int endPos){ + this.file = file; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + try { + System.out.println("DownloadThread.run"); + byte[] buffer = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf.seek(startPos); + raf.write(buffer, 0, buffer.length); + raf.close(); + } catch (IOException e) { + System.out.println("e = " + e.getMessage()); + } + } +} diff --git a/group04/351121278/src/com/coding/download/FileDownloader.java b/group04/351121278/src/com/coding/download/FileDownloader.java new file mode 100644 index 0000000000..13d24f5b2b --- /dev/null +++ b/group04/351121278/src/com/coding/download/FileDownloader.java @@ -0,0 +1,76 @@ +package com.coding.download; + + +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.api.ConnectionManager; +import com.coding.download.api.DownloadListener; + +import java.io.File; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + File file = new File("D:/test"); + int length = conn.getContentLength(); + for (int i=0; i<3; i++) { + new DownloadThread(file, conn, 0, length-1).start(); + } + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group04/351121278/src/com/coding/download/FileDownloaderTest.java b/group04/351121278/src/com/coding/download/FileDownloaderTest.java new file mode 100644 index 0000000000..84fbc8afa2 --- /dev/null +++ b/group04/351121278/src/com/coding/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coding.download; + +import com.coding.download.api.ConnectionManager; +import com.coding.download.api.DownloadListener; +import com.coding.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group04/351121278/src/com/coding/download/api/Connection.java b/group04/351121278/src/com/coding/download/api/Connection.java new file mode 100644 index 0000000000..bd75d6cad0 --- /dev/null +++ b/group04/351121278/src/com/coding/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coding.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group04/351121278/src/com/coding/download/api/ConnectionException.java b/group04/351121278/src/com/coding/download/api/ConnectionException.java new file mode 100644 index 0000000000..1f57f86606 --- /dev/null +++ b/group04/351121278/src/com/coding/download/api/ConnectionException.java @@ -0,0 +1,10 @@ +package com.coding.download.api; + +public class ConnectionException extends Exception { + + public ConnectionException(String exceptionMessage) { + + } + public ConnectionException() { + } +} diff --git a/group04/351121278/src/com/coding/download/api/ConnectionManager.java b/group04/351121278/src/com/coding/download/api/ConnectionManager.java new file mode 100644 index 0000000000..1d1a83caf2 --- /dev/null +++ b/group04/351121278/src/com/coding/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coding.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group04/351121278/src/com/coding/download/api/DownloadListener.java b/group04/351121278/src/com/coding/download/api/DownloadListener.java new file mode 100644 index 0000000000..c41045b0e8 --- /dev/null +++ b/group04/351121278/src/com/coding/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coding.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group04/351121278/src/com/coding/download/impl/ConnectionImpl.java b/group04/351121278/src/com/coding/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..7d524ec4a0 --- /dev/null +++ b/group04/351121278/src/com/coding/download/impl/ConnectionImpl.java @@ -0,0 +1,50 @@ +package com.coding.download.impl; + +import com.coding.download.api.Connection; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + + +public class ConnectionImpl implements Connection { + + private static final int THEAD_COUNT = 3; + private URL url; + private HttpURLConnection httpURLConnection; + private final int BUFFER_SIZE = 1024; + + public ConnectionImpl(URL url) { + this.url = url; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + URLConnection urlConnection = url.openConnection(); + httpURLConnection = (HttpURLConnection)urlConnection; + httpURLConnection.setRequestMethod("GET"); + InputStream in = httpURLConnection.getInputStream(); + ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); + int len; + byte[] buffer = new byte[BUFFER_SIZE]; + while ((len = in.read(buffer)) != -1) { + byteOutputStream.write(buffer, startPos, len); + } + return byteOutputStream.toByteArray(); + } + + @Override + public int getContentLength() { + return httpURLConnection.getContentLength(); + } + + @Override + public void close() { + httpURLConnection.disconnect(); + } + +} diff --git a/group04/351121278/src/com/coding/download/impl/ConnectionManagerImpl.java b/group04/351121278/src/com/coding/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..c07a0b545a --- /dev/null +++ b/group04/351121278/src/com/coding/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,23 @@ +package com.coding.download.impl; + +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.api.ConnectionManager; + +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + URL urlObj; + try { + urlObj = new URL(url); + } catch (MalformedURLException e) { + throw new ConnectionException("URL无法访问" + e.getMessage()); + } + return new ConnectionImpl(urlObj); + } + +} diff --git a/group04/474772605/.classpath b/group04/474772605/.classpath index 28e2b79383..8d7ead9fe8 100644 --- a/group04/474772605/.classpath +++ b/group04/474772605/.classpath @@ -1,7 +1,11 @@ + + + + diff --git a/group04/474772605/jsp/homepage.jsp b/group04/474772605/jsp/homepage.jsp new file mode 100644 index 0000000000..83fa84db7d --- /dev/null +++ b/group04/474772605/jsp/homepage.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +homepage + + + + + \ No newline at end of file diff --git a/group04/474772605/jsp/showLogin.jsp b/group04/474772605/jsp/showLogin.jsp new file mode 100644 index 0000000000..1e6cda01b1 --- /dev/null +++ b/group04/474772605/jsp/showLogin.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +showLogin + + + + + \ No newline at end of file diff --git a/group04/474772605/src/com/coderising/action/LoginAction.java b/group04/474772605/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..69ad2750c0 --- /dev/null +++ b/group04/474772605/src/com/coderising/action/LoginAction.java @@ -0,0 +1,40 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group04/474772605/src/com/coderising/action/LogoutAction.java b/group04/474772605/src/com/coderising/action/LogoutAction.java new file mode 100644 index 0000000000..10e4eb37c7 --- /dev/null +++ b/group04/474772605/src/com/coderising/action/LogoutAction.java @@ -0,0 +1,40 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LogoutAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success1"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group04/474772605/src/com/coderising/action/Struts.java b/group04/474772605/src/com/coderising/action/Struts.java new file mode 100644 index 0000000000..7db0e4687f --- /dev/null +++ b/group04/474772605/src/com/coderising/action/Struts.java @@ -0,0 +1,122 @@ +package com.coderising.action; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + + + public static void main(String[] args) throws DocumentException { + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = Struts.runAction(actionName,params); + System.out.println(view.getJsp()); + System.out.println(view.getParameters()); + } + + + public static View runAction(String actionName, Map parameters) { + View view = new View(); + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + org.dom4j.Document document; + try { + document = reader.read(new File("src/struts.xml")); + Element root = document.getRootElement(); + @SuppressWarnings("unchecked") + List elements = root.elements(); + for (Element element : elements) { + Attribute actionAttribute = element.attribute("name"); + Attribute classAttribute = element.attribute("class"); + if(actionName.equals(actionAttribute.getValue())){ + String clazz = null; + clazz = classAttribute.getValue(); + Object o = Class.forName(clazz).newInstance(); + for (Map.Entry entry : parameters.entrySet()) { + String name = entry.getKey(); + String value =entry.getValue(); + String methodname = "set"+name.substring(0,1).toUpperCase()+name.substring(1); + Method m = o.getClass().getMethod(methodname, String.class); + m.invoke(o, value); + + } + Method m3 = o.getClass().getMethod("execute"); + String result = (String) m3.invoke(o); + String jspPath = null; + List element1s = element.elements("result"); + if(result.equals("success")){ + for (int i = 0; i < element1s.size(); i++) { + Attribute attribute2 = element1s.get(i).attribute("name"); + if (attribute2.getValue().equals("success")) { + jspPath = element1s.get(i).getStringValue(); + } + } + }else if(result.equals("fail")){ + for (int i = 0; i < element1s.size(); i++) { + Attribute attribute2 = element1s.get(i).attribute("name"); + if (attribute2.getValue().equals("fail")) { + jspPath = element1s.get(i).getStringValue(); + } + } + } + HashMapviewparamterHashMap = new HashMap(); + Method[]methods = o.getClass().getMethods(); + String methodname; + for (int j = 0; j < o.getClass().getMethods().length; j++) { + methodname = methods[j].getName(); + if(methodname.startsWith("get")&&!methodname.equals("getClass")){ + String methodname1 = methods[j].getName(); + methodname1 = methodname.substring(3,4).toUpperCase()+methodname1.substring(4); + viewparamterHashMap.put(methodname1, methods[j].invoke(o)); + } + } + view.setJsp(jspPath); + view.setParameters(viewparamterHashMap); + return view; + } + } + } catch (Exception e) { + // TODO: handle exception + } + return null; + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + } + +} diff --git a/group04/474772605/src/com/coderising/action/StrutsTest.java b/group04/474772605/src/com/coderising/action/StrutsTest.java new file mode 100644 index 0000000000..c161c0f932 --- /dev/null +++ b/group04/474772605/src/com/coderising/action/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.action; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "com.coderising.action.LoginAction"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group04/474772605/src/com/coderising/action/View.java b/group04/474772605/src/com/coderising/action/View.java new file mode 100644 index 0000000000..11cb1872e5 --- /dev/null +++ b/group04/474772605/src/com/coderising/action/View.java @@ -0,0 +1,23 @@ +package com.coderising.action; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group04/474772605/src/com/coderising/array/ArrayUtil.java b/group04/474772605/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..ddb00c17b6 --- /dev/null +++ b/group04/474772605/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,146 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Stack; + +import com.coding.basic.LinkedList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + Stack stack = new Stack(); + for (int i = 0; i < origin.length; i++) { + stack.add(origin[i]); + } + + for (int j = 0; j < stack.size(); j++) { + origin[j] = (Integer) stack.pop(); + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + ArrayList newarray = new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + if(0!=oldArray[i]){ + newarray.add(oldArray[i]); + } + } + int result [] = new int [newarray.size()]; + for (int j = 0; j < result.length; j++) { + result[j]=newarray.get(j); + } + + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + ArrayList newarray = new ArrayList(); + for (int i = 0; i < array1.length; i++) { + newarray.add(i, array1[i]); + } + for (int j = 0; j < array2.length; j++) { + if (newarray.get(j)>array2[j]&&newarray.get(j+1)>array2[j]) { + newarray.add(j+1, array2[j]); + } + } + + int result [] = new int [newarray.size()]; + for (int z = 0; z < result.length; z++) { + result[z]=newarray.get(z); + } + return result; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + ArrayList newarray = new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + newarray.add(i, oldArray[i]); + } + while (newarray.size()='A'&&(char)num <='z'){ + n1++; + } + if((char)num >='A'&&(char)num <='Z'){ + n2++; + } + } + } + reader.close(); + System.out.println(n1+""+n2); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + + public float method(){ + return 13.21f; + } + + + + + } + + + + + + + + + + + diff --git a/group04/474772605/src/com/coding/iostreams/test.java b/group04/474772605/src/com/coding/iostreams/test.java new file mode 100644 index 0000000000..1bd474ca7b --- /dev/null +++ b/group04/474772605/src/com/coding/iostreams/test.java @@ -0,0 +1,14 @@ +package com.coding.iostreams; + +public interface test { + public float method(); + +} + + + + + + + + diff --git a/group04/474772605/test/Test.java b/group04/474772605/test/Test.java new file mode 100644 index 0000000000..8c0f62930b --- /dev/null +++ b/group04/474772605/test/Test.java @@ -0,0 +1,50 @@ + + + import java.io.File; + import java.io.FileInputStream; + import java.io.InputStreamReader; + import java.io.Reader; + + + + + public class Test{ + static int n1 =0 ; + static int n2 =0 ; + + public static void main(String[] args) throws Exception{ + + Test.readFileByChars("D://Text.txt"); + + System.out.println("字母个数为:"+n1+" 字母个数为"+n2); + + } + + public static void readFileByChars(String fileName) { + File file = new File(fileName); + Reader reader = null; + try { + System.out.println("以字符为单位读取文件内容,一次读一个字节:"); + // 一次读一个字符 + reader = new InputStreamReader(new FileInputStream(file)); + int tempchar; + while ((tempchar = reader.read()) != -1) { + // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 + // 但如果这两个字符分开显示时,会换两次行。 + // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 + if (((char) tempchar) != '\r') { + System.out.print((char) tempchar); + n1++; + if((char)tempchar >='A'&&(char)tempchar<='Z'){ + n2++; + } + + } + } + reader.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + diff --git a/group04/474772605/test/com/coding/basic/Heros.java b/group04/474772605/test/com/coding/basic/Heros.java new file mode 100644 index 0000000000..878204aede --- /dev/null +++ b/group04/474772605/test/com/coding/basic/Heros.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +import java.lang.reflect.Method; + +public class Heros { + private String name;//名字 + private String type;//类型 + private int camp;//0,近卫;1,天灾 + public Heros(){ + + } + /* + public Heros(String name, String type, int camp) { + + super(); + + this.name = name; + this.type = type; + this.camp = camp; + }*/ + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getCamp() { + return camp; + } + + public void setCamp(int camp) { + this.camp = camp; + } + + @Override + public String toString() { + return "Heros [\n name=" + name + ", \n type=" + type + ", \n camp=" + camp + "\n]"; + } + +} \ No newline at end of file diff --git a/group04/474772605/test/com/coding/basic/Test.java b/group04/474772605/test/com/coding/basic/Test.java new file mode 100644 index 0000000000..97f7254c82 --- /dev/null +++ b/group04/474772605/test/com/coding/basic/Test.java @@ -0,0 +1,43 @@ +package com.coding.basic; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class Test { + + public static void main(String args[]) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Foo foo = new Foo("这个一个Foo对象!"); + Class clazz = foo.getClass(); + Field[] abc =clazz.getDeclaredFields(); + + // Object value = getFieldValueByName(key, obj);  + Method m1 = clazz.getDeclaredMethod("outInfo"); + Method m2 = clazz.getDeclaredMethod("setMsg", String.class); + Method m3 = clazz.getDeclaredMethod("getMsg"); + m1.invoke(foo); + m2.invoke(foo, "重新设置msg信息!"); + String msg = (String) m3.invoke(foo); + System.out.println(msg); + } +} + +class Foo { + private String msg; + + public Foo(String msg) { + this.msg = msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public String getMsg() { + return msg; + } + + public void outInfo() { + System.out.println("这是测试Java反射的测试类"); + } +} diff --git a/group04/474772605/test/com/coding/basic/TestStack.java b/group04/474772605/test/com/coding/basic/TestStack.java new file mode 100644 index 0000000000..e4caa84d98 --- /dev/null +++ b/group04/474772605/test/com/coding/basic/TestStack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +import junit.framework.TestCase; + +public class TestStack extends TestCase{ +private Stack stack; + + +public void setUp() throws Exception { + stack = new Stack(); + +} + +public void testpop(){ +// Stack stack = new Stack(); + Object o = null ; + try { + stack.push(o); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + +} + + + + + + + +} diff --git a/group04/474772605/test/com/coding/basic/Testarray.java b/group04/474772605/test/com/coding/basic/Testarray.java new file mode 100644 index 0000000000..19edb9f85d --- /dev/null +++ b/group04/474772605/test/com/coding/basic/Testarray.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +import junit.framework.TestCase; + +public class Testarray extends TestCase{ + + public void testararry(){ + Throwable tx = null; + try { + ArrayList n = new ArrayList(); + Object o = null ; + + n.add(o); + fail(); + } catch (Exception e) { + tx =e; + assertEquals(Exception.class, tx.getClass()); + assertEquals("对象不能为空", e.getMessage()); + } + } + +} diff --git a/group04/474772605/test/com/coding/basic/teest.java b/group04/474772605/test/com/coding/basic/teest.java new file mode 100644 index 0000000000..e8004c14b6 --- /dev/null +++ b/group04/474772605/test/com/coding/basic/teest.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +import java.lang.reflect.Method; + +public class teest { + public static void main(String[] args) { + Class herosClass = Heros.class; + try { + Method m1 = herosClass.getMethod("setName",String.class); + Method m3 = herosClass.getMethod("setCamp",int.class); + Method m2 = herosClass.getMethod("getName"); + + + Object userInfo = herosClass.newInstance(); + System.out.println("调用构造函数:"+userInfo); + m1.invoke(userInfo,"影魔"); + m3.invoke(userInfo, 1); + System.out.println("调用set方法:"+userInfo); + System.out.println("调用get方法:"+m2.invoke(userInfo)); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group04/498654356/mini-jvm/jvm/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java b/group04/498654356/mini-jvm/jvm/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..b465389a69 --- /dev/null +++ b/group04/498654356/mini-jvm/jvm/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,83 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; + + + +public class ClassFileLoader { + + private static final String CLASS_SUFFIX = ".class"; + private static final byte[] EMPTY_BYTES = new byte[0]; + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + if(StringUtils.isEmpty(className)) { + return EMPTY_BYTES; + } + String child = className.replaceAll("\\.", "\\\\") + CLASS_SUFFIX; + for (String parent: clzPaths) { + File file = new File(parent, child); + if(file.exists()) { + return doReadBinaryCode(file); + } + } + return EMPTY_BYTES; + } + + + private byte[] doReadBinaryCode(File file) { + FileInputStream fis = null; + ByteArrayOutputStream baos = null; + try { + fis = new FileInputStream(file); + baos = new ByteArrayOutputStream(); + byte[] b = new byte[1024]; + int len = 0; + while((len = fis.read(b)) > 0) { + baos.write(b, 0, len); + } + return baos.toByteArray(); + } catch (Exception e) { + new RuntimeException(e); + } finally { + close(baos); + close(fis); + } + return EMPTY_BYTES; + } + + + private void close(Closeable stream) { + if(stream != null ) { + try { + stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + StringBuilder builder = new StringBuilder(); + for (String path : clzPaths) { + builder.append(path).append(";"); + } + if(builder.length() > 0) { + builder = builder.deleteCharAt(builder.length() - 1); + } + return builder.toString(); + } + +} diff --git a/group04/498654356/mini-jvm/jvm/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java b/group04/498654356/mini-jvm/jvm/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..7b9ca5c34d --- /dev/null +++ b/group04/498654356/mini-jvm/jvm/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,93 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + private static final String CAFEBABE = "cafebabe"; + static String path1 = "D:\\Dev\\git_repository\\coding2017\\group04\\498654356\\mini-jvm\\jvm\\target\\test-classes"; + static String path2 = "C:\\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals(CAFEBABE, acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i size) { + return Arrays.copyOf(baos.toByteArray(), size); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + try { + return url.openConnection().getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + try { + url.openStream().close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/group04/498654356/one/src/org/coding/three/download/impl/ConnectionManagerImpl.java b/group04/498654356/one/src/org/coding/three/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..21c7916805 --- /dev/null +++ b/group04/498654356/one/src/org/coding/three/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package org.coding.three.download.impl; + +import org.coding.three.download.api.Connection; +import org.coding.three.download.api.ConnectionException; +import org.coding.three.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group04/498654356/one/src/org/coding/three/list/List.java b/group04/498654356/one/src/org/coding/three/list/List.java new file mode 100644 index 0000000000..f631a65ba3 --- /dev/null +++ b/group04/498654356/one/src/org/coding/three/list/List.java @@ -0,0 +1,9 @@ +package org.coding.three.list; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java b/group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java new file mode 100644 index 0000000000..03601b055d --- /dev/null +++ b/group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java @@ -0,0 +1,422 @@ +package org.coding.three.list.impl; + +import java.util.Arrays; +import java.util.Iterator; + +import org.coding.three.list.List; +/** + * 单链表/单向链表 + */ +public class LinkedList implements List { + /** + * 0. head 节点存储数据 + * 1. 这里的 head 第一次添加之后 "引用" 将不再改变;"值" 可以被修改已表示往首节点插入新的值。 + * 2. 可以将 head 修改为对 node 引用, 不存储任何数据。 + */ + private Node head; + + public void add(Object o){ + Node node = new Node(o); + if(head == null){ //第一次 + head = node; + } else { + getNode(size() - 1).next = node; + } + } + + private Node getNode(int index) { + checkIndex(index); + Node node = head; + for(int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private void checkIndex(int index) { + int size = size(); + if(index < 0 || index > (size - 1)) { + throw new IndexOutOfBoundsException("size = " + size + ", index = " + index); + } + } + public void add(int index , Object o){ + checkIndex(index); + if(index == 0) { //更新 head 的值, 将旧值创建新的Node插入到 head 后 + Object data = head.data; + head.data = o; + Node node = new Node(data); + node.next = head.next; + head.next = node; + } else { + Node pre = getNode(index - 1); + Node node = new Node(o); + node.next = pre.next; + pre.next = node; + } + } + public Object get(int index){ + checkIndex(index); + return getNode(index).data; + } + public Object remove(int index){ + checkIndex(index); + Object data = null; + if(index == 0) { + Node next = head.next; + data = head.data; + if(next == null) { + head = null; + } else { + head.data = next.data; + head.next = next.next; + next.next = null; + } + } else { + Node pre = getNode(index - 1); + Node node = pre.next; + pre.next = node.next; + node.next = null; + data = node.data; + } + return data; + } + + public int size(){ + Node temp = head; + int size = 0; + while(temp != null) { + size++; + temp = temp.next; + } + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size() - 1); + } + public Iterator iterator(){ + return new LinkedIterator(); + } + + class LinkedIterator implements Iterator { + int cursor = 0; + int lastRet = -1; + @Override + public boolean hasNext() { + return cursor != LinkedList.this.size(); + } + + @Override + public Object next() { + int i = cursor; + Object data = LinkedList.this.get(i); + lastRet = i; + cursor = i + 1; + return data; + } + + @Override + public void remove() { + if(lastRet < - 1) { + throw new RuntimeException("非法操作"); + } + LinkedList.this.remove(lastRet); + cursor--; + lastRet = -1; + } + } + + private static class Node{ + Object data; + Node next; + public Node(Object data) { + super(); + this.data = data; + } + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + int size = size(); + if(size < 2) { + return ; + } + int preIndex = 0; + int behindIndex = size - 1; + Node preNode = head; + while(preIndex < behindIndex) { + Node behindNode = getNode(behindIndex); + Object temp = preNode.data; + preNode.data = behindNode.data; + behindNode.data = temp; + preIndex++; + behindIndex--; + preNode = preNode.next; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(size() < 2) { + return; + } + int count = size() / 2; + Node preNode = getNode(count - 1); + Node nextNode = preNode.next; + preNode.next = null; + head.data = nextNode.data; + head.next = nextNode.next; + nextNode.next = null; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int index, int length){ + checkIndex(index); + int size = size(); + if(index + length > size) { + length = size; + } + if(index == 0 && length == size) { + head = null; + return; + } + int tempIndex = index + length - 1; + Node endNode = getNode(tempIndex); + Node nextNode = endNode.next; + endNode.next = null; + if(index == 0) { //head + Node nnextNode = nextNode.next; + nextNode.next = null; + head.data = nextNode.data; + head.next = nnextNode; + } else { + Node preStartNode = getNode(index - 1); + preStartNode.next = nextNode; + } + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + Iterator it = list.iterator(); + int[] array = new int[list.size()]; + int size = size(); + int length = 0; + while(it.hasNext()) { + int index = (int) it.next(); + if(index >= size) { + break; + } + array[length++] = (int) get(index); + } + if(length == array.length) { + return array; + } else { + return Arrays.copyOf(array, length); + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + Iterator it = list.iterator(); + while(it.hasNext()) { + Object next = it.next(); + Iterator iterator = this.iterator(); + while(iterator.hasNext()) { + if(next.equals(iterator.next())) { + iterator.remove(); + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(size() < 2) { + return; + } + Iterator it = iterator(); + Object pre = null; + while(it.hasNext()){ + Object data = it.next(); + if(pre != null && pre.equals(data)) { + it.remove(); + } else { + pre = data; + } + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int size = size(); + if(size < 1) { + return; + } + int minVal = (int)get(0); + int maxVal = (int)get(size - 1); + if(minVal > min && maxVal < max) { //直接清空 + this.head = null; + return; + } + if(max <= minVal) { + return; + } + if(min >= maxVal) { + return; + } + int startIndex = getMinIndex(min, size); + int endIndex = getMaxIndex(max, size); + if(endIndex - startIndex < 0) { + return; + } + remove(startIndex, (endIndex - startIndex) + 1); + + } + + private int getMaxIndex(int max, int size) { + int start = 0; + int end = size - 1; + while(start < end) { + int index = (end + start) / 2; + int midVal = (int) get(index); + if(midVal == max) { + return index - 1; +// index = index - 1; +// Node node = getNode(index); +// if((int)node.data < maxVal) { +// return index ; +// }//不考虑重复 TODO + } + if(midVal > max) { + end = index - 1; + } else { + start = index + 1; + } + } + if((int)get(end) >= max) { + return 0; + } + return end; + } + + private int getMinIndex(int min, int size) { + int start = 0; + int end = size - 1; + while(start < end) { + int index = (end + start) / 2; + int midVal = (int) get(index); + if(midVal == min) { + return index + 1; +// Node node = getNode(index); //暂无考虑重复 TODO +// if(node.next != null && (int)node.next.data > midVal) { +// return index + 1; +// } else { +// while(node.next != null && (int)node.next.data == midVal) { // 重复值 +// node = node.next; +// index++; +// } +// return index; +// +// } + } + if(midVal > min) { + end = index - 1; + } else { + start = index + 1; + } + } + if((int)get(start) <= min) { + return size; + } + return start; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList linkedList = new LinkedList(); + if(list == null || size() == 0 || list.size() == 0) { + return linkedList; + } + Iterator it = iterator(); + int index = 0; + boolean iseqFlag = false; + boolean isgtFlag = false; + while(it.hasNext()) { + int v1 = (int) it.next(); + if(index != 0) { + if(iseqFlag) { + list.remove(0, index + 1); + iseqFlag = false; + } + if(isgtFlag) { + list.remove(0, index); + isgtFlag = false; + } + } + Iterator it2 = list.iterator(); + while(it2.hasNext()) { + int v2 = (int) it2.next(); + if(v2 == v1) { + linkedList.add(v1); + iseqFlag = true; + break; + } else if(v2 > v1) { + isgtFlag = true; + break; + } + index++; + } + if(index == list.size()) { //第二个链表中的值是否全部小于现在第一个链表中正在进行比较的值 + break; + } + } + return linkedList; + } +} diff --git a/group04/498654356/one/test/org/coding/four/lru/LRUPageFrameTest.java b/group04/498654356/one/test/org/coding/four/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..d93b78ec32 --- /dev/null +++ b/group04/498654356/one/test/org/coding/four/lru/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package org.coding.four.lru; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group04/498654356/one/test/org/coding/three/download/FileDownloaderTest.java b/group04/498654356/one/test/org/coding/three/download/FileDownloaderTest.java new file mode 100644 index 0000000000..fcaecf50ae --- /dev/null +++ b/group04/498654356/one/test/org/coding/three/download/FileDownloaderTest.java @@ -0,0 +1,61 @@ +package org.coding.three.download; + +import org.coding.three.download.api.ConnectionManager; +import org.coding.three.download.api.DownloadListener; +import org.coding.three.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + +// String url = "http://7xq43s.com1.z0.glb.clouddn.com/yunanding-6.jpg"; +// String url = "http://www.yinwang.org/blog-cn/2016/11/17/all-about-hillary"; +// String url = "http://orig04.deviantart.net/93d4/f/2007/314/9/5/audrey_tautou_by_shimoda7.jpg"; + String url = "http://pic36.nipic.com/20131230/1081324_162447228136_2.jpg"; + String destpath = "D:/b.jpg"; + int threadCount = 3; + + FileDownloader downloader = new FileDownloader(url, destpath, threadCount); + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group04/498654356/one/test/org/coding/three/list/impl/LinkedListTest.java b/group04/498654356/one/test/org/coding/three/list/impl/LinkedListTest.java new file mode 100644 index 0000000000..884407397b --- /dev/null +++ b/group04/498654356/one/test/org/coding/three/list/impl/LinkedListTest.java @@ -0,0 +1,477 @@ +package org.coding.three.list.impl; + +import static org.junit.Assert.fail; + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class LinkedListTest { + private LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void testAddObject() { + int expected = 0; + int actual = linkedList.size(); + Assert.assertEquals(expected, actual); + + linkedList.add(1); + expected = 1; + actual = linkedList.size(); + Assert.assertEquals(expected, actual); + + linkedList.add(2); + expected = 2; + actual = linkedList.size(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testAddIntObject() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + + linkedList.add(0, 4); + int expected = 4; + int actual = (int) linkedList.get(0); + Assert.assertEquals(expected, actual); + Assert.assertEquals(4, linkedList.size()); + + linkedList.add(2, 5); + Assert.assertEquals(5, linkedList.size()); + expected = 5; + actual = (int) linkedList.get(2); + Assert.assertEquals(expected, actual); + + } + + @Test + public void testGet() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + + int expected = 1; + int actual = (int) linkedList.get(0); + Assert.assertEquals(expected, actual); + + expected = 2; + actual = (int) linkedList.get(1); + Assert.assertEquals(expected, actual); + + expected = 3; + actual = (int) linkedList.get(2); + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemoveInt() { + linkedList.add(1); + + int v = (int) linkedList.remove(0); + Assert.assertEquals(1, v); + Assert.assertEquals(0, linkedList.size()); + + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + + v = (int) linkedList.remove(1); + Assert.assertEquals(2, v); + Assert.assertEquals(2, linkedList.size()); + + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + + v = (int) linkedList.remove(linkedList.size() - 1); + Assert.assertEquals(6, v); + Assert.assertEquals(4, linkedList.size()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, linkedList.size()); + linkedList.add(1); + Assert.assertEquals(1, linkedList.size()); + linkedList.remove(0); + Assert.assertEquals(0, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + linkedList.addFirst(1); + Assert.assertEquals(4, linkedList.size()); + Assert.assertEquals(1, linkedList.get(0)); + } + + @Test + public void testAddLast() { + linkedList.addLast(1); + Assert.assertEquals(1, linkedList.size()); + Assert.assertEquals(1, linkedList.get(0)); + linkedList.addLast(2); + Assert.assertEquals(2, linkedList.size()); + Assert.assertEquals(2, linkedList.get(1)); + } + + @Test + public void testRemoveFirst() { + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + int v = (int) linkedList.removeFirst(); + Assert.assertEquals(2, linkedList.size()); + Assert.assertEquals(4, v); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveFirstException() { + linkedList.removeFirst(); + } + + @Test + public void testRemoveLast() { + linkedList.add(4); + int v = (int) linkedList.removeLast(); + Assert.assertEquals(0, linkedList.size()); + Assert.assertEquals(4, v); + + linkedList.add(5); + linkedList.add(6); + v = (int) linkedList.removeLast(); + Assert.assertEquals(1, linkedList.size()); + Assert.assertEquals(6, v); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveLastException() { + linkedList.removeLast(); + } + @Test + public void testIterator() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + Iterator it = linkedList.iterator(); + int expected = 1; + while(it.hasNext()) { + Object v = it.next(); + Assert.assertEquals(expected++, v); + } + + } + + @Test + public void testIteratorRemove() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + Iterator it = linkedList.iterator(); + while(it.hasNext()) { + it.next(); + it.remove(); + } + Assert.assertEquals(0, linkedList.size()); + + } + @Test + public void testReverse() { + linkedList.add(3); + linkedList.add(7); + linkedList.add(10); + linkedList.reverse(); + Assert.assertEquals(10, linkedList.get(0)); + Assert.assertEquals(7, linkedList.get(1)); + Assert.assertEquals(3, linkedList.get(2)); + } + + @Test + public void testReverse2() { + linkedList.add(3); + linkedList.reverse(); + Assert.assertEquals(3, linkedList.get(0)); + } + + @Test + public void testReverse3() { + linkedList.add(3); + linkedList.add(7); + linkedList.reverse(); + Assert.assertEquals(7, linkedList.get(0)); + Assert.assertEquals(3, linkedList.get(1)); + } + + + + @Test + public void testRemoveFirstHalf() { + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.removeFirstHalf(); + Assert.assertEquals(7, linkedList.get(0)); + Assert.assertEquals(8, linkedList.get(1)); + } + + @Test + public void testRemoveFirstHalf2() { + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + linkedList.removeFirstHalf(); + Assert.assertEquals(7, linkedList.get(0)); + Assert.assertEquals(8, linkedList.get(1)); + Assert.assertEquals(10, linkedList.get(2)); + } + + @Test + public void testRemoveIntInt() { + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + linkedList.remove(1, 2); + Assert.assertEquals(3, linkedList.size()); + Assert.assertEquals(2, linkedList.get(0)); + Assert.assertEquals(8, linkedList.get(1)); + Assert.assertEquals(10, linkedList.get(2)); + } + + + @Test + public void testRemoveIntIntFull() { + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + linkedList.remove(0, 10); + Assert.assertEquals(0, linkedList.size()); + } + + + @Test + public void testRemoveIntIntHead() { + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + linkedList.remove(0, 2); + Assert.assertEquals(3, linkedList.size()); + Assert.assertEquals(7, linkedList.get(0)); + Assert.assertEquals(8, linkedList.get(1)); + Assert.assertEquals(10, linkedList.get(2)); + } + + @Test + public void testGetElements() { +// 11->101->201->301->401->501->601->701 + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); +// 1->3->4->6 + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(6); + int[] actuals = linkedList.getElements(list ); + int[] expecteds = {101,301,401,601}; + Assert.assertArrayEquals(expecteds, actuals); + } + + + @Test + public void testGetElements2() { +// 11->101->201->301->401->501->601->701 + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); +// 1->3->4->20 + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(20); + int[] actuals = linkedList.getElements(list ); + int[] expecteds = {101,301,401}; + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testSubtract() { + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + LinkedList list = new LinkedList(); + list.add(11); + list.add(201); + list.add(501); + linkedList.subtract(list ); + Assert.assertEquals(5, linkedList.size()); + + } + + @Test + public void testSubtract2() { + linkedList.add(11); + linkedList.add(101); + LinkedList list = new LinkedList(); + list.add(11); + list.add(201); + list.add(501); + linkedList.subtract(list ); + Assert.assertEquals(1, linkedList.size()); + + } + + + @Test + public void testRemoveDuplicateValues() { + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(201); + linkedList.add(201); + linkedList.add(301); + linkedList.add(301); + linkedList.add(401); + Assert.assertEquals(8, linkedList.size()); + linkedList.removeDuplicateValues(); + Assert.assertEquals(5, linkedList.size()); + Assert.assertEquals(301, linkedList.get(linkedList.size() - 2)); + Assert.assertEquals(201, linkedList.get(linkedList.size() - 3)); + + } + + @Test + public void testRemoveRange() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.removeRange(4, 6); + Assert.assertEquals(2, linkedList.size()); + + } + + @Test + public void testRemoveRange2() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.removeRange(0, 6); + Assert.assertEquals(0, linkedList.size()); + + } + + @Test + public void testRemoveRange3() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.removeRange(3, 5); + Assert.assertEquals(3, linkedList.size()); + + } + + @Test + public void testRemoveRange4() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.removeRange(1, 3); + Assert.assertEquals(3, linkedList.size()); + + } + + @Test + public void testRemoveRange5() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.add(6); + linkedList.removeRange(3, 5); + Assert.assertEquals(4, linkedList.size()); + + } + + @Test + public void testIntersection() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.add(6); + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + LinkedList newList = linkedList.intersection(list ); + Assert.assertEquals(2, newList.size()); + Assert.assertEquals(1, newList.get(0)); + Assert.assertEquals(3, newList.get(1)); + } + + @Test + public void testIntersection2() { + linkedList.add(1); + linkedList.add(3); + linkedList.add(5); + linkedList.add(6); + LinkedList list = new LinkedList(); + list.add(10); + list.add(13); + LinkedList newList = linkedList.intersection(list ); + Assert.assertEquals(0, newList.size()); + } + + @Test + public void testIntersection3() { + linkedList.add(3); + linkedList.add(5); + linkedList.add(6); + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + LinkedList newList = linkedList.intersection(list ); + Assert.assertEquals(0, newList.size()); + } + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/DownloadThread.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..702edb8bbd --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,62 @@ +package com.coderising.download; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + private RandomAccessFile tempFile = null; + public DownloadThread( Connection conn, RandomAccessFile tempFile,String treadName){ + super.setName(treadName); + this.conn = conn; + this.startPos = conn.getStartPos(); + this.endPos = conn.getEndPos(); + this.tempFile = tempFile; + } + public void run(){ + + byte buf[] = null; + int count = (endPos - startPos)/1024; + int seekPos = 0; + try { + for (int i = 1; i < count; i++) { + System.out.println(this.getName() + " : " + (startPos+ 1024*(i-1)) + "-------" + (startPos + 1024*i) ); + buf = new byte[1024]; + conn.read(buf); + seekPos = startPos+ 1024*(i-1); + if (0 != seekPos) { + seekPos--; + } + tempFile.seek(seekPos); + writeToFile(buf); + buf = null; + } + + System.out.println(this.getName() + " : " + (startPos+ 1024*(count-1)) + "------- " + (endPos) ); + buf = new byte[endPos-(startPos+ 1024*(count-1))]; + conn.read(buf); + seekPos = startPos+ 1024*(count-1)-1; + tempFile.seek(seekPos); + writeToFile(buf); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private synchronized void writeToFile(byte[] buf) throws IOException { + tempFile.write(buf, 0, buf.length); + } + + + + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/FileDownloader.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..8a76bb29b4 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,115 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.net.URL; +import java.util.UUID; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + private String url; + private String localPath = ""; + private DownloadListener listener; + private ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + try { + + int length = cm.open(url).getContentLength(); + RandomAccessFile tempFile = CreateTempFile(localPath,cm.open(url)); + + int step = length / 4; + DownloadThread downloadThread0 = new DownloadThread(cm.open(this.url,0,step-1),tempFile,"downLoad_Thread0"); + DownloadThread downloadThread1 = new DownloadThread(cm.open(this.url,step,2*step-1),tempFile,"downLoad_Thread1"); + DownloadThread downloadThread2 = new DownloadThread(cm.open(this.url,2*step,3*step-1),tempFile,"downLoad_Thread2"); + DownloadThread downloadThread3 = new DownloadThread(cm.open(this.url,3*step,length),tempFile,"downLoad_Thread3"); + downloadThread0.start(); + downloadThread1.start(); + downloadThread2.start(); + downloadThread3.start(); + + while(true) { + if (!(downloadThread0.isAlive()||downloadThread1.isAlive()||downloadThread2.isAlive()||downloadThread3.isAlive())) { + tempFile.close(); + this.listener.notifyFinished(); + break; + } + } + + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + private RandomAccessFile CreateTempFile(String path,Connection _conn) { + String tempFileName = UUID.randomUUID().toString() + ".jpg"; + RandomAccessFile randomAccessFile = null; + try { + randomAccessFile = new RandomAccessFile(path + File.separator +tempFileName,"rw"); + randomAccessFile.setLength(_conn.getContentLength()); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return randomAccessFile; + } + + private boolean ChangeFileName(File _f,Connection _conn) { + String fileName = _conn.getURL().getFile().substring(url.lastIndexOf("/")); + return _f.renameTo(new File(_f.getAbsolutePath()+File.separator+fileName)); + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + public String getLocalPath() { + return localPath; + } + + public void setLocalPath(String localPath) { + this.localPath = localPath; + } + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/FileDownloaderTest.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..9f54c9ef47 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,58 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://static.oschina.net/uploads/img/201701/09170848_HsPK.jpg"; + + FileDownloader downloader = new FileDownloader(url); + downloader.setLocalPath("E:/temp_backup/temp"); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/Connection.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..598591b990 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,29 @@ +package com.coderising.download.api; + +import java.io.IOException; +import java.net.URL; + +public interface Connection { + + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public int read(byte data[]) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); + + public URL getURL(); + public int getStartPos(); + public int getEndPos(); +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/ConnectionException.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..95fb1d444d --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + private static final long serialVersionUID = 4776347926322882920L; +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/ConnectionManager.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..76024894f6 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,11 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url,int startPos ,int endPos) throws ConnectionException; + public Connection open(String url) throws ConnectionException; +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/DownloadListener.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..ee7bbd4fc2 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,87 @@ +package com.coderising.download.impl; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URLConnection uc = null; + private BufferedInputStream bs = null; + private URL url; + int startPos; + int endPos; + public ConnectionImpl(String path,int _startPos,int _endPos) throws Exception { + try { + if (startPos >= _endPos || _startPos < 0) { + throw new IllegalArgumentException(); + } + this.startPos = _startPos; + this.endPos = _endPos; + url = new URL(path); + uc = url.openConnection(); + uc.setRequestProperty("Range", "bytes=" + _startPos + "-" + _endPos); + bs = new BufferedInputStream(uc.getInputStream()); + } catch (MalformedURLException e) { + e.printStackTrace(); + throw e; + } catch (IOException e) { + e.printStackTrace(); + throw e; + } + } + public ConnectionImpl(String path) throws Exception { + try { + + url = new URL(path); + uc = url.openConnection(); + } catch (MalformedURLException e) { + e.printStackTrace(); + throw e; + } catch (IOException e) { + e.printStackTrace(); + throw e; + } + } + + public int read(byte data[]) { + int ret = 0; + try { + ret = bs.read(data); + } catch (IOException e) { + e.printStackTrace(); + } + return ret; + } + + public int getContentLength() { + return uc.getContentLength(); + } + + public URL getURL() { + return url; + } + + public void close() { + try { + if (null != bs) { + bs.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public int getStartPos() { + return this.startPos; + } + public int getEndPos() { + return this.endPos; + } + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..838820f189 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,42 @@ +package com.coderising.download.impl; + + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + public Connection open(String url,int startPos ,int endPos) throws ConnectionException { + if (null == url || "".equals(url)) { + throw new IllegalArgumentException("参数异常"); + } + + Connection conn = null; + try { + conn = new ConnectionImpl(url,startPos,endPos); + } catch (Exception e) { + e.printStackTrace(); + throw new ConnectionException(); + } + + return conn; + } + + public Connection open(String url) throws ConnectionException { + if (null == url || "".equals(url)) { + throw new IllegalArgumentException("参数异常"); + } + + Connection conn = null; + try { + conn = new ConnectionImpl(url); + } catch (Exception e) { + e.printStackTrace(); + throw new ConnectionException(); + } + + return conn; + } + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java index c168b5efa8..71ead0324f 100644 --- a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.concurrent.CyclicBarrier; + /** * @ClassName: ArrayList * @Description: 自增长数组 @@ -116,4 +118,5 @@ private boolean checkOutOfBounds() { return true; } } + } diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java index a824ad9372..1d8205dbed 100644 --- a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.concurrent.CyclicBarrier; + /** * @ClassName: LinkedList @@ -129,6 +131,219 @@ private static class Node{ public String toString() { return "{" + this.data +" |---}--->"; } + } + + + //数据结构习题 + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public boolean reverse(){ + if (this.size()<=0) { + return false; + } + Stack stack = new Stack(); + + while(this.size()>0) { + stack.push(this.removeFirst()); + } + while(stack.size()>0) { + this.addLast(stack.pop()); + } + + return true; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public boolean removeFirstHalf(){ + if (this.size()<=0) { + return false; + } + //计算中间位置 + int tempCount = this.size(); + tempCount = tempCount%2 == 0 ? tempCount/2 : (tempCount-1)/2; + tempCount--; + while(tempCount >= 0) { + this.removeFirst(); + tempCount --; + } + return true; } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(i<0|| i>=size) { + throw new IllegalArgumentException("下标超出链表范围!"); + } + if (!(length>0|| (i+1+length)<=this.size())) { + throw new IllegalArgumentException("参数非法!"); + } + + Node tempHead = head; + head = getNode(i-1); + while(length-->0){ + this.removeFirst(); + } + head = tempHead; + } + + /** + * 获取第i个元素的引用 + */ + public Node getNode(int index) { + if(index<0|| index>=size) { + throw new IllegalArgumentException("下标超出链表范围!"); + } + Node tempHead = head; + int i = 0; + while(i++ < index) { + tempHead = tempHead.next; + } + + return tempHead.next; + } + + + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public Integer[] getElements(LinkedList list){ + int listSizeB = list.size(); + int i=0; + Integer res[] = new Integer[listSizeB]; + while(listSizeB-- > 0) { + res[i] = (Integer) this.get((Integer)list.get(i)); + i++; + } + return res; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * @param list + */ + + public void subtract(LinkedList list){ + Node tempNodePre = this.head; + Node tempNode = this.head.next; + Node tempNodeB = list.head.next; + Node temp = null; + while(null != tempNode && null != tempNodeB) { + Integer a = (Integer) tempNode.data; + Integer b = (Integer) tempNodeB.data; + if (a < b) { + tempNodePre = tempNodePre.next; + tempNode = tempNode.next; + } else if(a > b){ + tempNodeB = tempNodeB.next; + }else { + temp = tempNode; + tempNodePre.next = tempNode.next; + tempNode = tempNode.next; + temp.next = null; + temp = null; + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + Node tempPre = this.head; + Node tempCur = this.head.next; + Node temp = null; + while(null != tempCur) { + Integer a = (Integer) tempPre.data; + Integer b = (Integer) tempCur.data; + if(a == b) { + temp = tempCur; + tempPre.next = tempCur.next; + tempCur = tempCur.next; + temp.next = null; + temp = null; + }else { + tempPre = tempPre.next; + tempCur = tempCur.next; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if (min<0 || max=min) { + preMin = pre; + } + if (a<=max && b>max) { + preMax = pre; + } + pre = pre.next; + cur = cur.next; + } + preMin.next = preMax.next; + preMax = null; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList listB){ + Node nodeA = this.head.next; + Node nodeB = listB.head.next; + LinkedList listC = new LinkedList(); + while (null != nodeA && null != nodeB) { + Integer a = (Integer) nodeA.data; + Integer b = (Integer) nodeB.data; + if (a>b) { + nodeB = nodeB.next; + } else if(a7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group04/844028312/four/min-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/group04/844028312/four/min-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..d2bed45b48 --- /dev/null +++ b/group04/844028312/four/min-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +import javax.annotation.Resources; + + + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + InputStream ips = null; + ByteArrayOutputStream bao = null; + try { + String name=className.replace(".", "\\")+".class"; + for(int i=0;i parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + return null; + } + +} diff --git a/group04/844028312/three/src/com/coderising/litestruts/StrutsTest.java b/group04/844028312/three/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group04/844028312/three/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/View.java b/group04/844028312/three/src/com/coderising/litestruts/View.java similarity index 100% rename from group15/1502_1617273078/src/com/coderising/litestruts/View.java rename to group04/844028312/three/src/com/coderising/litestruts/View.java diff --git a/group04/844028312/three/src/com/coding/basic/ArrayList.java b/group04/844028312/three/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group04/844028312/three/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group04/844028312/three/src/com/coding/basic/BinaryTreeNode.java b/group04/844028312/three/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/844028312/three/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/Iterator.java b/group04/844028312/three/src/com/coding/basic/Iterator.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/Iterator.java rename to group04/844028312/three/src/com/coding/basic/Iterator.java diff --git a/group04/844028312/three/src/com/coding/basic/LinkedList.java b/group04/844028312/three/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..df43a6dec7 --- /dev/null +++ b/group04/844028312/three/src/com/coding/basic/LinkedList.java @@ -0,0 +1,417 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size=0; + public void add(Object o){ + if(head==null){ + head =new Node(); + head.data=o; + last=head; + } + else{ + Node temp=new Node(); + temp.data=o; + last.next=temp; + last=temp; + } + size++; + } + public boolean enCapacity(int index){ + if(index>=0&&index7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node temp1=last; + for(int i=size-2;i>=0;i--){ + Node temp2=indexOf(i); + temp1.next=temp2; + temp1=temp2; + } + head.next=null; + temp1=head; + head=last; + last=temp1; + } + + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(size>1){ + Node index=indexOf(size/2-1); + index.next=null; + last=index; + size=size-size/2; + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){//1 2 3 4 5 + if( i=0){ + int len=length+i>size? size-i:length; + int j=0; + while(j0 && i>=0){ + Node before=indexOf(i-1); + Node after=indexOf(length+i); + if(before==null&&after==null){ + head=null; + last=null; + size=0; + } + else if(before==null&&after!=null){ + head=after; + size=size-length; + } + else if(before!=null&&after==null){ + before.next=null; + last=before; + size=size-length; + } + else{ + before.next=after; + size=size-length; + } + }*/ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(list==null){ + return null; + } + int size=list.size; + int jude=0; + int [] newInt=new int[size]; + while(jude0){ + int index=(int) list.get(jude); + if(index>=0&&index0){ + int index=(int) list.get(jude); + for(int i=0;imin){ + start=i; + } + if((int)temp.data>=max){ + end=i; + break; + } + i++; + temp=temp.next; + } + if(start==-1){ + start=0; + } + if(end==-1){ + end=size; + } + this.remove(start,end-start); + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + if(list==null){ + return null; + } + int i=0; + int j=0; + LinkedList c=new LinkedList(); + while(i(int)list.get(j)){ + j++; + } + else{ + i++; + } + } + return c; + } +} diff --git a/group04/844028312/three/src/com/coding/basic/LinkedListTest.java b/group04/844028312/three/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..0c965c90bd --- /dev/null +++ b/group04/844028312/three/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,145 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private LinkedList linkedList; + @Before + public void setUp() throws Exception { + linkedList=new LinkedList(); + for(int i=0;i<10;i++){ + linkedList.add(i); + } + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + + System.out.println(linkedList.size()); + } + + @Test + public void testAddIntObject() { + linkedList.add(10, "@"); + System.out.println(linkedList.size()); + } + + @Test + public void testGet() { + System.out.println(linkedList.get(100)); + } + + @Test + public void testRemoveInt() { + System.out.println(linkedList.remove(9)); + System.out.println(linkedList.size()); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + + @Test + public void testAddFirst() { + linkedList.addFirst("aa"); + System.out.println(linkedList.size()); + } + + @Test + public void testAddLast() { + linkedList.addLast("bb"); + System.out.println(linkedList.size()); + } + + @Test + public void testRemoveFirst() { + linkedList.removeFirst(); + System.out.println(linkedList.size()); + } + + @Test + public void testRemoveLast() { + linkedList.removeLast(); + System.out.println(linkedList.size()); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + + @Test + public void testReverse() { + linkedList.reverse(); + System.out.println(linkedList.size()); + } + + @Test + public void testRemoveFirstHalf() { + linkedList.removeFirstHalf(); + System.out.println(linkedList.size()); + } + + @Test + public void testRemoveIntInt() { + linkedList.remove(2, 5);//0 1 2 3 4 5 6 7 8 9 + System.out.println(linkedList.size()); + } + + @Test + public void testGetElements() { + LinkedList list=new LinkedList(); + list.add(1); + list.add(3); + list.add(2); + list.add(7); + int [] a=linkedList.getElements(list); + System.out.println(a); + + } + + @Test + public void testSubtract() { + LinkedList list=new LinkedList(); + list.add(1); + list.add(3); + list.add(2); + list.add(10); + linkedList.subtract(list); + System.out.println(linkedList); + } + + @Test + public void testRemoveDuplicateValues() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.removeDuplicateValues(); + System.out.println(linkedList); + } + + @Test + public void testRemoveRange() { + linkedList.removeRange(2, 5); + System.out.println(linkedList); + } + + @Test + public void testIntersection() { + LinkedList list=new LinkedList(); + list.add(5); + list.add(6); + LinkedList c=linkedList.intersection(list); + System.out.println(c); + } + +} diff --git a/group15/1502_1617273078/src/com/coding/basic/List.java b/group04/844028312/three/src/com/coding/basic/List.java similarity index 100% rename from group15/1502_1617273078/src/com/coding/basic/List.java rename to group04/844028312/three/src/com/coding/basic/List.java diff --git a/group04/844028312/three/src/com/coding/basic/Queue.java b/group04/844028312/three/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group04/844028312/three/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group04/844028312/three/src/com/coding/basic/Stack.java b/group04/844028312/three/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group04/844028312/three/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group04/844028312/two/src/com/coderising/array/ArrayUtil.java b/group04/844028312/two/src/com/coderising/array/ArrayUtil.java index 309610c6be..504c07640e 100644 --- a/group04/844028312/two/src/com/coderising/array/ArrayUtil.java +++ b/group04/844028312/two/src/com/coderising/array/ArrayUtil.java @@ -12,14 +12,16 @@ public class ArrayUtil { * @return */ public void reverseArray(int[] origin){ + if(origin==null){ + return; + } int size=origin.length; - if(size>1){ - for(int i=0;iarray2[j]){ + newArray[count++]=array2[j++]; + + } + if(array1[i]0) - System.arraycopy(array1, 0, newArray, 0, size1); - if(size2>0) - System.arraycopy(array2,0, newArray, size1, size2); - for(int i=0;inewArray[j]){ - int temp=min; - min=newArray[j]; - newArray[i]=min; - newArray[j]=temp; - } - } + while(j==size1&&i2){ - a=new int[10]; + a=new int[max]; int record=2; do{ a[0]=1; a[1]=1; - if(a.length>record) - a[record]=a[record-2]+a[record-1]; - else{ - a=grow(a,3); - a[record]=a[record-2]+a[record-1]; - } + a[record]=a[record-2]+a[record-1]; record++; }while(a[record-1]1;i--){ - if(n%i==0){ - isPrime=false; + if(max>=2){ + while(n1;i--){ + if(n%i==0){ + isPrime=false; + break; + } + } - } - if(isPrime){ - if(record=min){ - while(true){ - boolean isPerfect=false;//是否是完数的标志 - if(max<=min){ - break; - } - int n=(int) Math.sqrt(min); - int count=0; - for(int i=n;i>=1;i--){ + while(min0;i--){ if(min%i==0){ - count=count+i; - int b=min/i; - if(b!=min) - count=count+b; + sum=sum+i; } } - if(count==min){ - isPerfect=true; - } - if(isPerfect){ - if(record - + diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java b/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java index 0c9e702951..b4ac9cf2b9 100644 --- a/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java @@ -9,23 +9,30 @@ public class ArrayList implements List { private Object[] elementData = new Object[3]; public void add(Object o){ - add(size,o); + ensureCapacity(size + 1); + elementData[size] = o; + size++; } + public void add(int index, Object o){ if (index > size){ throw new IndexOutOfBoundsException(); } + // 扩容 - if (size == elementData.length || index + 1 > elementData.length) { - int newLength = index + 1 > size * 2 ? index + 1 :size * 2; - elementData = Arrays.copyOf(elementData, newLength); - } + ensureCapacity(size + 1); + // 移动元素 System.arraycopy(elementData,index,elementData,index + 1 ,size-index); elementData[index] = o; size ++ ; } - + + private void ensureCapacity(int minCapacity) { + int newLength = Math.max(minCapacity, size * 2); + elementData = Arrays.copyOf(elementData, newLength); + } + public Object get(int index){ checkIndex(index); return elementData[index]; @@ -55,17 +62,17 @@ public Iterator iterator(){ private class ArrayListIterator implements Iterator { - private int currentIndex = 0; + private int position = 0; @Override public boolean hasNext() { - return currentIndex < size(); + return position < size(); } @Override public Object next() { - Object o = get(currentIndex); - currentIndex ++ ; + Object o = get(position); + position++ ; return o; } } diff --git a/group04/916758663/learn03/src/main/java/com/example/download/DownloadThread.java b/group04/916758663/learn03/src/main/java/com/example/download/DownloadThread.java new file mode 100644 index 0000000000..6239bc13d2 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/DownloadThread.java @@ -0,0 +1,50 @@ +package com.example.download; + + +import com.example.download.api.Connection; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String file; + CountDownLatch latch; + + public DownloadThread( Connection conn, int startPos, int endPos,String file,CountDownLatch latch){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.file = file; + this.latch = latch; + } + public void run(){ + RandomAccessFile randomAccessFile = null; + try { + byte[] data = conn.read(startPos, endPos); + randomAccessFile = new RandomAccessFile(file, "rw"); + randomAccessFile.seek(startPos); + randomAccessFile.write(data); + latch.countDown(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + if (randomAccessFile != null) { + randomAccessFile.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + conn.close(); + } + } +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/FileDownloader.java b/group04/916758663/learn03/src/main/java/com/example/download/FileDownloader.java new file mode 100644 index 0000000000..743955e329 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/FileDownloader.java @@ -0,0 +1,76 @@ +package com.example.download; + + +import com.example.download.api.Connection; +import com.example.download.api.ConnectionException; +import com.example.download.api.ConnectionManager; +import com.example.download.api.DownloadListener; +import java.util.concurrent.CountDownLatch; + +public class FileDownloader { + + String url; + + String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + private CountDownLatch latch = new CountDownLatch(3); + + + public FileDownloader(String _url,String localFile) { + this.url = _url; + this.localFile = localFile; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + int[][] array = Utils.split(length, 3); + for (int i = 0; i < 3; i++) { + new DownloadThread(conn,array[i][0],array[i][1],localFile,latch).start(); + } + + latch.await(); + + this.getListener().notifyFinished(); + + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally{ + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/Utils.java b/group04/916758663/learn03/src/main/java/com/example/download/Utils.java new file mode 100644 index 0000000000..521583d7e5 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/Utils.java @@ -0,0 +1,24 @@ +package com.example.download; + +/** + * Created by qilei on 17/3/26. + */ +public class Utils { + + public static int[][] split(int len, int count) { + int[][] result = new int[count][2]; + int baseLen = (int)Math.ceil(((double)len / count)); + for (int i = 0; i < count; i++) { + int startPos = baseLen * i ; + int endPos = baseLen * (i + 1) -1; + if (i == count - 1) { + if (endPos > len - 1) { + endPos = len - 1; + } + } + result[i][0] = startPos; + result[i][1] = endPos; + } + return result; + } +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/api/Connection.java b/group04/916758663/learn03/src/main/java/com/example/download/api/Connection.java new file mode 100644 index 0000000000..d3906b1859 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.example.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/api/ConnectionException.java b/group04/916758663/learn03/src/main/java/com/example/download/api/ConnectionException.java new file mode 100644 index 0000000000..ffbd61ec31 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.example.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/api/ConnectionManager.java b/group04/916758663/learn03/src/main/java/com/example/download/api/ConnectionManager.java new file mode 100644 index 0000000000..1888a879ef --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.example.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/api/DownloadListener.java b/group04/916758663/learn03/src/main/java/com/example/download/api/DownloadListener.java new file mode 100644 index 0000000000..9cc73ddee6 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.example.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/impl/ConnectionImpl.java b/group04/916758663/learn03/src/main/java/com/example/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..62ee66f1c5 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/impl/ConnectionImpl.java @@ -0,0 +1,58 @@ +package com.example.download.impl; + +import com.example.download.api.Connection; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + + +public class ConnectionImpl implements Connection { + + private URL url; + + ConnectionImpl(String urlStr){ + try { + url = new URL(urlStr); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + URLConnection urlConnection = url.openConnection(); + urlConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + + endPos); + InputStream inputStream = urlConnection.getInputStream(); + int len = endPos + 1 - startPos; + int bytesRead = 0; + byte[] buffer = new byte[len]; + while (bytesRead < len) { + int result = inputStream.read(buffer, bytesRead, len - bytesRead); + if (result == -1){ + break; + } + bytesRead += result; + } + inputStream.close(); + return buffer; + } + + @Override + public int getContentLength() { + try { + URLConnection urlConnection = url.openConnection(); + return urlConnection.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + } + +} diff --git a/group04/916758663/learn03/src/main/java/com/example/download/impl/ConnectionManagerImpl.java b/group04/916758663/learn03/src/main/java/com/example/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..d346c4d350 --- /dev/null +++ b/group04/916758663/learn03/src/main/java/com/example/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package com.example.download.impl; + + +import com.example.download.api.Connection; +import com.example.download.api.ConnectionException; +import com.example.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection conn = new ConnectionImpl(url); + return conn; + } + +} diff --git a/group04/916758663/learn03/src/test/java/com/example/download/FileDownloaderTest.java b/group04/916758663/learn03/src/test/java/com/example/download/FileDownloaderTest.java new file mode 100644 index 0000000000..7dbeffd108 --- /dev/null +++ b/group04/916758663/learn03/src/test/java/com/example/download/FileDownloaderTest.java @@ -0,0 +1,57 @@ +package com.example.download; + + +import com.example.download.api.ConnectionManager; +import com.example.download.api.DownloadListener; +import com.example.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by qilei on 17/3/14. + */ +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + String url ="http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + String file = "/Users/qilei/tmp/tmp.jpg"; + FileDownloader downloader = new FileDownloader(url,file); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } + +} \ No newline at end of file diff --git a/group04/916758663/learn03/src/test/java/com/example/download/UtilsTest.java b/group04/916758663/learn03/src/test/java/com/example/download/UtilsTest.java new file mode 100644 index 0000000000..ff41d07731 --- /dev/null +++ b/group04/916758663/learn03/src/test/java/com/example/download/UtilsTest.java @@ -0,0 +1,31 @@ +package com.example.download; + +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +/** + * Created by qilei on 17/3/26. + */ +public class UtilsTest { + + @Test + public void testSplit(){ + int len = 10; + + int[][] result = Utils.split(10,3); + + assertThat(result[0][0]).isEqualTo(0); + assertThat(result[0][1]).isEqualTo(3); + assertThat(result[2][0]).isEqualTo(8); + assertThat(result[2][1]).isEqualTo(9); + } + + @Test + public void testMath(){ + double a = Math.ceil((double)10 / 3); + double b = Math.floor((double)10 / 3); + System.out.println(""); + + } + +} diff --git a/group04/916758663/learn03/src/test/java/com/example/download/impl/ConnectionImplTest.java b/group04/916758663/learn03/src/test/java/com/example/download/impl/ConnectionImplTest.java new file mode 100644 index 0000000000..7b21df4109 --- /dev/null +++ b/group04/916758663/learn03/src/test/java/com/example/download/impl/ConnectionImplTest.java @@ -0,0 +1,49 @@ +package com.example.download.impl; + +import static org.assertj.core.api.Assertions.*; + +import com.example.download.api.Connection; +import com.example.download.api.ConnectionException; +import com.example.download.api.ConnectionManager; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by qilei on 17/3/24. + */ +public class ConnectionImplTest { + + private Connection connection; + + @Before + public void setup(){ + ConnectionManager cm = new ConnectionManagerImpl(); + String url ="http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + try { + connection = cm.open(url); + } catch (ConnectionException e) { + e.printStackTrace(); + } + } + + @Test + public void read() throws Exception { + byte[] data = null; + data = connection.read(0, 35469); + assertThat(data.length).isEqualTo(35470); + + data = connection.read(0, 1023); + assertThat(data.length).isEqualTo(1024); + + data = connection.read(1024, 2023); + assertThat(data.length).isEqualTo(1000); + + } + + @Test + public void getContentLength() throws Exception { + int contentLength = connection.getContentLength(); + assertThat(contentLength).isEqualTo(35470); + } + +} \ No newline at end of file diff --git a/group04/916758663/minijvm/src/main/java/com/example/jvm/loader/ClassFileLoader.java b/group04/916758663/minijvm/src/main/java/com/example/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..cb860bf053 --- /dev/null +++ b/group04/916758663/minijvm/src/main/java/com/example/jvm/loader/ClassFileLoader.java @@ -0,0 +1,70 @@ +package com.example.jvm.loader; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + String filePartPath = className.replace(".", "/") + ".class"; + for (String clzPath : clzPaths) { + String filePath = clzPath + "/" + filePartPath; + File file = new File(filePath); + if (file.exists()) { + try { + FileInputStream inputStream = new FileInputStream(file); + int bytesRead = 0; + int len = inputStream.available(); + byte[] buffer = new byte[len]; + while (bytesRead < len) { + int result = inputStream.read(buffer, bytesRead, len - bytesRead); + if (result == -1){ + break; + } + bytesRead += result; + } + inputStream.close(); + return buffer; + } catch (FileNotFoundException e) { + e.printStackTrace(); + }catch (IOException e) { + e.printStackTrace(); + } + } + } + throw new RuntimeException("未找到类"); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + String result = ""; + StringBuilder sb = new StringBuilder(); + for(String path : clzPaths){ + sb.append(path + ";"); + } + result = sb.toString(); + if (result != "") { + result = result.substring(0, result.length() - 1); + } + return result; + } + + + + + +} diff --git a/group04/916758663/minijvm/src/test/java/com/example/jvm/loader/ClassFileLoaderTest.java b/group04/916758663/minijvm/src/test/java/com/example/jvm/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..04bb301b5e --- /dev/null +++ b/group04/916758663/minijvm/src/test/java/com/example/jvm/loader/ClassFileLoaderTest.java @@ -0,0 +1,78 @@ +package com.example.jvm.loader; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by qilei on 17/4/3. + */ +public class ClassFileLoaderTest { + + static String path1 = "/Users/qilei/idea/coding2017/coding2017/group04/916758663/minijvm/target/test-classes"; + static String path2 = "/Users/qilei/idea/coding2017/coding2017/group04/916758663/minijvm/target/classes"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.example.jvm.loader.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1054, byteCodes.length); + + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.example.jvm.loader.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws ClassNotFoundException, IOException { + if (clzPaths.size() == 0) { + return new byte[0]; + } + String actualPath = getActualPath(className); + + File f = new File(actualPath); + + if (!f.exists()) { + throw new ClassNotFoundException(actualPath); + } + + ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); + BufferedInputStream is = null; + try { + is = new BufferedInputStream(new FileInputStream(f)); + + byte[] buffer = new byte[1024]; + int len = 0; + + while (-1 != (len = is.read(buffer))) { + bos.write(buffer, 0, len); + } + + return bos.toByteArray(); + + } catch (IOException e) { + e.printStackTrace(); + throw e; + } finally { + is.close(); + bos.close(); + } + } + + private String getActualPath(String className) { + + String fileName = className.substring(className.lastIndexOf(".") + 1) + ".class"; + String dirPath = className.substring(0, className.lastIndexOf(".")).replace(".", "\\"); + + return clzPaths.get(clzPaths.size() - 1) + "\\" + dirPath + "\\" + fileName; //classPath 取最近添加的一个 + + } + + public void addClassPath(String path) { + + if (path == null) { + return; + } + + clzPaths.add(path); + + } + + public String getClassPath() { + + if (clzPaths.size() == 0) { + return ""; + } + + StringBuffer buffer = new StringBuffer(""); + + for (String str : clzPaths) { + buffer.append(str); + buffer.append(";"); + } + + return buffer.substring(0, buffer.length() - 1);// 去除最后一个分号 + + } + +} diff --git a/group05/1094051862/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group05/1094051862/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..287786b541 --- /dev/null +++ b/group05/1094051862/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,95 @@ +package com.coderising.jvm.test; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "E:\\practise\\group05\\1094051862\\mini-jvm\\bin"; + static String path2 = "C:\\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() throws ClassNotFoundException, IOException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber() throws ClassNotFoundException, IOException{ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + + } + + /** + * 二进制数组转换成16进制 + * @param codes + * @return + */ + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i 0) { + accessNormally(pageNum); + capacity --; + } else { + Node node = first; + while(node != null) { + + if (node.pageNum == pageNum) { + accessNodeExisting(node); + return; + } + + node = node.next; + } + + accessNormally(pageNum); + + removeLast(); + } + + } + + private void accessNodeExisting(Node node) { + if (node.next == null) { //最后一个元素为要添加的元素 + removeLast(); + exchangeFirstWithNode(node); + } else if (node.prev != null) { //要添加的元素在中间 + Node n = node.next; + Node p = node.prev; + p.next = n; + n.prev = p; + + exchangeFirstWithNode(node); + } + } + + private void exchangeFirstWithNode(Node node) { + node.next = first; + first.prev = node; + node.prev = null; //忘记这个就会导致找不到第一个添加的元素,测试出现堆溢出 + first = node; + } + + private void removeLast() { + last = last.prev; + last.next = null; + } + + private void accessNormally(int pageNum) { + + Node temp = first; + first = new Node(); + first.pageNum = pageNum; + first.next = temp; + temp.prev = first; + + } + + private void accessFirst(int pageNum) { + + first = new Node(); + first.pageNum = pageNum; + last = first; + capacity --; + + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + + } + return buffer.toString(); + } + +} diff --git a/group05/1094051862/test01/src/com/coding/lru/LRUPageFrameTest.java b/group05/1094051862/test01/src/com/coding/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..55bf75ec41 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/lru/LRUPageFrameTest.java @@ -0,0 +1,38 @@ +package com.coding.lru; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + System.out.println(frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + System.out.println(frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + System.out.println(frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + System.out.println(frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + System.out.println(frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + System.out.println(frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + System.out.println(frame.toString()); + } + +} diff --git a/group05/284422826/src/com/coderising/jvm/loader/ClassFileLoader.java b/group05/284422826/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..a15237f0cb --- /dev/null +++ b/group05/284422826/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,46 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList<>(); + + public byte[] readBinaryCode(String className) { + String name = this.getClassPath() + File.separatorChar + className.replace('.', File.separatorChar) + ".class"; + File file = new File(name); + byte[] bytes = new byte[(int)file.length()]; + try { + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + while (bis.read(bytes) != -1) { + System.out.println(Arrays.toString(bytes)); + } + } catch (IOException e) { + e.printStackTrace(); + } + return bytes; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + public String getClassPath() { + StringBuilder path = new StringBuilder(); + for (String str : clzPaths) { + path.append(str).append(";"); + } + return path.substring(0, path.length() - 1); + } + + +} diff --git a/group05/284422826/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group05/284422826/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..ca669de57a --- /dev/null +++ b/group05/284422826/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,74 @@ +package com.coderising.jvm.test; + +import com.coderising.jvm.loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + static String path1 = "D:\\git\\coding2017\\group05\\284422826\\bin"; + static String path2 = "C:\\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes ){ + StringBuilder buffer = new StringBuilder(); + for (byte b : codes) { + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} diff --git a/group05/284422826/src/com/coderising/jvm/test/EmployeeV1.java b/group05/284422826/src/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..12e3d7efdd --- /dev/null +++ b/group05/284422826/src/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group05/284422826/src/com/coding2017/basic/LinkedList.java b/group05/284422826/src/com/coding2017/basic/LinkedList.java deleted file mode 100644 index fbc92ff474..0000000000 --- a/group05/284422826/src/com/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,321 +0,0 @@ -package com.coding2017.basic; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -/** - * 功能:实现LinkedList. - * - * @author zhanglifeng. - */ -public class LinkedList implements List { - private Node head, tail; - private int size; - - private Node getNodeByIndex(int index) { - if (index < 0 || index > size - 1) { - throw new IndexOutOfBoundsException("线性表索引越界"); - } - Node current = head; - for (int i = 0; i < size && current != null; i++, current = current.next) { - if (i == index) { - return current; - } - } - return null; - } - - public void add(Object o) { - addLast(o); - } - - public void add(int index, Object o) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("线性表索引越界"); - } - - if (0 == index) { - addFirst(o); - } else { - Node node = getNodeByIndex(index - 1); - node.next = new Node(o, node.next); - size++; - } - } - - public Object get(int index) { - return getNodeByIndex(index).data; - } - - public Object remove(int index) { - if (index < 0 || index > size - 1) { - throw new IndexOutOfBoundsException("线性表索引越界"); - } - - if (0 == index) { - return removeFirst(); - } else if (size - 1 == index) { - return removeLast(); - } else { - Node node = getNodeByIndex(index); - Node preNode = getNodeByIndex(index - 1); - preNode.next = node.next; - size--; - return node.data; - } - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - Node currentHead = head; - Node newNode = new Node(o, currentHead); - head = newNode; - if (currentHead == null) { - tail = newNode; - } - - size++; - } - - public void addLast(Object o) { - Node currentTail = tail; - Node newNode = new Node(o, null); - tail = newNode; - if (currentTail == null) { - head = newNode; - } else { - currentTail.next = newNode; - } - size++; - } - - public Object removeFirst() { - if (head == null) { - throw new NoSuchElementException(); - } - Node node = new Node(head.data, null); - head = head.next; - size--; - return node.data; - } - - public Object removeLast() { - if (tail == null) { - throw new NoSuchElementException(); - } - Node node = getNodeByIndex(size - 1); - node.next = null; - size--; - return node.data; - } - - public Iterator iterator() { - return new LinkedListIterator(this); - } - - private static class Node { - Object data; - Node next; - - public Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - private class LinkedListIterator implements Iterator { - LinkedList linkedList = null; - private int current = 0; - - public LinkedListIterator(LinkedList linkedList) { - this.linkedList = linkedList; - } - - @Override - public boolean hasNext() { - return current < size; - } - - @Override - public Object next() { - return linkedList.get(current++); - } - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse() { - Node first = head; - Node reverse = null; - while (first != null) { - Node second = first.next; - first.next = reverse; - reverse = first; - first = second; - } - head = reverse; - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf() { - for (int i = 0; i < size / 2; i++) { - remove(i); - } - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * - * @param i - * @param length - */ - public void remove(int i, int length) { - if (size < i + length) { - throw new IndexOutOfBoundsException("线性表索引越界"); - } - for (int j = i + length - 1; j >= i; j--) { - remove(j); - } - } - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * - * @param list - */ - public int[] getElements(LinkedList list) { - if (list.size() > size) { - throw new IndexOutOfBoundsException("线性表索引越界"); - } - int[] array = new int[list.size]; - for (int i = 0; i < array.length; i++) { - int element = (int) list.get(i); - if (element >= size) { - throw new IndexOutOfBoundsException("线性表索引越界"); - } - array[i] = ((Integer) get(element)); - } - - System.out.println(Arrays.toString(array)); - - return array; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - * - * @param list - */ - - public void subtract(LinkedList list) { - int length = list.size(); - for (int i = size - 1; i >= 0; i--) { - for (int j = 0; j < length; j++) { - if (get(i) == list.get(j)) { - remove(i); - break; - } - } - } - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues() { - for (int i = size - 1; i > 0; i--) { - if (get(i) == get(i - 1)) { - remove(i); - } - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * - * @param min - * @param max - */ - public void removeRange(int min, int max) { - for (int i = size - 1; i >= 0; i--) { - int element = ((int) get(i)); - if ((element > min) && element < max) { - remove(i); - } - } - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * - * @param list - */ - public LinkedList intersection(LinkedList list) { - LinkedList newList = new LinkedList(); - int length = list.size(); - for (int i = 0; i < size; i++) { - for (int j = 0; j < length; j++) { - if (get(i) == list.get(j)) { - newList.add(get(i)); - break; - } - } - } - - Iterator it = newList.iterator(); - while (it.hasNext()) { - System.out.print(it.next() + " "); - } - System.out.println(); - return newList; - } - - public static void main(String[] args) { - LinkedList linkedList = new LinkedList(); - linkedList.add(1); - linkedList.add(2); - linkedList.add(3); - linkedList.addFirst(0); - linkedList.addLast(4); - - /*System.out.println("第3个元素:" + linkedList.get(3)); - - System.out.println(linkedList.removeFirst()); - System.out.println(linkedList.size()); - System.out.println("Last element:" + linkedList.removeLast()); - System.out.println(linkedList.size()); - System.out.println("第2个元素:" + linkedList.remove(2));*/ - System.out.println(linkedList.size()); - - //linkedList.remove(0, 3); - LinkedList list = new LinkedList(); - list.add(1); - list.add(3); - list.add(4); - //linkedList.getElements(list); - //linkedList.intersection(list); - linkedList.reverse(); - Iterator it = linkedList.iterator(); - while (it.hasNext()) { - System.out.print(it.next() + " "); - } - System.out.println(); - } -} diff --git a/group05/284422826/src/com/coding2017/basic/Queue.java b/group05/284422826/src/com/coding2017/basic/Queue.java index 57d63f43bf..0148cc7c38 100644 --- a/group05/284422826/src/com/coding2017/basic/Queue.java +++ b/group05/284422826/src/com/coding2017/basic/Queue.java @@ -1,5 +1,7 @@ package com.coding2017.basic; +import com.coding2017.basic.linklist.LinkedList; + import java.util.EmptyStackException; public class Queue { diff --git a/group05/284422826/src/com/coding2017/basic/ArrayList.java b/group05/284422826/src/com/coding2017/basic/array/ArrayList.java similarity index 100% rename from group05/284422826/src/com/coding2017/basic/ArrayList.java rename to group05/284422826/src/com/coding2017/basic/array/ArrayList.java diff --git a/group05/284422826/src/com/coderising/array/ArrayUtil.java b/group05/284422826/src/com/coding2017/basic/array/ArrayUtil.java similarity index 100% rename from group05/284422826/src/com/coderising/array/ArrayUtil.java rename to group05/284422826/src/com/coding2017/basic/array/ArrayUtil.java diff --git a/group05/284422826/src/com/coderising/array/ArrayUtilTest.java b/group05/284422826/src/com/coding2017/basic/array/ArrayUtilTest.java similarity index 100% rename from group05/284422826/src/com/coderising/array/ArrayUtilTest.java rename to group05/284422826/src/com/coding2017/basic/array/ArrayUtilTest.java diff --git a/group05/284422826/src/com/coding2017/basic/linklist/LRUPageFrame.java b/group05/284422826/src/com/coding2017/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..d43bbc13d7 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/linklist/LRUPageFrame.java @@ -0,0 +1,113 @@ +package com.coding2017.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin + */ +public class LRUPageFrame { + private static class Node { + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + + private int size = 0; + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if (size < capacity) { + Node node = new Node(); + node.pageNum = pageNum; + if (first == null && last == null) { + node.prev = null; + node.next = null; + first = node; + last = node; + } else { + if (last.prev == null) { + last.prev = node; + node.next = last; + } else { + assert first != null; + first.prev = node; + node.next = first; + } + node.prev = null; + first = node; + } + size++; + } else { + Node node = last; + while (node != null) { + if (pageNum == node.pageNum) { + Node temp = node; + if(node == last){ + last = last.prev; + last.prev = temp.prev.prev; + last.next = null; + }else if(node == first){ + first = temp.next; + first.prev = null; + first.next = temp.next.next; + }else{ + node.next.prev = temp.prev; + node.prev.next = temp.next; + } + temp = null; + break; + } + node = node.prev; + } + + if(node == null){ + Node temp = last; + last = last.prev; + last.prev = temp.prev.prev; + last.next = null; + temp = null; + } + + Node newNode = new Node(); + newNode.pageNum = pageNum; + first.prev = newNode; + newNode.prev = null; + newNode.next = first; + first = newNode; + + } + + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group05/284422826/src/com/coding2017/basic/linklist/LRUPageFrameTest.java b/group05/284422826/src/com/coding2017/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7e0e79767c --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding2017.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group05/284422826/src/com/coding2017/basic/linklist/LinkedList.java b/group05/284422826/src/com/coding2017/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..e1dce5568c --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/linklist/LinkedList.java @@ -0,0 +1,324 @@ +package com.coding2017.basic.linklist; + +import com.coding2017.basic.Iterator; +import com.coding2017.basic.List; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * 功能:实现LinkedList. + * + * @author zhanglifeng. + */ +public class LinkedList implements List { + private Node head, tail; + private int size; + + private Node getNodeByIndex(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + Node current = head; + for (int i = 0; i < size && current != null; i++, current = current.next) { + if (i == index) { + return current; + } + } + return null; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if (0 == index) { + addFirst(o); + } else { + Node node = getNodeByIndex(index - 1); + node.next = new Node(o, node.next); + size++; + } + } + + public Object get(int index) { + return getNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if (0 == index) { + return removeFirst(); + } else if (size - 1 == index) { + return removeLast(); + } else { + Node node = getNodeByIndex(index); + Node preNode = getNodeByIndex(index - 1); + preNode.next = node.next; + size--; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node currentHead = head; + Node newNode = new Node(o, currentHead); + head = newNode; + if (currentHead == null) { + tail = newNode; + } + + size++; + } + + public void addLast(Object o) { + Node currentTail = tail; + Node newNode = new Node(o, null); + tail = newNode; + if (currentTail == null) { + head = newNode; + } else { + currentTail.next = newNode; + } + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node node = new Node(head.data, null); + head = head.next; + size--; + return node.data; + } + + public Object removeLast() { + if (tail == null) { + throw new NoSuchElementException(); + } + Node node = getNodeByIndex(size - 1); + node.next = null; + size--; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedList = null; + private int current = 0; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + return linkedList.get(current++); + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node first = head; + Node reverse = null; + while (first != null) { + Node second = first.next; + first.next = reverse; + reverse = first; + first = second; + } + head = reverse; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + for (int i = 0; i < size / 2; i++) { + remove(i); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (size < i + length) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + for (int j = i + length - 1; j >= i; j--) { + remove(j); + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + if (list.size() > size) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + int[] array = new int[list.size]; + for (int i = 0; i < array.length; i++) { + int element = (int) list.get(i); + if (element >= size) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + array[i] = ((Integer) get(element)); + } + + System.out.println(Arrays.toString(array)); + + return array; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + int length = list.size(); + for (int i = size - 1; i >= 0; i--) { + for (int j = 0; j < length; j++) { + if (get(i) == list.get(j)) { + remove(i); + break; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + for (int i = size - 1; i > 0; i--) { + if (get(i) == get(i - 1)) { + remove(i); + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + for (int i = size - 1; i >= 0; i--) { + int element = ((int) get(i)); + if ((element > min) && element < max) { + remove(i); + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList newList = new LinkedList(); + int length = list.size(); + for (int i = 0; i < size; i++) { + for (int j = 0; j < length; j++) { + if (get(i) == list.get(j)) { + newList.add(get(i)); + break; + } + } + } + + Iterator it = newList.iterator(); + while (it.hasNext()) { + System.out.print(it.next() + " "); + } + System.out.println(); + return newList; + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.addFirst(0); + linkedList.addLast(4); + + /*System.out.println("第3个元素:" + linkedList.get(3)); + + System.out.println(linkedList.removeFirst()); + System.out.println(linkedList.size()); + System.out.println("Last element:" + linkedList.removeLast()); + System.out.println(linkedList.size()); + System.out.println("第2个元素:" + linkedList.remove(2));*/ + System.out.println(linkedList.size()); + + //linkedList.remove(0, 3); + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + //linkedList.getElements(list); + //linkedList.intersection(list); + linkedList.reverse(); + Iterator it = linkedList.iterator(); + while (it.hasNext()) { + System.out.print(it.next() + " "); + } + System.out.println(); + } +} diff --git a/group05/289326186/src/com/coderising/download/DownloadThread.java b/group05/289326186/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..9690e741a2 --- /dev/null +++ b/group05/289326186/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,35 @@ +package com.coderising.download; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + try { + byte[] b = conn.read(startPos, endPos); + String path = "D:" + File.separator + "test"+File.separator+"123.jpg"; + RandomAccessFile f = new RandomAccessFile(path, "rw"); + f.seek(startPos); + f.write(b); + f.close(); + System.out.println(Thread.currentThread().getName()+"线程下载完毕"); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/group05/289326186/src/com/coderising/download/FileDownloader.java b/group05/289326186/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..d8a37c3abe --- /dev/null +++ b/group05/289326186/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,104 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + int threadCount = 3; + + private DownloadThread[] threads = new DownloadThread[threadCount]; + + int length = 0;//文件总长度 + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + length = conn.getContentLength(); + System.out.println("length:"+length); + + //计算每个线程需要下载的文件大小 + int perLen = 0; + int lastLen = 0; + int tail = length % threadCount; + perLen = length/threadCount; + if(tail == 0){ + lastLen = perLen; + }else{ + lastLen = perLen + tail; + } + for(int i=0; i parameters) String key = entry.getKey(); if(pd.getName().equals(key)){ Method setMethod = pd.getWriteMethod();//获得set方法 - setMethod .invoke(obj, entry.getValue());//调用 + setMethod.invoke(obj, entry.getValue());//调用 break; } } @@ -65,23 +65,14 @@ public static View runAction(String actionName, Map parameters) Method method = clazz.getMethod("execute", null); Object result = method.invoke(obj); Map map = new HashMap(); - if("success".equals(result)){ - map.put("message", "login successful"); - }else{ - map.put("message", "login failed,please check your user/pwd"); - } // 3. 通过反射找到对象的所有getter方法(例如 getMessage), // 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , // 放到View对象的parameters for(PropertyDescriptor pd : pds){ - for (Entry entry : parameters.entrySet()) { - String key = entry.getKey(); - if(pd.getName().equals(key)){ - Method getMethod = pd.getReadMethod();//获得get方法 - Object getresult = getMethod .invoke(obj);//调用 - map.put(pd.getName(), getresult.toString()); - break; - } + Method getMethod = pd.getReadMethod();//获得get方法 + Object getresult = getMethod.invoke(obj);//调用 + if(!"class".equals(pd.getName())){ + map.put(pd.getName(), getresult.toString()); } } view.setParameters(map); diff --git a/group06/1378560653/.classpath b/group06/1378560653/.classpath index 3e0fb272a8..036cc56d25 100644 --- a/group06/1378560653/.classpath +++ b/group06/1378560653/.classpath @@ -3,5 +3,6 @@ + diff --git a/group06/1378560653/article.txt b/group06/1378560653/article.txt index 478dde01b7..42b64998ea 100644 --- a/group06/1378560653/article.txt +++ b/group06/1378560653/article.txt @@ -1,3 +1,5 @@ һƪ£http://blog.csdn.net/raymond120/article/details/57415472 ڶƪ£http://blog.csdn.net/raymond120/article/details/58043040 -ƪ£http://blog.csdn.net/raymond120/article/details/60759278 \ No newline at end of file +ƪ£http://blog.csdn.net/raymond120/article/details/60759278 +ƪ£http://blog.csdn.net/raymond120/article/details/61937892 +ƪ£http://blog.csdn.net/raymond120/article/details/68665071 \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/array/ArrayUtil.java b/group06/1378560653/src/com/coderising/array/ArrayUtil.java deleted file mode 100644 index 2542d4336d..0000000000 --- a/group06/1378560653/src/com/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,212 +0,0 @@ -package com.coderising.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - //一定要判断边界条件 - if(origin == null || origin.length == 0){ - return; - } - int N = origin.length; - for(int i = 0; i < N/2; i++){ - int temp = origin[i]; - origin[i] = origin[N-i-1]; - origin[N-i-1] = temp; - } - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - int N = oldArray.length; - int[] newArray = new int[N]; - int j = 0; - for(int i = 0; i < N; i++){ - if(oldArray[i] != 0){ - newArray[j] = oldArray[i]; - j++; - } - } - return newArray; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - int N = array1.length + array2.length; - int[] array3 = new int[N]; - System.arraycopy(array1, 0, array3, 0, array1.length); - for(int i = 0; i < N; i++){ - for(int j = 0; j < array2.length; j++){ - if(array3[i] > array2[j]){ - System.arraycopy(array3, i , array3, i+1, N-i-1); - array3[i] = array2[j]; - } - } - } - return array3; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - int[] newArray = new int[oldArray.length + size]; - System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - int[] zero = new int[0]; - int[] array = new int[100]; - array[0] = 1; - array[1] = 1; - int i = 1; - - if(max == 1){ - return zero; - }else{ - while(array[i] <= max){ - i++; - if(i > array.length){ - grow(array, i*2); - } - array[i+1] = array[i] + array[i-1]; - } - return array; - } - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - boolean[] prime = new boolean[max + 1]; - int[] zero = new int[0]; - int[] array = new int[max]; - int q = 1; - if(max == 1 || max ==2){ - return zero; - }else{ - for(int i = 3; i < max; i++) - if(i % 2 == 0){ - prime[i] = false; - }else{ - prime[i] = true; - } - - for(int i = 3; i < Math.sqrt(max); i++){//因子;若n是合数,则其所有因子都不超过sqrt(n) - if(prime[i]) - for(int j = 2 * i; j<= max; j += i){//其倍数 - prime[j] = false; - } - } - array[0] = 2; - for(int p = 0; p < max; p++){ - if(prime[p] == true){ - array[q] = p; - q++; - } - } - return array; - } - } - /*int[] zero = new int[0]; - int[] array = new int[100]; - int k = 0; - - if(max == 1 || max == 2){ - return zero; - }else{ - for(int n = 2; n <= max; n++){ - int isPrime = 1; - for(int i = 2; i < n; i++){ - if(n % i == 0){ - isPrime = 0; - break; - } - if(isPrime == 1){ - array[k] = n; - k++; - } - } - } - return array; - }*/ - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - int[] perfectNumbers = new int[max]; - int n = 0; - for(int i = 1; i < max; i++){//i:要判断的数 - int sum = 0; - for(int j = 1; j < i; j++){//j:可能因子 - if(i % j == 0){ - sum += j; - } - } - if(sum == i){ - perfectNumbers[n] = i; - n++; - } - } - return perfectNumbers; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - String s = ""; - for(int i = 0; i < array.length; i++){ - s = s + array[i] + seperator; - } - return s; - } - - -} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/download/DownloadThread.java b/group06/1378560653/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..fd0fc27001 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,40 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String localFile; + CyclicBarrier barrier; + + public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + public void run(){ + + try { + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + byte[] data = conn.read(startPos, endPos); + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + file.seek(startPos); + file.write(data); + file.close(); + conn.close(); + barrier.await();//等待别的线程完成 + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/download/FileDownloader.java b/group06/1378560653/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..5a260a365b --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,131 @@ +package com.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +public class FileDownloader { + + private String url; + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int DOWNLOAD_THREAD_NUM = 3; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + /* + * CyclicBarrier类的用法: + * 当多个线程需要互相等待,直到所有线程跨过某个“屏障”的时候,用这个类 + */ + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM, new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }) ; + + Connection conn = null; + + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + createPlaceHolderFile(this.localFile, length); //占位子 + + int[][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM, length);//分配每个线程的下载长度 + + for(int i = 0; i < DOWNLOAD_THREAD_NUM;i++){ + + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + thread.start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + } + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + int[][] ranges = new int[threadNum][2]; + + int eachThreadSize = contentLen / threadNum; + int left = contentLen % threadNum; + + for(int i = 0; i < threadNum; i++){ + int startPos = i * threadNum; + + int endPos = (i + 1) * eachThreadSize - 1; + + if((i == (threadNum - 1))){ + endPos += left; + } + ranges[i][0] = startPos; + ranges[i][1] = endPos; + } + return ranges; + } + + private void createPlaceHolderFile(String fileName, int contentLen) throws IOException { + + RandomAccessFile file = new RandomAccessFile(fileName, "rw");//以读写方式 + + for(int i = 0; i < contentLen; i++){ + file.write(0); + } + + file.close(); + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/download/api/Connection.java b/group06/1378560653/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group06/1378560653/src/com/coderising/download/api/ConnectionException.java b/group06/1378560653/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..505f61e224 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,9 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + + public ConnectionException(Exception e) { + super(e); + } + +} diff --git a/group06/1378560653/src/com/coderising/download/api/ConnectionManager.java b/group06/1378560653/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..0a625bf472 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; //得到一个Connection的实例 +} diff --git a/group06/1378560653/src/com/coderising/download/api/DownloadListener.java b/group06/1378560653/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group06/1378560653/src/com/coderising/download/impl/ConnectionImpl.java b/group06/1378560653/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..643984ee73 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,96 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; + + +//非public class,仅包内可见,依靠ConnectionMangerImpl实现 +class ConnectionImpl implements Connection { + + URL url; + static final int BUFFER_SIZE = 1024; + + ConnectionImpl(String _url) throws ConnectionException { + try { + this.url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } + } + + /* + * 分段读取数据 + * @see com.coderising.download.api.Connection#read(int, int) + */ + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + /* + * HttpURLConnection是基于HTTP协议的,HttpURLConnection的对象不能直接构造,需要通过url.openConnection()来获得HttpURLConnection对象 + * 示例如下: + * String slurl = "http://...."; + * URL url = new URL(slurl); + * HttpURLConnection httpCoon = (HttpURLConnection) url.openConnection(); + * 调用HttpURLConnection连接对象的getInputStream()函数,将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端 + */ + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setRequestProperty("Range","bytes=" + startPos + "-" + endPos); //只发一个特定的片段,比is.skip有效 + InputStream is = httpConn.getInputStream();// 注意,实际发送请求的代码段就在这里 -------------------------------输入流 + + //is.skip(startPos);//跳过startPos之前的内容 + + byte[] buff = new byte[BUFFER_SIZE]; + int totalLen = endPos - startPos + 1; //注意+1 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节数组流, 可以捕获内存缓冲区的数据,转换成字节数组。----输出流 + + while(baos.size() < totalLen){ + + int len = is.read(buff);//从输入流中读取数据到buff数组,同时返回读取长度,每次读取量小于等于1024 + if (len < 0){ + break; + } + baos.write(buff, 0, len);//buff数组中的数据写入输出流 + } + + if(baos.size() > totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + /* + * 获取资源长度 + * @see com.coderising.download.api.Connection#getContentLength() + */ + @Override + public int getContentLength() { + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group06/1378560653/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group06/1378560653/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..bdd7d3f8e9 --- /dev/null +++ b/group06/1378560653/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group06/1378560653/src/com/coderising/jvm/loader/ClassFileLoader.java b/group06/1378560653/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..ff832c8dde --- /dev/null +++ b/group06/1378560653/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,61 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + String classDirName = className.replace('.', '\\')+".class"; + String clzpath = getClassPath()+"\\"+ classDirName; + try { + FileInputStream clz = new FileInputStream(clzpath); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + int flag = 0; + while((flag = clz.read())!=-1){ + baos.write(flag); + } + clz.close(); + return baos.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + + public void addClassPath(String path) { + File file = new File(path); + if(file.exists()){ + clzPaths.add(path); + } else { + throw new IllegalArgumentException("路径:"+path+"不存在"); + } + } + + + public String getClassPath(){ + if(clzPaths.isEmpty()){ + return " "; + } + + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < clzPaths.size(); i++) { + buf.append(clzPaths.get(i)); + if(i != (clzPaths.size() - 1)){ + buf.append(";"); + } + } + return buf.toString(); + } + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group06/1378560653/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..3ace0974f5 --- /dev/null +++ b/group06/1378560653/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,85 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + +public class ClassFileloaderTest { + + + static String path1 = "H:\\github\\coding2017\\group06\\1378560653\\bin"; + static String path2 = "C:\\Temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i actions = new HashMap<>(); + + public Configuration(String fileName) { + + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is){ + + SAXBuilder builder = new SAXBuilder(); + + try { + + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for(Element actionElement : root.getChildren("action")){ + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for(Element resultElement : actionElement.getChildren("result")){ + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + + } + + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig{ + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } + +} diff --git a/group06/1378560653/src/com/coderising/litestruts/ConfigurationException.java b/group06/1378560653/src/com/coderising/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..97e286827f --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.coderising.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group06/1378560653/src/com/coderising/litestruts/ConfigurationTest.java b/group06/1378560653/src/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..734649f37a --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,50 @@ +package com.coderising.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ConfigurationTest { + + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView(){ + String jsp = cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } + +} diff --git a/group06/1378560653/src/com/coderising/litestruts/LoginAction.java b/group06/1378560653/src/com/coderising/litestruts/LoginAction.java index 76547ac3b3..52a9b71cfb 100644 --- a/group06/1378560653/src/com/coderising/litestruts/LoginAction.java +++ b/group06/1378560653/src/com/coderising/litestruts/LoginAction.java @@ -2,7 +2,7 @@ /** * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin + * @author * */ public class LoginAction{ @@ -36,4 +36,4 @@ public void setPassword(String password){ public String getMessage(){ return this.message; } -} \ No newline at end of file +} diff --git a/group06/1378560653/src/com/coderising/litestruts/ReflectionUtil.java b/group06/1378560653/src/com/coderising/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..dda2eec6dd --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/ReflectionUtil.java @@ -0,0 +1,123 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for(String name : params.keySet() ){ + + String methodName = "set" + name; + + for(Method m: methods){ + + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + private static List getMethods(Class clz, String startWithName){ + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith(startWithName)){ + + methods.add(m); + + } + + } + + return methods; + } + + public static Map getParameterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for(Method m : methods){ + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + + return params; + } + + ////////////////////////Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("get")){ + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("set")){ + + methods.add(m); + + } + + } + + return methods; + + } + + + + +} diff --git a/group06/1378560653/src/com/coderising/litestruts/ReflectionUtilTest.java b/group06/1378560653/src/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..af378808c6 --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,111 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception{ + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + ReflectionUtil.setParameters(o,params); + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + + } + + @Test + public void testGetGetterMethod() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParameters() throws Exception { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + + LoginAction action = (LoginAction) clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + Map params = ReflectionUtil.getParameterMap(action); + + Assert.assertEquals(3, params.size()); + + + Assert.assertEquals(null, params.get("message")); + Assert.assertEquals("test", params.get("name")); + Assert.assertEquals("123456", params.get("password")); + } + +} diff --git a/group06/1378560653/src/com/coderising/litestruts/StructsTest.java b/group06/1378560653/src/com/coderising/litestruts/StructsTest.java deleted file mode 100644 index 4e761b0b2d..0000000000 --- a/group06/1378560653/src/com/coderising/litestruts/StructsTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - -public class StructsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/Struts.java b/group06/1378560653/src/com/coderising/litestruts/Struts.java index 0e73017f99..885c604940 100644 --- a/group06/1378560653/src/com/coderising/litestruts/Struts.java +++ b/group06/1378560653/src/com/coderising/litestruts/Struts.java @@ -1,31 +1,26 @@ package com.coderising.litestruts; -import java.io.IOException; +import java.lang.reflect.Method; import java.util.Map; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; public class Struts { + private final static Configuration cfg = new Configuration("struts.xml"); + public static View runAction(String actionName, Map parameters) { /* 0. 读取配置文件struts.xml - 1. 根据actionName找到相对应的class , 例如LoginAction,通过反射实例化(创建对象) + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") , 那就应该调用 setName和setPassword方法 - 2. 通过反射调用对象的exectue方法, 并获得返回值,例如"success" + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , @@ -35,22 +30,37 @@ public static View runAction(String actionName, Map parameters) { 放到View对象的jsp字段中。 */ + + String clzName = cfg.getClassName(actionName); + + if(clzName == null){ + return null; + } - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { - DocumentBuilder db = dbf.newDocumentBuilder(); - Document document = db.parse("structs.xml"); - NodeList actionList = document.getElementsByTagName("action"); + + Class clz = Class.forName(clzName); + Object action = clz.newInstance(); + + ReflectionUtil.setParameters(action, parameters); + + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String)m.invoke(action); + + Map params = ReflectionUtil.getParameterMap(action); + String resultView = cfg.getResultView(actionName, resultName); + View view = new View(); + view.setParameters(params); + view.setJsp(resultView); + return view; + + + + } catch (Exception e) {//得处理这个异常 - } catch (ParserConfigurationException e) { - e.printStackTrace(); - } catch (SAXException e) { - e.printStackTrace(); - } catch (IOException e) { e.printStackTrace(); } - return null; } -} \ No newline at end of file +} diff --git a/group06/1378560653/src/com/coderising/litestruts/StrutsTest.java b/group06/1378560653/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44021cacc --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group06/1378560653/src/com/coderising/litestruts/structs.xml b/group06/1378560653/src/com/coderising/litestruts/structs.xml deleted file mode 100644 index dd598a3664..0000000000 --- a/group06/1378560653/src/com/coderising/litestruts/structs.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group06/1378560653/src/com/coding/basic/ArrayList.java b/group06/1378560653/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 8d9d0c30fb..0000000000 --- a/group06/1378560653/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - if( size <= elementData.length){ - elementData[size + 1] = o; - size++; - }else{ - elementData = grow(elementData, 1); - elementData[size+1] = o; - size++; - } - } - public void add(int index, Object o){ - Object[] temp = new Object[elementData.length]; - for(int i = 0; i= size){ - return false; - }else{ - return true; - } - } - - @Override - public Object next() { - return elementData[pos]; - } - - } - public static Object[] grow(Object[]src, int size){ - Object[] target = new Object[src.length + size]; - System.arraycopy(src, 0, target, 0, src.length); - return target; - } -} diff --git a/group06/1378560653/src/com/coding/basic/LinkedList.java b/group06/1378560653/src/com/coding/basic/LinkedList.java deleted file mode 100644 index d7b4775f35..0000000000 --- a/group06/1378560653/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,342 +0,0 @@ -package com.coding.basic; - - - -public class LinkedList implements List { - - private Node head; - private int size; - - private static class Node { - Object data; - Node next; - - public Node(Object data){ - this.data = data; - this.next = null; - } - } - - public LinkedList(){ - this.head = new Node(null); - this.size = 0; - } - - public void add(Object o){ - Node newNode = new Node(o); - Node pNode = head; - while(pNode.next != null){ - pNode = pNode.next; - } - - pNode.next = newNode; - size++; - } - public void add(int index , Object o){ - checkIndex(index); - - Node newNode = new Node(o); - Node node = new Node(null); - Node pNode = head; - for(int i = 0; i < index; i++){ - node = pNode; - pNode = pNode.next; - } - - node.next = newNode; - newNode.next = pNode; - size++; - } - public Object get(int index){ - checkIndex(index); - - Node pNode = head; - for(int i = 0; i < index; i++){ - pNode = pNode.next; - } - - return pNode.data; - } - public Object remove(int index){ - checkIndex(index); - if(size == 0){ - return null; - } - - Node node = new Node(null); - Node pNode = head; - for(int i = 0; i < index; i++){ - node = pNode; - pNode = pNode.next; - } - node.next = pNode.next; - size--; - - return pNode; - } - - public int size(){ - Node pNode = head; - while(pNode.next != null){ - pNode = pNode.next; - size++; - } - return size; - } - - public void addFirst(Object o){ - if(size == 0){ - head.data = o; - } - - Node newNode = new Node(o); - Node pNode = head; - head = newNode; - newNode.next = pNode.next; - size++; - } - public void addLast(Object o){ - if(size == 0){ - head.data = o; - } - - Node newNode = new Node(o); - Node pNode = head; - while(pNode.next != null){ - pNode = pNode.next; - } - pNode.next = newNode; - newNode.next = null; - size++; - } - public Object removeFirst(){ - if(size == 0){ - return null; - } - - Node pNode = head; - head = pNode.next; - head.next = pNode.next.next; - size--; - return pNode; - } - public Object removeLast(){ - if(size == 0){ - return null; - } - - Node pNode = head; - Node node = new Node(null); - while(pNode.next != null){ - node = pNode; - pNode = pNode.next; - } - - node.next = null; - size--; - return pNode; - } - public Iterator iterator(){ - return new LinkedListIterator(); - } - - //注意这一个问题 - public class LinkedListIterator implements Iterator { - private int position; - - @Override - public boolean hasNext() { - return position < size(); - } - - @Override - public Object next() { - if(hasNext()){ - return get(position++); - } - return null; - } - - } - - public void checkIndex(int index){ - if(index < 0 || index >= size){ - throw new IndexOutOfBoundsException(); - } - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(Node head){ - if(head.next == null || head.next.next == null){ - return; - } - - Node p = head.next; - Node q = head.next.next; - Node t = null; - - while(q.next != null){ - t = q.next; - q.next = p; - p = q; - q = t; - } - - head.next.next = null;//设置链表尾部 - head.next = p;//设置链表头部 - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - if(size == 0 || head.next == null || head.next.next == null){ - return; - } - - Node pNode = head; - Node node = null; - for(int i = 0; i < size/2; i++){ - node = pNode; - pNode = pNode.next; - } - - if(size %2 == 0){ - head.next = pNode; - }else{ - head.next = node; - } - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - if(size == 0 || head.next == null){ - return; - } - - for(int k = i; k < i + length; k++){ - checkIndex(k); - remove(k); - } - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * list = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - if(list.size == 0 || list == null){ - return new int[0]; - } - - int[] array = new int[list.size]; - int k = 0; - for(int i = 0; i < list.size; i++){ - int index = (int) list.get(i); - array[k] = (int) get(index); - } - return array; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list,LinkedList oldList){ - if(oldList == null || oldList.size ==0 || list == null || list.size == 0){ - return; - } - - for(int i = 0; i < oldList.size; i++){ - for(int j = 0; j < list.size; j++){ - if(list.get(j) == oldList.get(i)){ - oldList.remove(i); - } - } - } - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(LinkedList list){ - if(list == null || list.size == 0){ - return; - } - - int count = 0; - Node pNode = head; - while(pNode.next != null){ - pNode = pNode.next; - count++; - if(pNode.data == pNode.next.data){ - list.remove(count+1); - } - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - if(size == 0){ - return; - } - - int count = 0; - Node pNode = head; - while(pNode.next != null){ - pNode = pNode.next; - count++; - if(min < (int)pNode.data || (int)pNode.data < max){ - remove(count); - } - } - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - if(list.size == 0){ - return null; - } - - LinkedList listC = new LinkedList(); - Node p = head; - Node q = list.head; - - while(p.next != null){ - p = p.next; - while(q.next !=null){ - q = q.next; - if(p.data.equals(q.data)){ - listC.add(p); - } - } - } - return listC; - } -} diff --git a/group06/1378560653/src/com/coding/basic/Queue.java b/group06/1378560653/src/com/coding/basic/Queue.java index a574f4b859..7a82835913 100644 --- a/group06/1378560653/src/com/coding/basic/Queue.java +++ b/group06/1378560653/src/com/coding/basic/Queue.java @@ -1,5 +1,7 @@ package com.coding.basic; +import com.coding.basic.linklist.LinkedList; + public class Queue { private LinkedList linkedlist = new LinkedList(); diff --git a/group06/1378560653/src/com/coding/basic/Stack.java b/group06/1378560653/src/com/coding/basic/Stack.java index 2d0b260880..37fb5f26b5 100644 --- a/group06/1378560653/src/com/coding/basic/Stack.java +++ b/group06/1378560653/src/com/coding/basic/Stack.java @@ -1,5 +1,7 @@ package com.coding.basic; +import com.coding.basic.array.ArrayList; + public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group06/1378560653/src/com/coding/basic/array/ArrayList.java b/group06/1378560653/src/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..9fd1e776cc --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/array/ArrayList.java @@ -0,0 +1,98 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if( size <= elementData.length){ + elementData[size + 1] = o; + size++; + }else{ + elementData = grow(elementData, 1); + elementData[size+1] = o; + size++; + } + } + public void add(int index, Object o){ + Object[] temp = new Object[elementData.length]; + for(int i = 0; i= size){ + return false; + }else{ + return true; + } + } + + @Override + public Object next() { + return elementData[pos]; + } + + } + public static Object[] grow(Object[]src, int size){ + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} diff --git a/group06/1378560653/src/com/coding/basic/array/ArrayUtil.java b/group06/1378560653/src/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..3e3b51e735 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,262 @@ +package com.coding.basic.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + //注意边界条件 + if(origin == null || origin.length == 0){ + return; + } + + for(int i=0, j = origin.length-1; i array2[j]){ + newArray[count++] = array2[j++]; + }else if(array1[i] == array2[j]){ + newArray[count++] = array2[j++]; + i++; + } + } + + while(i==array1.length && j= max){ + break; + }else{ + count++; + } + } + + return Arrays.copyOf(a,count); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 3){ + return new int[0]; + } + + boolean[] isPrime = isPrime(max); + + int[] array = new int[max]; + int count = 1; + array[0] = 2; + for(int i = 3; i < max; i++){ + if(isPrime[i]){ + array[count++] = i; + } + } + return Arrays.copyOf(array, count); + } + + private boolean[] isPrime(int max) { + boolean[] isPrime = new boolean[max]; + for(int i = 3; i < max; i++){ + if(i % 2 != 0 ){ + isPrime[i] = true; + }else{ + isPrime[i] = false; + } + } + + for(int i = 3; i < Math.sqrt(max); i++){ + if(isPrime[i]){ + for(int j = 2*i ; j < max; j += i){ + isPrime[j] = false; + } + } + } + return isPrime; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max < 0){ + return new int[0]; + } + + int[] Array = new int[max]; + + int count = 0; + for(int n = 1; n < max; n++) + { + int sum = 0; + for(int i=1; i< n; i++) + { + if(n%i == 0) + sum += i; + } + if(sum == n) + Array[count++] = n; + } + + return Arrays.copyOf(Array, count); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) + { + if(array == null || array.length == 0){ + return ""; + } + + StringBuilder buffer = new StringBuilder(); + + for(int i = 0; i < array.length; i++){ + buffer.append(array[i]); + if(i < array.length - 1){ + buffer.append(seperator); + } + } + + return buffer.toString(); + } +} diff --git a/group06/1378560653/src/com/coding/basic/array/ArrayUtilTest.java b/group06/1378560653/src/com/coding/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0513b8e05f --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/array/ArrayUtilTest.java @@ -0,0 +1,150 @@ +package com.coding.basic.array; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ArrayUtilTest { + + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception{ + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() + { + int[] a = {3, 5, 7, 8, 9}; + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 抛出异常 + expectedEx.expect(Exception.class); + myArray.grow(a, -3);//注意这里 + } + + @Test + public void testFibonacci() + { + //max == 1时返回空数组 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2的时候没有素数,数组为空数组 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //传递空数组时,返回空字符串 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } +} + diff --git a/group06/1378560653/src/com/coding/basic/linklist/LRUPageFrame.java b/group06/1378560653/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..75f3002227 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,154 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node(int pageNum) { + this.pageNum = pageNum; + } + } + + private int capacity;//容量 + + private Node first; // 链表头 + private Node last; // 链表尾 + + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + if(capacity == 0){ + return; + } + + Node node = new Node(pageNum); + + //填满:从后往前填满 + if(!isFull()){ + if(first == null && last == null){ + first = node; + last = node; + first.prev = null; + last.prev = null; + } else { + first.prev = node; + node.next = first; + first = node; + } + } else { + if(!isFind(pageNum)){ + first.prev = node; + node.next = first; + first = node; + last = last.prev; + last.next = null; + } else { + Node pNode = first; + if(first.pageNum == pageNum){ + return; + } + + //注意:while循环只是遍历了1~last.prev的节点 + while(pNode.next != null){ + if(pNode.pageNum == pageNum){ + pNode.next.prev = pNode.prev; + pNode.prev.next = pNode.next; + pNode.next = first; + first.prev = pNode; + first = pNode; + break; + } + pNode = pNode.next; + } + + if(last.pageNum == pageNum){ + last.next = first; + first.prev = last; + first = last; + last = last.prev; + last.next = null; + } + } + } + + } + + private boolean isFind(int pageNum) { + Node pNode = first; + while(pNode != null){ + if(pNode.pageNum == pageNum){ + return true; + } + pNode = pNode.next; + } + return false; + } + + public boolean isFull() { + int count = 0; + Node pNode = first; + while(pNode != null){ + count++; + pNode = pNode.next; + } + + if(count < capacity){ + return false; + } else { + return true; + } + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + public static void main(String args[]){ + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + System.out.println(frame.toString()); + frame.access(2); + System.out.println(frame.toString()); + frame.access(0); + System.out.println(frame.toString()); + frame.access(0); + System.out.println(frame.toString()); + frame.access(3); + System.out.println(frame.toString()); + frame.access(0); + System.out.println(frame.toString()); + frame.access(4); + System.out.println(frame.toString()); + } +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group06/1378560653/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..c323d03b3f --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coding/basic/linklist/LinkedList.java b/group06/1378560653/src/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..22ad1d6070 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,445 @@ +package com.coding.basic.linklist; + +import java.util.Arrays; +import java.util.Stack; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + private Node head; + private int size; + + public LinkedList(){ + this.head = null; + this.size = 0; + } + + private static class Node { + Object data; + Node next; + + private Node(Object data){ + this.data = data; + this.next = null; + } + } + + public void add(Object o){ + Node node = new Node(o); + if(head == null){ + head = node; + }else{ + Node pNode = head; + while(pNode.next != null){ + pNode = pNode.next; + } + pNode.next = node; + } + size++; + } + + public void add(int index , Object o){ + checkIndex(index); + + Node newNode = new Node(o); + Node node = new Node(null); + Node pNode = head; + for(int i = 0; i < index; i++){ + node = pNode; + pNode = pNode.next; + } + + node.next = newNode; + newNode.next = pNode; + size++; + } + public Object get(int index){ + checkIndex(index); + + Node pNode = head; + for(int i = 0; i < index; i++){ + pNode = pNode.next; + } + + return pNode.data; + } + public Object remove(int index){ + checkIndex(index); + if(head == null){ + return null; + } + + if(index == 0){ + removeFirst(); + }//忘了考虑这种情况了 + + Node node = new Node(null); + Node pNode = head; + for(int i = 0; i < index; i++){ + node = pNode; + pNode = pNode.next; + } + node.next = pNode.next; + size--; + + return pNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o); + + if(head == null){ + head = newNode; + }else{ + newNode.next = head; + head = newNode; + } + size++; + } + + public void addLast(Object o){ + Node newNode = new Node(o); + if(head == null){ + head = newNode; + } + + Node pNode = head; + while(pNode.next != null){ + pNode = pNode.next; + } + pNode.next = newNode; + newNode.next = null; + size++; + } + + public Object removeFirst(){ + if(head == null){ + return null; + } + + Node pNode = head; + head = pNode.next; + head.next = pNode.next.next; + size--; + return pNode.data; + } + + public Object removeLast(){ + if(head == null){ + return null; + } + + Node pNode = head; + Node node = new Node(null); + while(pNode.next != null){ + node = pNode; + pNode = pNode.next; + } + + node.next = null; + size--; + return pNode.data; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + //注意这一个问题 + public class LinkedListIterator implements Iterator { + private int position; + + @Override + public boolean hasNext() { + return position < size(); + } + + @Override + public Object next() { + if(hasNext()){ + return get(position++); + } + return null; + } + + } + + public void checkIndex(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(); + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(head == null){ + return; + } + + Stack s = new Stack<>(); + + Node currentNode = head; + while(currentNode != null){ + s.push(currentNode); + + Node nextNode = currentNode.next; + currentNode.next = null; //把链断开 + currentNode = nextNode; + } + + head = s.pop(); + + currentNode = head; + while(!s.isEmpty()){ + Node nextNode = s.pop(); + currentNode.next = nextNode; + currentNode = nextNode; + } + } + + /*if(head == null || head.next == null){ + return; + } + + Node next = null;//当前节点的后一个节点 + Node pre = null;//当前节点的前一个节点 + + while(head != null){ + next = head.next; + head.next = pre; + pre = head; + head = next; + } + head = pre; + }*/ + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(head == null || head.next == null){ + return; + } + + for(int i = 0; i <= size/2; i++){ + removeFirst(); + size--; + } + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(head == null){ + return; + } + + for(int k = i; k < i + length; k++){ + checkIndex(k); + remove(k); + } + size -= length; + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * list = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(list == null){ + return new int[0]; + } + + int[] array = new int[list.size]; + int count = 0; + for(int i = 0; i < list.size; i++){ + int index = (int) list.get(i); + if(index > -1 && index < size){ + array[count] = (int) get(index); + count++; + } + } + + return Arrays.copyOf(array, count); //Arrays.copyOf(a,count)截取数组a的count长度 + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + if(list == null){ + return; + } + + for(int i = 0; i < list.size; i++){ + for(int j = 0; j < size; j++){ + if(list.get(i).equals(get(j))){ + remove(j); + size --; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(head == null){ + return; + } + + for(int i = 0; i < size; i++){ + for(int j = i+1; j < size; j++){ + if(get(i).equals(get(j))){ + remove(j); + size--; + } + } + } + /*if(head == null){ + throw new RuntimeException("LinkedList is empty!"); + }else{ + Node pre = head; + Node cur = head; + while(cur.next != null){ + cur = cur.next; + Object data = pre.data; + while(cur.data == data){ + if(cur.next == null){ + pre.next = null; + break; + } + pre.next = cur.next; + size--; + cur =cur.next; + if(cur == null){ + break; + } + } + pre = pre.next; + } + } + */ + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(head == null){ + return; + } + + Node pre = head; + Node cur = head; + + while(cur.next != null){ + cur = cur.next; + int data = (int)cur.data; + if(data < max && data > min){ + pre.next = cur.next; + cur = cur.next; + } else { + pre = pre.next; + cur = cur.next; + } + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + if(head == null || list.size ==0 ){ + return null; + } + + LinkedList result = new LinkedList(); + + int i = 0; + int j = 0; + + while(i < this.size && j < list.size()){ + + int value1 = (int)this.get(i); + int value2 = (int)list.get(j); + + if(value1 == value2){ + result.add(value1); + i++; + j++; + } else if (value1 < value2) { + i++; + } else { + j++; + } + } + return result; + } + + /* + * 为了测试方便,引入toString()方法 + */ + public String toString() { + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + Node node = head; + while(node != null){ + buffer.append(node.data); + if(node.next != null){ + buffer.append(","); + } + node = node.next; + } + buffer.append("]"); + + return buffer.toString(); + } + + public static void main(String args[]){ + LinkedList list7 = new LinkedList(); + + list7.add(1); + list7.add(2); + list7.add(3); + list7.add(4); + list7.add(5); + list7.add(6); + list7.add(12); + + for(int i = 0; i < list7.size(); i++){ + System.out.println(list7.get(i)); + } + } +} + + diff --git a/group06/1378560653/src/com/coding/basic/linklist/LinkedListTest.java b/group06/1378560653/src/com/coding/basic/linklist/LinkedListTest.java new file mode 100644 index 0000000000..a51e6455b3 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/linklist/LinkedListTest.java @@ -0,0 +1,309 @@ +package com.coding.basic.linklist; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + + +public class LinkedListTest { + @Test + public void testAdd() { + LinkedList l1 = new LinkedList(); + + Assert.assertEquals("[]", l1.toString()); + + l1.add(1); + l1.add(2); + l1.add(3); + l1.add(4); + + Assert.assertEquals("[1,2,3,4]", l1.toString()); + } + + @Test + public void TestSize(){ + LinkedList l2 = new LinkedList(); + + Assert.assertEquals(0, l2.size()); + + l2.add(1); + l2.add(2); + l2.add(3); + + Assert.assertEquals(3, l2.size()); + } + + @Test + public void testAddIndex(){ + LinkedList l3 = new LinkedList(); + + l3.add(1); + l3.add(2); + l3.add(3); + l3.add(4); + + l3.add(2, 6); + + Assert.assertEquals("[1,2,6,3,4]", l3.toString()); + } + + @Test + public void testGet(){ + LinkedList l4 = new LinkedList(); + l4.add(1); + l4.add(2); + l4.add(3); + l4.add(4); + + Assert.assertEquals(3, l4.get(2)); + } + + @Test + public void testRemove(){ + LinkedList l5 = new LinkedList(); + l5.add(1); + l5.add(2); + l5.add(3); + l5.add(4); + + l5.remove(3); + + Assert.assertEquals("[1,2,3]", l5.toString()); + } + + @Test + public void testAddFirst(){ + LinkedList l6 = new LinkedList(); + + l6.addFirst(1); + + Assert.assertEquals("[1]", l6.toString()); + + l6.add(2); + l6.add(3); + + l6.addFirst(2); + + Assert.assertEquals("[2,1,2,3]", l6.toString()); + } + @Test + public void testAddLast(){ + LinkedList l7 = new LinkedList(); + + l7.addLast(1); + + Assert.assertEquals("[1]", l7.toString()); + + l7.add(2); + l7.add(3); + + l7.addLast(4); + + Assert.assertEquals("[1,2,3,4]", l7.toString()); + } + @Test + public void testRmemoveFirst(){ + LinkedList l8 = new LinkedList(); + + l8.removeFirst(); + + Assert.assertEquals("[]", l8.toString()); + + l8.add(2); + l8.add(3); + + l8.removeFirst(); + + Assert.assertEquals("[3]", l8.toString()); + } + + @Test + public void testRmemoveLast(){ + LinkedList l9 = new LinkedList(); + + l9.removeLast(); + + Assert.assertEquals("[]", l9.toString()); + + l9.add(2); + l9.add(3); + + l9.removeLast(); + + Assert.assertEquals("[2]", l9.toString()); + } + + @Test + public void testReverse(){ + LinkedList list1 = new LinkedList(); + + list1.reverse(); + + Assert.assertEquals("[]", list1.toString()); + + list1.add(1); + list1.add(2); + list1.add(3); + + list1.reverse(); + + Assert.assertEquals("[3,2,1]", list1.toString()); + } + + @Test + public void testRemoveFirstHalf(){ + LinkedList list2 = new LinkedList(); + list2.removeFirstHalf(); + Assert.assertEquals("[]", list2.toString()); + + list2.add(1); + list2.removeFirstHalf(); + Assert.assertEquals("[1]", list2.toString()); + + list2.add(2); + list2.add(3); + list2.add(4); + list2.removeFirstHalf(); + + Assert.assertEquals("[3,4]", list2.toString()); + + list2.add(5); + list2.removeFirstHalf(); + + Assert.assertEquals("[4,5]", list2.toString()); + + } + @Test + public void testRemoveLength(){ + LinkedList list3 = new LinkedList(); + list3.remove(1,3); + Assert.assertEquals("[]", list3.toString()); + + list3.add(1); + list3.add(2); + list3.add(3); + list3.add(4); + list3.add(5); + list3.remove(0,1); + + Assert.assertEquals("[2,3,4,5]", list3.toString()); + } + + @Test + public void testGetElements(){ + LinkedList list4 = new LinkedList(); + LinkedList list = new LinkedList(); + + int[] array = null; + array = list4.getElements(list); + + assertArrayEquals(new int[0], array); + + list4.add(11); + list4.add(101); + list4.add(201); + list4.add(301); + list4.add(401); + list4.add(501); + list4.add(601); + list4.add(701); + list.add(1); + list.add(3); + list.add(12); + list.add(6); + + array = list4.getElements(list); + int[] result = {101,301,601}; + + assertArrayEquals(result, array); + + } + @Test + public void testSubtract(){ + LinkedList list5 = new LinkedList(); + LinkedList list = new LinkedList(); + + list5.subtract(list); + + Assert.assertEquals("[]", list5.toString()); + + list5.add(11); + list5.add(101); + list5.add(201); + list5.add(301); + list5.add(401); + list5.add(501); + list5.add(601); + list5.add(701); + list.add(11); + list.add(301); + list.add(12); + list.add(401); + + list5.subtract(list); + + Assert.assertEquals("[101,201,501,601,701]", list5.toString()); + } + @Test + public void testRemoveDuplicateValues(){ + LinkedList list6 = new LinkedList(); + + list6.removeDuplicateValues(); + + Assert.assertEquals("[]", list6.toString()); + + list6.add(1); + list6.add(2); + list6.add(2); + list6.add(10); + + list6.removeDuplicateValues(); + + Assert.assertEquals("[1,2,10]", list6.toString()); + } + + @Test + public void testRemoveRange(){ + LinkedList list7 = new LinkedList(); + + list7.removeRange(0, 10); + + Assert.assertEquals("[]", list7.toString()); + + list7.add(1); + list7.add(2); + list7.add(3); + list7.add(4); + list7.add(5); + list7.add(6); + list7.add(12); + + list7.removeRange(3, 10); + + Assert.assertEquals("[1,2,12]", list7.toString()); + } + + @Test + public void testIntersection(){ + LinkedList list8 = new LinkedList(); + + list8.add(1); + list8.add(2); + list8.add(3); + list8.add(4); + + LinkedList list9 = new LinkedList(); + list9.add(2); + list9.add(3); + list9.add(4); + list9.add(8); + + LinkedList result = new LinkedList(); + result = list8.intersection(list9); + + Assert.assertEquals("[2,3,4]", result.toString()); + + } +} + diff --git a/group06/1454385822/src/com/coding/basic/homework_04/jvm.rar b/group06/1454385822/src/com/coding/basic/homework_04/jvm.rar new file mode 100644 index 0000000000..2d7096c7b7 Binary files /dev/null and b/group06/1454385822/src/com/coding/basic/homework_04/jvm.rar differ diff --git a/group06/1454385822/src/com/coding/basic/homework_04/jvm/loader/ClassFileLoader.java b/group06/1454385822/src/com/coding/basic/homework_04/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..e4d36e04c6 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_04/jvm/loader/ClassFileLoader.java @@ -0,0 +1,114 @@ +package com.coding.basic.homework_04.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + private static final int BUFFER_SIZE = 1024; + + public byte[] readBinaryCode(String className) { + byte[] result = null; + for(String path : clzPaths){ + File file = new File(getPath(path, className)); + if(!file.exists()){ + continue; + } + result = readFile(file); + } + return result; + } + + /** + * 文件数据存放在字节数组中返回 + * @param file + * @return + */ + private byte[] readFile(File file){ + FileInputStream fileInputStream; + byte[] buffer = new byte[BUFFER_SIZE]; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + try { + fileInputStream = new FileInputStream(file); + while(byteArrayOutputStream.size() < file.length()){ + int len = fileInputStream.read(buffer); + if(len < 0){ + break; + } + byteArrayOutputStream.write(buffer, 0, len); + } + } catch (Exception e) { + e.printStackTrace(); + } + + if(byteArrayOutputStream.size() > file.length()){ + byte[] result = byteArrayOutputStream.toByteArray(); + return Arrays.copyOf(result, (int)file.length()); + } + return byteArrayOutputStream.toByteArray(); + } + + /** + * 获取真实路径路径 + * @param className + * @return + */ + private String getPath(String path ,String className){ + System.out.println(className); + String [] ways = className.split("\\."); + for (String string : ways) { + System.out.println(string); + } + StringBuilder builder = new StringBuilder(); + builder.append(path); + for (String string : ways) { + + builder.append("\\"); + builder.append(string); + } + builder.append(".class"); + System.out.println(builder.toString()); + return builder.toString(); + } + + private byte[] loadClassFile(String clzFileName) { + + return null; + } + + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath_V1(){ + + return null; + } + + + public String getClassPath(){ + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < clzPaths.size(); i++){ + builder.append(clzPaths.get(i)); + if(i < clzPaths.size() - 1){ + builder.append(";"); + } + } + return builder.toString(); + } + + + + + +} \ No newline at end of file diff --git a/group06/1454385822/src/com/coding/basic/homework_04/jvm/test/ClassFileloaderTest.java b/group06/1454385822/src/com/coding/basic/homework_04/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..a0ba195077 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_04/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,89 @@ +package com.coding.basic.homework_04.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.homework_04.jvm.loader.ClassFileLoader; + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\yanght\\Documents\\mygit\\coding2017-1\\group06\\1454385822\\bin"; + static String path2 = "D:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coding.basic.homework_04.jvm.test.EmployeeV1"; +// String className = "EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1084, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coding.basic.homework_04.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i= capacity){ //缓存页满了 + Node node = getNode(pageNum); + LRU(node, pageNum); + } + } + + /** + * lru算法 + * @param node + * @param pageNum + */ + private void LRU(Node node, int pageNum){ + if(node != null){ + if(last.pageNum == node.pageNum){ //缓存是last + last = node.prev; + last.next = null; + node.next = first; + first.prev = node; + node.prev = null; + first = node; + }else if(last.pageNum != node.pageNum && first.pageNum != node.pageNum){ + //缓存在first和last的中间范围 + node.prev.next = node.next; + node.next.prev = node.prev; + node.prev = null; + node.next = first; + first = node; + } + }else{ + //新缓存 + last = last.prev; + last.next = null; + node = new Node(pageNum); + node.next = first; + first.prev = node; + first = node; + } + } + + /** + * 根据数据在缓存中获取节点 + * @param pageNum + * @return + */ + private Node getNode(int pageNum){ + Node node = first; + while(node != null){ + if(node.pageNum == pageNum){ + return node; + } + node = node.next; + } + return null; + } + + public int size(){ + int num = 0; + Node node = first; + while(node != null){ + num++; + node = node.next; + } + return num; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group06/1454385822/src/com/coding/basic/homework_04/lru/LRUPageFrameTest.java b/group06/1454385822/src/com/coding/basic/homework_04/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..330dbf4836 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_04/lru/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.homework_04.lru; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group06/2415980327/CodeSE01/down2.png b/group06/2415980327/CodeSE01/down2.png new file mode 100644 index 0000000000..4c8bf3db8f Binary files /dev/null and b/group06/2415980327/CodeSE01/down2.png differ diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/basic/LinkedList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/basic/LinkedList.java new file mode 100644 index 0000000000..261d8f5427 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/basic/LinkedList.java @@ -0,0 +1,456 @@ +package com.pxshuo.se03.basic; + +import com.pxshuo.se01.basic.Iterator; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size = 0; + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 获取一个节点,不进行范围检查 + * @param index 位置 + */ + private Node getNode(int index){ + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + /** + * 添加一个节点 + * @param o + */ + public void add(Object o){ + Node node = new Node(o); + Node l = last; + last = node; + + if (head == null) { + head = node; + } + else{ + l.next = last; + } + size ++; + } + + /** + * 在某个位置进行插入 + * @param index 位置 + * @param o 插入的元素 + */ + public void add(int index , Object o){ + checkPositionIndex(index); + + if (size == index) { + add(o); + } + else if (0 == index){ + addFirst(o); + } + else { + Node node = new Node(o); + Node preNode = getNode(index - 1); + + node.next = preNode.next; + preNode.next = node; + + size++; + } + } + + /** + * 获取一个节点 + * @param index 位置 + */ + public Object get(int index){ + checkElementIndex(index); + return getNode(index).data; + } + + + public Object remove(int index){ + checkElementIndex(index); + Object data = null; + if (index == 0) { + data = removeFirst(); + } + else if(index == size) { + data = removeLast(); + } + else { + Node pre = getNode(index - 1); + data = pre.next.data; + pre.next = pre.next.next; + size --; + } + return data; + } + + public int size(){ + return size; + } + + /** + * 添加第一个元素 + * @param o + */ + public void addFirst(Object o){ + if (head == null) { + add(o); + return; + } + + Node node = new Node(o); + node.next = head; + head = node; + size ++; + } + /** + * 添加最后的元素 + * @param o + */ + public void addLast(Object o){ + add(o); + } + /** + * 移除最初元素 + * @return + */ + public Object removeFirst(){ + checkElementIndex(0);//检查首位是否有元素 + if (head == last) { + last = null; + } + Object data = head.data; + head = head.next; + size --; + return data; + } + + /** + * 移除最后的元素 + * @return + */ + public Object removeLast(){ + checkElementIndex(0);//检查首位是否有元素 + if (head == last) { + return removeFirst(); + } + Object data = last.data; + last = getNode(size - 2); + last.next = null; + return data; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + public void display() { + Node cur = head; + int i = 0; + while(cur != null && i < 1000) + { + System.out.println(i + ":" + cur.data.toString()); + cur = cur.next; + } + } + + public String getResult() { + Node cur = head; + int i = 0; + String result = ""; + while(cur != null && i < 1000) + { + result += cur.data.toString(); + cur = cur.next; + if (cur != null) { + result += ","; + } + } + + return result; + } + + private static class Node{ + Object data; + Node next; + + public Node(Object o){ + data = o; + } + } + + private class LinkedListIterator implements Iterator{ + Node position = null; + + public LinkedListIterator() { + this.position = head; + } + + @Override + public boolean hasNext() { + if (position == null) { + return false; + } + return true; + } + + @Override + public Object next() { + Object data = position.data; + position = position.next; + return data; + } + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + checkElementIndex(0); + if (size == 1) { + return; + } + + last = head; + Node pre = null; + Node next = null; + while(head.next != null){ + next = head.next; + head.next = pre; + pre = head; + head = next; + } + head.next = pre; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if (size < 2) { + head = null; + last = null; + size = 0; + } + + int half = size/2; + Node halfPre = getNode(half - 1); + head = halfPre.next; + halfPre.next = null; + size = size - half; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if (length < 1) { + return; + } + checkElementIndex(i); + checkElementIndex(i + length - 1); + + Node pre = null;//删除元素的前一个 + Node nextPre = getNode(i + length - 1);//删除元素的最后一个 + if (i != 0) { + pre = getNode(i - 1); + } + + if (pre != null) { + pre.next = nextPre.next; + } + else { + head = nextPre.next; + } + + if (nextPre.next == null) { + last = pre; + } + + nextPre = null; + size -= length; + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] listResult = new int[list.size]; + int i = 0; + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + int index = Integer.parseInt(iterator.next().toString()); + listResult[i] = Integer.parseInt(get(index).toString()); + i ++; + } + return listResult; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + public void subtract(LinkedList list){ + int index = 0; + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + Object del = iterator.next(); + for (index = 0;index < size;index ++) { + if (((String)get(index)).equals((String)del)) { + remove(index); + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + checkElementIndex(0); + Node cur = head; + while(cur.next != null){ + String curData = (String)(cur.data); + String nextData = (String)(cur.next.data); + if (curData.equals(nextData)) { + size --; + if (cur.next == last) { + cur.next = null; + last = cur; + }else { + cur.next = cur.next.next; + } + } + else { + cur = cur.next; + } + + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + checkElementIndex(0); + Node minNode = null;//删除的前一个节点 + Node maxNode = null; + Node cur = head; + Node pre = null; + int delSize = 0; + + while(cur != null){ + int curVal = Integer.parseInt((String)(cur.data)); + if (minNode == null && delSize == 0) { + if (curVal > min) {//开始删除 + minNode = pre; + delSize ++; + } + } + + if (delSize != 0) { + if (curVal > max) {//结束删除 + maxNode = cur; + break;//跳出循环 + } + else { + delSize ++; + } + } + pre = cur; + cur = cur.next; + } + //进行删除 + if (delSize - 1 == size) {//删除全部 + head = null; + last = null; + size = 0; + return; + } + else if (delSize == 0) {//没有删除任务 + return; + } + else if (minNode == null) {//从头部开始删除 + head = maxNode; + } + else if (maxNode == null) {//删除尾部 + last = minNode; + last.next = null; + } + else {//删除中间部分 + minNode.next = maxNode; + } + + size -= delSize - 1; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList result = new LinkedList(); + Node cur = head; + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + Object data = iterator.next(); + int dataNum = Integer.parseInt((String)data); + while(cur != null){ + int curData = Integer.parseInt((String)(cur.data)); + if(curData == dataNum){ + result.add(data); + break; + } + else if(dataNum < curData){ + break; + } + cur = cur.next; + } + } + return result; + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/basic/List.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/basic/List.java new file mode 100644 index 0000000000..bd0aca9a4c --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/basic/List.java @@ -0,0 +1,9 @@ +package com.pxshuo.se03.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/DownloadThread.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/DownloadThread.java new file mode 100644 index 0000000000..36d2555ab6 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/DownloadThread.java @@ -0,0 +1,51 @@ +package com.pxshuo.se03.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +import com.pxshuo.se03.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + CyclicBarrier barrier; + String localFile; + + public DownloadThread( Connection conn, int startPos, int endPos,String localFile,CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + try { + byte[] data = conn.read(startPos, endPos); + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + + file.seek(startPos); + file.write(data); + file.close(); + + conn.close(); + barrier.await(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (BrokenBarrierException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/FileDownloader.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/FileDownloader.java new file mode 100644 index 0000000000..8d78d08a62 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/FileDownloader.java @@ -0,0 +1,140 @@ +package com.pxshuo.se03.download; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.pxshuo.se03.download.api.Connection; +import com.pxshuo.se03.download.api.ConnectionException; +import com.pxshuo.se03.download.api.ConnectionManager; +import com.pxshuo.se03.download.api.DownloadListener; + + +public class FileDownloader { + + private final static int DOWNLOAD_THREAD_NUM = 3; + + private String url; + private String filePath; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url,String filePath) { + this.url = _url; + this.filePath = filePath; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + CyclicBarrier cyclicBarrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM,new Runnable() { + + @Override + public void run() { + // TODO Auto-generated method stub + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + System.out.println(length); + + createPlaceHolderFile(this.filePath, length); + + int[][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM, length); + + for(int i = 0; i < DOWNLOAD_THREAD_NUM; i++){ + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + filePath, + cyclicBarrier); + + thread.start(); + } + + } catch (ConnectionException | IOException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + + /** + * 先在硬盘中占据一部分空间 + * @param filePath + * @param length + * @throws IOException + */ + private void createPlaceHolderFile(String filePath,int length) throws IOException{ + RandomAccessFile file = new RandomAccessFile(filePath, "rw"); + + for (int i = 0; i < length; i++) {//初始化一个文件 + file.write(0); + } + + file.close(); + } + + private int[][] allocateDownloadRange(int threadNum,int length){ + int[][] ranges = new int[threadNum][2]; + + int eachThreadSize = length/threadNum; + int left = length % threadNum; + + for (int i = 0; i < threadNum; i++) { + int startPos = i * eachThreadSize; + int endPos = (i + 1) * eachThreadSize - 1; + + if (i == (threadNum - 1)) { + endPos += left; + } + + ranges[i][0] = startPos; + ranges[i][1] = endPos; + } + + return ranges; + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/FileDownloaderTest.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/FileDownloaderTest.java new file mode 100644 index 0000000000..d00fc54677 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/FileDownloaderTest.java @@ -0,0 +1,64 @@ +package com.pxshuo.se03.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.pxshuo.se03.download.api.ConnectionManager; +import com.pxshuo.se03.download.api.DownloadListener; +import com.pxshuo.se03.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + public static void main(String[] args) { + FileDownloaderTest test = new FileDownloaderTest(); + test.testDownload(); + } + + public void testDownload() { + + String url = "https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/3c6d55fbb2fb4316352d920a22a4462309f7d394.jpg"; + String filePath = "C://Users//Pxshuo//Desktop//test.png"; + + FileDownloader downloader = new FileDownloader(url,filePath); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/Connection.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/Connection.java new file mode 100644 index 0000000000..27ad88a2ac --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.pxshuo.se03.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/ConnectionException.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/ConnectionException.java new file mode 100644 index 0000000000..c0afde1b28 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.pxshuo.se03.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/ConnectionManager.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/ConnectionManager.java new file mode 100644 index 0000000000..b3c94ba85c --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.pxshuo.se03.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/DownloadListener.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/DownloadListener.java new file mode 100644 index 0000000000..daea7b952f --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.pxshuo.se03.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/impl/ConnectionImpl.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..265923f7b4 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/impl/ConnectionImpl.java @@ -0,0 +1,95 @@ +package com.pxshuo.se03.download.impl; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.util.Arrays; + +import com.pxshuo.se03.download.api.Connection; + +class ConnectionImpl implements Connection{ + + private URL url; + private HttpURLConnection connect = null; + private int length;//不知道每次都确定一下是否比较好 + + private static int BUFFER_SIZE = 1024; + + /** + * 初始化 + * @param destUrl + */ + public ConnectionImpl(String destUrl) { + // TODO Auto-generated constructor stub + + try { + url = new URL(destUrl); + length = -1; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + connect = (HttpURLConnection)url.openConnection(); + connect.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream is = connect.getInputStream(); + + byte[] buff = new byte[BUFFER_SIZE]; + int totalLength = endPos - startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while(baos.size() < totalLength){ + int len = is.read(buff); + if (len < 0) { + break; + } + baos.write(buff,0,len); + } + + if (baos.size() > totalLength) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLength); + } + + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + int length = -1; + try { + connect = (HttpURLConnection) url.openConnection(); + connect.setRequestMethod("GET"); + connect.setConnectTimeout(10000); + if (connect.getResponseCode() == 200) { + length = connect.getContentLength(); + } + System.out.println("error:" + length); + connect.disconnect(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return length; + } + + @Override + public void close() { + if (connect != null) { + connect.disconnect(); + } + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/impl/ConnectionManagerImpl.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..b10970b5f9 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se03/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.pxshuo.se03.download.impl; + +import com.pxshuo.se03.download.api.Connection; +import com.pxshuo.se03.download.api.ConnectionException; +import com.pxshuo.se03.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/test/ConnectTest.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/ConnectTest.java new file mode 100644 index 0000000000..2b75370625 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/ConnectTest.java @@ -0,0 +1,184 @@ +package com.pxshuo.test; + +import java.io.BufferedInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Vector; + + +/** + * 测试实现下载功能 + * @author Pxshuo + * + */ + +public class ConnectTest { + + public static void main(String[] args) { + ConnectTest test = new ConnectTest(); + String url = "https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/3c6d55fbb2fb4316352d920a22a4462309f7d394.jpg"; + try { + //test.saveToFile("https://code.getmdl.io/1.3.0/mdl-template-dashboard.zip", "./down.zip"); + //test.saveToFile("https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/3c6d55fbb2fb4316352d920a22a4462309f7d394.jpg", "./down.png"); +// ConnectionImpl ci = new ConnectionImpl(url); +// FileOutputStream fos = new FileOutputStream("down.png"); +// int length = ci.getContentLength(); +// System.out.println(length); +// fos.write(ci.read(0, length - 1),0,length); +// fos.close(); + System.out.println("success"); + + URL url2 = new URL(url); + HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); + //connection.setRequestMethod("GET"); + //connection.setConnectTimeout(10000); + + FileOutputStream fos = null; + BufferedInputStream bis = null; + byte[] buf = new byte[connection.getContentLength()]; + bis = new BufferedInputStream(connection.getInputStream()); + //建立文件 + fos = new FileOutputStream("down2.png"); + int size = 0;//= bis.read(buf); + int offset = 0; + while((size = bis.read(buf, 0, buf.length)) != -1){ + } + fos.write(buf,0,buf.length); + //保存文件 +// while((size = bis.read(buf)) != -1){ +// fos.write(buf,0,size); +// } + fos.close(); + bis.close(); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public final static boolean DEBUG = true; + private static int BUFFER_SIZE = 8096;//缓存大小 + private Vector vDownload = new Vector<>();//Url列表 + private Vector vFileList = new Vector<>();//文件名 + + public ConnectTest() { + // TODO Auto-generated constructor stub + } + + /** + * 清除下载列表 + */ + public void resetList(){ + vDownload.clear(); + vFileList.clear(); + } + + /** + * 增加下载列表项 + * @param url + * @param filename + */ + public void addItem(String url,String filename){ + vDownload.add(url); + vFileList.add(filename); + } + + /** + * 根据列表下载资源 + */ + public void downLoadByList() { + String url = null; + String filename = null; + + for(int i = 0;i < vDownload.size();i++){ + url = (String)vDownload.get(i); + filename = (String)vFileList.get(i); + + try { + saveToFile(url, filename); + } catch (IOException e) { + // TODO Auto-generated catch block + if (DEBUG) { + System.out.println("资源[" + url + "]下载失败!!!"); + } + e.printStackTrace(); + } + } + + if (DEBUG) { + System.out.println("下载完成"); + } + } + + public void saveToFile(String destUrl,String filename) throws IOException { + FileOutputStream fos = null; + BufferedInputStream bis = null; + HttpURLConnection httpUrl = null; + URL url = null; + byte[] buf = new byte[BUFFER_SIZE]; + int size = 0; + + //建立连接 + url = new URL(destUrl); + httpUrl = (HttpURLConnection)url.openConnection(); + //链接指定的资源 + //httpUrl.connect(); + //获取网络输入流 + bis = new BufferedInputStream(httpUrl.getInputStream()); + //建立文件 + fos = new FileOutputStream(filename); + + if (DEBUG) { + System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + filename + "]"); + } + + int byteLength = 0; + + //保存文件 + while((size = bis.read(buf)) != -1){ + fos.write(buf,0,size); + byteLength += size; + } + System.out.println(httpUrl.getContentLength() + ":" + byteLength); + fos.close(); + bis.close(); + httpUrl.disconnect(); + } + + public void saveToFile(String destUrl,String filename,int start,int end) throws IOException { + FileOutputStream fos = null; + BufferedInputStream bis = null; + HttpURLConnection httpUrl = null; + URL url = null; + byte[] buf = new byte[BUFFER_SIZE]; + int size = 0; + + //建立连接 + url = new URL(destUrl); + httpUrl = (HttpURLConnection)url.openConnection(); + //链接指定的资源 + httpUrl.connect(); + httpUrl.setRequestProperty("Range", "bytes=" + start + "-" + end); + //获取网络输入流 + bis = new BufferedInputStream(httpUrl.getInputStream()); + //建立文件 + fos = new FileOutputStream(filename); + + if (DEBUG) { + System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + filename + "]"); + } + + //保存文件 + while((size = bis.read(buf)) != -1){ + fos.write(buf,0,size); + } + + fos.close(); + bis.close(); + httpUrl.disconnect(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java index 9737d269e8..95c1fef665 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java @@ -1,59 +1,13 @@ package com.pxshuo.test; - -import com.pxshuo.se01.basic.Iterator; -import com.pxshuo.se01.basic.TreeData; -import com.pxshuo.se01.basic.impl.ArrayList; -import com.pxshuo.se01.basic.impl.BinaryTree; -import com.pxshuo.se01.basic.impl.LinkedList; -import com.pxshuo.se01.basic.impl.Queue; -import com.pxshuo.se01.basic.impl.Stack; +import com.pxshuo.se03.basic.LinkedList; public class Test { public static void main(String[] args) { -// LinkedList arrayList = new LinkedList(); -// arrayList.add("hello1"); -// arrayList.add("hello2"); -// arrayList.add(9,"hello3"); -// //arrayList.add(10,"hello4"); -// arrayList.addLast("hi"); -// arrayList.addLast("hihi"); -// arrayList.addFirst("hi1"); -// arrayList.removeFirst(); -// arrayList.removeLast(); -// arrayList.add(1,"hi1"); -// arrayList.remove(1); -// //arrayList.add(0, "hi"); -// //arrayList.remove(8); -// //arrayList.remove(0); -// for (Iterator iterator = arrayList.iterator(); iterator.hasNext();) { -// System.out.println("hi"+iterator.next()); -// } - //Queue queue = new Queue(); -// Stack stack = new Stack(); -// -// for (int i = 0; i < 10; i++) { -// //queue.enQueue("test-" + i); -// stack.push("test-" + i); -// } -// for(int i =0; i< 11; i++) -// { -// System.out.println(stack.pop()); -// } -// stack.push("test-" + 233); -// System.out.println(stack.pop()); - - BinaryTree binaryTree = new BinaryTree(); - binaryTree.add(new TreeData(5)); - binaryTree.add(new TreeData(2)); - binaryTree.add(new TreeData(7)); - binaryTree.add(new TreeData(1)); - binaryTree.add(new TreeData(6)); - binaryTree.add(new TreeData(4)); - binaryTree.add(new TreeData(8)); - - System.out.println(binaryTree.get(5).getClass()); - - //binaryTree.display(); + LinkedList obj = new LinkedList(); + obj.add("3"); + obj.add("7"); + obj.add("10"); + System.out.println(obj.getResult()); } } diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se03/array/LinkedListUntilTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se03/array/LinkedListUntilTest.java new file mode 100644 index 0000000000..962d938479 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se03/array/LinkedListUntilTest.java @@ -0,0 +1,144 @@ +package test.com.pxshuo.se03.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.pxshuo.se03.basic.LinkedList; + +public class LinkedListUntilTest { + private LinkedList obj; + + @Before + public void init() { + obj = new LinkedList(); + } + + @After + public void clear() { + obj = null; + } + + @Test + public void reverseTest() { + obj.add("3"); + obj.add("7"); + obj.add("10"); + obj.reverse(); + Assert.assertEquals("10,7,3", obj.getResult()); + } + + @Test + public void removeFirstHalfTest() { + obj.add("2"); + obj.add("5"); + obj.add("7"); + obj.add("8"); + obj.add("10"); + obj.removeFirstHalf(); + Assert.assertEquals("7,8,10", obj.getResult()); + Assert.assertEquals(3, obj.size()); + } + + @Test + public void removeLengthTest() { + obj.add("2"); + obj.add("5"); + obj.add("7"); + obj.add("8"); + obj.add("10"); + obj.remove(1,3); + Assert.assertEquals("2,10", obj.getResult()); + Assert.assertEquals(2, obj.size()); + } + + @Test + public void getElementsTest() { + obj.add("11"); + obj.add("101"); + obj.add("201"); + obj.add("301"); + obj.add("401"); + obj.add("501"); + obj.add("601"); + obj.add("701"); + LinkedList getList = new LinkedList(); + getList.add("1"); + getList.add("3"); + getList.add("4"); + getList.add("6"); + Assert.assertArrayEquals(new int[]{101,301,401,601}, obj.getElements(getList)); + } + + @Test + public void subtractTest() { + obj.add("11"); + obj.add("101"); + obj.add("201"); + obj.add("301"); + obj.add("401"); + obj.add("501"); + obj.add("601"); + obj.add("701"); + LinkedList getList = new LinkedList(); + getList.add("101"); + getList.add("301"); + getList.add("401"); + getList.add("601"); + obj.subtract(getList); + Assert.assertEquals("11,201,501,701", obj.getResult()); + } + + @Test + public void removeDuplicateValuesTest() { + obj.add("11"); + obj.add("101"); + obj.add("101"); + obj.add("301"); + obj.add("401"); + obj.add("401"); + obj.add("601"); + obj.add("601"); + obj.removeDuplicateValues(); + Assert.assertEquals("11,101,301,401,601", obj.getResult()); + } + + @Test + public void removeRangeTest() { + obj.add("11"); + obj.add("101"); + obj.add("201"); + obj.add("301"); + obj.add("401"); + obj.add("501"); + obj.add("601"); + obj.add("701"); + obj.removeRange(200, 600); + Assert.assertEquals("11,101,601,701", obj.getResult()); + Assert.assertEquals(4, obj.size()); + + } + + @Test + public void intersectionTest() { + obj.add("11"); + obj.add("101"); + obj.add("201"); + obj.add("301"); + obj.add("401"); + obj.add("501"); + obj.add("601"); + obj.add("701"); + + LinkedList getList = new LinkedList(); + getList.add("10"); + getList.add("101"); + getList.add("301"); + getList.add("402"); + getList.add("601"); + + Assert.assertEquals("101,301,601", obj.intersection(getList).getResult()); + + } +} diff --git "a/group06/2415980327/\346\226\207\347\253\240/SE02_\347\254\254\344\272\214\345\221\250\345\215\232\345\256\242_\345\205\263\344\272\216Java\347\232\204\345\217\215\345\260\204\346\234\272\345\210\266.md" "b/group06/2415980327/\346\226\207\347\253\240/SE02_\347\254\254\344\272\214\345\221\250\345\215\232\345\256\242_\345\205\263\344\272\216Java\347\232\204\345\217\215\345\260\204\346\234\272\345\210\266.md" new file mode 100644 index 0000000000..3ac75a6b86 --- /dev/null +++ "b/group06/2415980327/\346\226\207\347\253\240/SE02_\347\254\254\344\272\214\345\221\250\345\215\232\345\256\242_\345\205\263\344\272\216Java\347\232\204\345\217\215\345\260\204\346\234\272\345\210\266.md" @@ -0,0 +1,81 @@ +# 第二周博客 关于Java的反射机制 + +## 前言 + +​ 自从毕业以来,逐渐的接触了Java的开发。虽然也接触了两年有余吧,但总是停留在写一些基础业务流程方面。在工作中,长此以往,也是如此了。所以也是给自己一个机会来了解一些进阶的东西来充实自己。 + +​ 对于Java的反射机制,我不会用,也不懂的原理,不敢随意妄言,只是简单记录一下本次对于Java反射作业的认识,同时梳理一下自己的思路,为了日后更加深刻的了解做一些铺垫吧。 + +## 一、结构梳理 + +​ 本次的作业是一个轻量级的Struts框架的模拟。实际上来说,以前倒是做过一次,只是时间有点久远,或许是没有及时的温习巩固,后来就慢慢的忘记了。此次再次进行模拟,也是参考之前的程序进行编写的。 + +​ 作为一个轻量级的Struts框架,老师给出了一个思路。这个思路是 + +1. 通过传入actionName与参数来使用Struts框架,生成一个View,这个View可以用来获取Html页面与传递数据。 + +2. Struts框架生成View是通过读取xml配置文件来进行生成的。 + + + 根据这个思路来看的话,我们需要: + + * 一个View来展示与保存结果。 + * 一个Struts的框架来利用反射机制,生成结果。 + * 一个xml的配置文件来供Struts进行读取。 + * 还需要一个Action类书写业务流程,为Struts的反射提供实体。 + * 一个Test用于测试,这个不是核心的类了就是了。 + + 而我们看到的作业的初始框架也就分为这几部分了。这些类的运行过程就是Struts根据参数来读取xml文件,利用反射生成Action类,并运行其中固定的函数,并将结果封装成View返回。 + + + +## 二、反射的使用 + +代码示例: + +~~~java +//反射实例化一个对象 +Object action = Class.forName(actionClass).newInstance(); + +//调用exectue +Method exectue = action.getClass().getDeclaredMethod("execute"); +String result = (String)exectue.invoke(action); + +//生成返回列表 +Map actionParam = new HashMap<>(); +Method[] methods = action.getClass().getDeclaredMethods(); +for (Method method : methods) { + if (method.getName().startsWith("get")) { + String methodName = Tools.lowerFirst(method.getName().substring(3)); + String methodValue = (String)method.invoke(action); + actionParam.put(methodName, methodValue); + } +} +~~~ + +### 2.1 反射实例化一个对象 + +​ 反射实例化一个对象是使用``` Object obj = Class.forName("className").newInstance``` 来实现的。通过这种方式,我们可以通过类名(完整的,带包名的字符串)来新建一个实例。可以通过字符串来新建实例也就意味着我们可以通过动态的方式来**new** 一个对象,提供了很大的灵活性。 + +### 2.2 反射调用实例中的方法 + +​ 通过反射调用是使用``` Method method = obj.getClass().getDeclaredMethod("methodName")``` 来获取方法体,并使用``` method.invoke(obj);//obj为一个实例``` 通过这种方式来执行一个函数。 + +​ 这个虽然很灵活,但我用的不是很多,想来如何向函数中传递参数以及如何强制执行私有函数,还有如何防止这种反射机制我还未了解,有时间研究一下,再进行相应的更新。 + +### 2.3 获取方法列表 + +​ 可以通过``` methods = obj.getClass().getDeclaredMethods()``` 来获取实例中所有的方法。 + +​ 不知道有么有方法可以往实例中注入一些方法呢? + +### 2.4 这些可以做什么 + +​ 这些功能这么灵活,那么应该如何使用它们呢?是不是可以开发一个自由度特别特别高的程序呢?感觉上是可以的吧,就像是一个更专业向的框架一样。是不是一个好的想法呢~ + + + +## 三、POI的XML文件读取 + +​ 这次作业,感觉比较让人头大的应该是xml文件的读取了,但是只是属于比较繁琐,但是也比较好理解的范畴,随用随查即可,不像反射那样可以有灵活的运用,所以就不做重点的介绍。 + diff --git "a/group06/2415980327/\346\226\207\347\253\240/SE03_\347\254\254\344\270\211\345\221\250\345\215\232\345\256\242_Java\347\232\204\345\244\232\347\272\277\347\250\213\343\200\201\344\270\213\350\275\275\344\270\216\346\226\207\344\273\266IO.md" "b/group06/2415980327/\346\226\207\347\253\240/SE03_\347\254\254\344\270\211\345\221\250\345\215\232\345\256\242_Java\347\232\204\345\244\232\347\272\277\347\250\213\343\200\201\344\270\213\350\275\275\344\270\216\346\226\207\344\273\266IO.md" new file mode 100644 index 0000000000..5cf7dbf6b9 --- /dev/null +++ "b/group06/2415980327/\346\226\207\347\253\240/SE03_\347\254\254\344\270\211\345\221\250\345\215\232\345\256\242_Java\347\232\204\345\244\232\347\272\277\347\250\213\343\200\201\344\270\213\350\275\275\344\270\216\346\226\207\344\273\266IO.md" @@ -0,0 +1,96 @@ +# 第三周博客 Java的多线程、下载与文件IO + +# 前言 + +​ Java里面目前三个最让我头疼的地方在这次的作业中一次性的全都出现了。虽然是一个专科出身的程序员,但是自认为在Java上对于这方面的掌握应该是远远不及经历过培训的同学们的。这是自己的一大弱点,对于一个新的技术,不能系统的去进行学习与研究。对于此,只能是慢慢地区梳理与使用才是了。 + +​ 对于本次的作业,显得异常的吃力,最后也是只能等到老师的讲解视频出来之后才慢慢跟着老师的思路去编码,所以再次的梳理一次程序,让自己有一个更加清晰的概念吧。 + +## 一、总体结构 + +​ 本次的作业被分为了三个包 + +* download + +* download.api + +* download.impl + + 在API接口中,有四个接口文件,其中异常接口未被实现,其他三个,分别是Connection,ConnectionManager和DownloadListener分别在impl或者程序运行中进行了实现。这种接口形式的结构可以在没有业务代码的情况下,整理好整个程序运行的框架,剩下就可以再填写.目前来看,这方面的能力需要多加练习 + + 程序主体流程包含了测试类、多线程下载类与网络连接管理类。 + +## 二、测试程序 + +​ 感觉实际上测试类可以再封装一次,虽然会更加方便地使用,但是是否会牺牲很多的灵活性呢?还是看实际的应用吧。正所谓适可而止,过犹不及。 + +​ 从测试来看的话,我们将url与文件地址传递到下载类中去,之后再在其中设置网络连接的管理类与下载完成的回调函数。这个回调函数是在所有线程全部完成之后进行回调。接下来开始下载,然后通过回调函数中的标识位来循环判断下载的进度。 + + + +## 三、多线程下载类 + +主流程: + +​ 1、使用CyclicBarrier来监测多个线程的结束与否,使用方法是在创建线程的run方法最后使用``` barrier.await()``` 来执行监测 + +​ 2、然后通过网络连接管理类获取到connection的相关信息,我们就可以根据*连接的长度* 来使用RandomAccessFile创建一个空白文件来确定硬盘空间足够。在之后各个线程下载完毕就会改写这个文件相关部分,从而完成文件的合并。 + +​ 3、根据下载的线程数来分配每个线程需要下载的部分,并开始创建线程,进行下载。 + +单个下载线程: + +​ 通过RandomAccessFile将下载下来的byte[]写入文件中,通过``` file.seek(index)``` 来确定写入文件的位置。 + + + +## 四、网络连接类 + +​ 本次作业通过管理类将连接类隐藏了起来。连接类只是对包内可见,这样,程序的封装性显得很好,值得学习。 + +​ 代码示例: + +```java +@Override +public byte[] read(int startPos, int endPos) throws IOException { + //创建连接 + connect = (HttpURLConnection)url.openConnection(); + //纠结了许久的问题,原来是在setRequestProperty之前不可以使用getContentLength + connect.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + //获取数据 + InputStream is = connect.getInputStream(); + byte[] buff = new byte[BUFFER_SIZE];//建立临时缓存区 + int totalLength = endPos - startPos + 1; + //byte数组输出流,如此看的话,命名还是很有意思的 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + //将输入流中的数据先读到缓存区中,再将缓存区中的数据写入输出流中 + while(baos.size() < totalLength){ + int len = is.read(buff); + if (len < 0) { + break; + } + baos.write(buff,0,len); + } + + //防止数组越界。这也就一位置上方的len应该只有-1和1024两种值吧,所以最后一次的读取会造成有少量的留白,这个操作是用来去除留白的。 + if (baos.size() > totalLength) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLength);//转成byte[]的一种方法 + } + + return baos.toByteArray();//转成byte[]的另一种方法 +} + +//这个byte[]如何使用呢? +RandomAccessFile file = new RandomAccessFile("filename", "rw"); +file.seek(startPos); +file.write(data);//放在这里写入即可。 +file.close(); +``` + + + + + diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java index b807a9b321..78bd364577 100644 --- a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java @@ -20,6 +20,10 @@ public class ArrayUtil { */ public static int[] reverseArray(int[] origin){ int length = origin.length; + if(origin == null || length == 0){ + return null; + } + for(int i=0;i collection){ + public static int[] returnByIntArray(Collection collection){ int[] returnArray = new int[collection.size()]; int i = 0; for(Iterator it = collection.iterator(); it.hasNext();){ diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java deleted file mode 100644 index 007a6e48c3..0000000000 --- a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.github.chaoswang.learning.java.collection.myown; - -import java.util.NoSuchElementException; - -public class MyLinkedList { - private int size = 0; - private Node head = null; - private Node tail = null; - - // - public void add(E element){ - Node tmp = new Node(element, null); - if(tail == null){ - head = tmp; - }else{ - tail.next = tmp;; - } - tail = tmp; - size++; - } - - public void add(int index, E element){ - if(index < 0 || index > size){ - throw new IndexOutOfBoundsException(); - } - Node tmpBefore = getElement(index -1); - Node tmpAfter = getElement(index); - Node tmp = new Node(element, tmpAfter); - tmpBefore.next = tmp; - - } - - public void addFirst(E element){ - Node tmp = new Node(element, null); - if(head != null){ - tmp.next = head; - }else{ - tail = tmp; - } - head = tmp; - } - - public E removeFirst(){ - if(size <= 0){ - throw new NoSuchElementException(); - } - Node tmp = head; - head = head.next; - return (E)tmp.element; - } - - // - public E remove(int index) { - if(index < 0 || index >= size()){ - throw new IndexOutOfBoundsException(); - } - Node tmpBeore = this.getElement(index-1); - Node tmp = this.getElement(index); - Node tmpNext = this.getElement(index+1); - tmpBeore.next = tmpNext; - size--; - return (E)tmp.element; - } - - // - public E get(int index){ - return (E)this.getElement(index).element; - } - - public int size() { - return size; - } - - @Override - public String toString() { - if(head == null){ - return "[]"; - } - StringBuffer sb = new StringBuffer("["); - Node tmp = head; - while(tmp != null){ - sb.append(tmp.element.toString()); - sb.append(","); - tmp = tmp.next; - } - String returnStr = sb.toString(); - returnStr = returnStr.substring(0, returnStr.length()-1); - return returnStr + "]"; - } - - private Node getElement(int index) { - Node tmp = head; - for(int i=0;i { - private int size = 0; - private int initialSize; - private Object[] elements = null;//ԸΪԼArrayListʵ - - public MyStack(int initialSize){ - this.initialSize = initialSize; - elements = new Object[initialSize]; - } - - //ѹջ - public void push(E element){ - //ﵽޣinitialSize100% - if(++size == elements.length){ - elements = Arrays.copyOf(elements, size + initialSize); - } - elements[size - 1] = element; - } - - //жջǷΪ - public boolean empty(){ - return size <= 0? true : false; - } - - public int size(){ - return size; - } - - //鿴ջԪ - public E peek(){ - if(size == 0){ - throw new EmptyStackException(); - } - return (E)elements[size - 1]; - } - - //ջԪ - public E pop(){ - if(size == 0){ - throw new EmptyStackException(); - } - E removed = (E)elements[size -1]; - elements[size -1] = null; - size--; - return removed; - } - - public int search(E element) { - int index = 0; - for (int i = 0; i < size - 1; i++) { - if (element.equals(elements[i])) { - index = (size -1) - i; - break; - } - } - return index; - } -} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/DownloadThread.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/DownloadThread.java new file mode 100644 index 0000000000..766393f535 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/DownloadThread.java @@ -0,0 +1,49 @@ +package com.github.chaoswang.learning.java.downloader; + +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.github.chaoswang.learning.java.downloader.api.Connection; + +public class DownloadThread extends Thread{ + + private Connection conn; + private int startPos; + private int endPos; + + public DownloadThread(Connection conn, int totalSection, int sectionIndex){ + this.conn = conn; + int contentLength = conn.getContentLength(); + initStartPosAndEndPos(contentLength, totalSection, sectionIndex); + } + + private void initStartPosAndEndPos(int contentLength, int totalSection, int sectionIndex){ + int sectionLength = contentLength / totalSection; + startPos = (sectionIndex - 1) * sectionLength; + if(sectionIndex == totalSection){ + endPos = contentLength - 1; + }else{ + endPos = sectionIndex * sectionLength - 1; + } + } + + public void run(){ + try { + writeByteArrayToFile(conn.read(startPos, endPos), "F:\\tmp\\6977.png"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void writeByteArrayToFile(byte[] buf, String destFilePath){ + try { + // һɶдļ + RandomAccessFile out = new RandomAccessFile(destFilePath, "rw"); + out.seek(startPos);// ָλÿʼд + out.write(buf); + out.close();// ﵽرļ + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/FileDownloader.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/FileDownloader.java new file mode 100644 index 0000000000..1ce8f8cfe6 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/FileDownloader.java @@ -0,0 +1,90 @@ +package com.github.chaoswang.learning.java.downloader; + +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +import com.github.chaoswang.learning.java.downloader.api.Connection; +import com.github.chaoswang.learning.java.downloader.api.ConnectionException; +import com.github.chaoswang.learning.java.downloader.api.ConnectionManager; +import com.github.chaoswang.learning.java.downloader.api.DownloadListener; + +public class FileDownloader { + String url; + DownloadListener listener; + ConnectionManager cm; + int threadNum = 10; + + public FileDownloader(String _url, int threadNum) { + this.url = _url; + this.threadNum = threadNum; + } + + public void execute(){ + // ʵĴ룬 ע⣺ Ҫö߳ʵ + // ӿ, Ҫд⼸ӿڵʵִ + // (1) ConnectionManager , ԴһӣͨConnectionԶȡеһΣstartPos, endPosָ + // (2) DownloadListener, Ƕ߳أ Ŀͻ˲֪ʲôʱҪʵֵ + // ̶ִ߳Ժ listenernotifiedFinished ͻ˾յ֪ͨ + // ʵ˼· + // 1. ҪConnectionManageropenӣ ȻͨConnection.getContentLengthļij + // 2. 3߳أ עÿ߳ҪȵConnectionManageropen + // Ȼread readжȡļĿʼλúͽλõIJ ֵbyte[] + // 3. byteд뵽ļ + // 4. е̶߳Ժ ҪlistenernotifiedFinished + + // Ĵʾ룬 Ҳ˵ֻһ̣߳ Ҫɶ̵߳ġ + // οhttp://blog.csdn.net/yan8024/article/details/46474239 + + long startTime = System.currentTimeMillis(); + //ж߳Ƿ + ArrayList list = new ArrayList(); + + try{ + for(int i=1; i<=threadNum; i++){ + Connection conn = cm.open(url); + Thread dt = new DownloadThread(conn, threadNum, i); + dt.start(); + list.add(dt); + } + }catch(ConnectionException e){ + e.printStackTrace(); + return; + } + + while(true){ + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + if(!isAllFinished(list)){ + continue; + } + System.out.println("finished, cost time:" + (System.currentTimeMillis() - startTime)); + listener.notifyFinished(); + break; + } + } + + private boolean isAllFinished(ArrayList list){ + for(Thread t : list){ + if(t.getState() != Thread.State.TERMINATED){ + return false; + } + } + return true; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/Connection.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/Connection.java new file mode 100644 index 0000000000..32d891f9c7 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/Connection.java @@ -0,0 +1,23 @@ +package com.github.chaoswang.learning.java.downloader.api; + +import java.io.IOException; + +public interface Connection { + /** + * ʼͽλã ȡݣ ֵֽ + * @param startPos ʼλã 0ʼ + * @param endPos λ + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * õݵij + * @return + */ + public int getContentLength(); + + /** + * ر + */ + public void close(); +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/ConnectionException.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/ConnectionException.java new file mode 100644 index 0000000000..b4dc450098 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/ConnectionException.java @@ -0,0 +1,10 @@ +package com.github.chaoswang.learning.java.downloader.api; + +public class ConnectionException extends Exception { + private static final long serialVersionUID = -6832188361613061488L; + + public ConnectionException(String message){ + super(message); + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/ConnectionManager.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/ConnectionManager.java new file mode 100644 index 0000000000..4c8e83a07f --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.github.chaoswang.learning.java.downloader.api; + +public interface ConnectionManager { + /** + * һurl , һ + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/DownloadListener.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/DownloadListener.java new file mode 100644 index 0000000000..2b3d2ba7f7 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.github.chaoswang.learning.java.downloader.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java new file mode 100644 index 0000000000..6b57ef3269 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java @@ -0,0 +1,73 @@ +package com.github.chaoswang.learning.java.downloader.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Arrays; + +import com.github.chaoswang.learning.java.downloader.api.Connection; + +public class ConnectionImpl implements Connection{ + + private HttpURLConnection conn; + private InputStream is; + + public ConnectionImpl(String url){ + initConn(url); + } + + private void initConn(String url){ + try{ + URL httpUrl = new URL(url); + conn = (HttpURLConnection)httpUrl.openConnection(); + conn.setConnectTimeout(3 * 1000); + conn.connect(); + is = conn.getInputStream(); + }catch(Exception e){ + e.printStackTrace(); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + //˼ǣܶ1024Ҳܲ1024 + byte[] buffer = new byte[1024]; + is.skip(startPos); + //߳Ҫصֽ + int currentSectionLength = endPos - startPos + 1; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int len = 0; + int hasRead = 0; + while((len < currentSectionLength) && ((hasRead = is.read(buffer)) != -1)) { + bos.write(buffer, 0, hasRead); + len += hasRead; + } + bos.close(); + is.close(); + //bytesܱcurrentSectionLengthԶ࣬ȡ + byte[] downloadedBytes = bos.toByteArray(); + byte[] needToDownload = Arrays.copyOf(downloadedBytes, currentSectionLength); + return needToDownload; + } + + @Override + public int getContentLength() { + int length = conn.getContentLength(); + System.out.println("length:" + length); + return length; + } + + @Override + public void close() { + try { + if(is != null){ + is.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionManagerImpl.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..0c9245b8f4 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionManagerImpl.java @@ -0,0 +1,23 @@ +package com.github.chaoswang.learning.java.downloader.impl; + +import com.github.chaoswang.learning.java.downloader.api.Connection; +import com.github.chaoswang.learning.java.downloader.api.ConnectionException; +import com.github.chaoswang.learning.java.downloader.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection conn = null; + if (url.startsWith("http")){ + conn = new ConnectionImpl(url); + }else if(url.startsWith("ftp")){ + //TODO + } + if(conn == null){ + throw new ConnectionException("Failed to get conneciton."); + } + return conn; + } + +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/ClassFileLoader.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/ClassFileLoader.java new file mode 100644 index 0000000000..1b0b8f1514 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/ClassFileLoader.java @@ -0,0 +1,24 @@ +package com.github.chaoswang.learning.java.jvm; + +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + return null; + + } + + public void addClassPath(String path) { + + } + + public String getClassPath() { + return null; + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/linkedlist/LRUPageFrame.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/linkedlist/LRUPageFrame.java new file mode 100644 index 0000000000..01bf2a7333 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/linkedlist/LRUPageFrame.java @@ -0,0 +1,151 @@ +package com.github.chaoswang.learning.java.linkedlist; + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// ͷ + private Node last;// β + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * ȡж + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + // ڸöдڣ ᵽͷ + if (node != null) { + + moveExistingNodeToHead(node); + + } else { + + node = new Node(); + node.pageNum = pageNum; + + // ǷѾС. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + } + } + + private void addNewNodetoHead(Node node) { + + if (isEmpty()) { + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else { + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize++; + } + + private Node find(int data) { + + Node node = first; + while (node != null) { + if (node.pageNum == data) { + return node; + } + node = node.next; + } + return null; + + } + + /** + * ɾβڵ ʾ ɾʹõĻ + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize--; + } + + /** + * ƶͷʾڵʹù + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } else if (node == last) { + // ǰڵβ Ҫŵͷ + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else { + // node м䣬 node ǰڵ + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + + private boolean isEmpty() { + return (first == null) && (last == null); + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/linkedlist/LinkedList.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/linkedlist/LinkedList.java new file mode 100644 index 0000000000..3f721d4793 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/linkedlist/LinkedList.java @@ -0,0 +1,287 @@ +package com.github.chaoswang.learning.java.linkedlist; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.NoSuchElementException; +import java.util.Stack; + +import com.github.chaoswang.learning.java.array.ArrayUtil; + +public class LinkedList { + //޸IJҪάsize + private int size = 0; + //漰βڵʱҪάheadtailָ + private Node head = null; + private Node tail = null; + + // + public void add(E element){ + Node tmp = new Node(element, null); + if(tail == null){ + head = tmp; + }else{ + tail.next = tmp;; + } + tail = tmp; + size++; + } + + public void add(int index, E element){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + if(index == size){ + add(element); + return; + }else if(index == 0){ + addFirst(element); + return; + } + Node tmpBefore = getNode(index -1); + Node tmpAfter = getNode(index); + Node tmp = new Node(element, tmpAfter); + tmpBefore.next = tmp; + size++; + } + + public void addFirst(E element){ + Node tmp = new Node(element, null); + if(head != null){ + tmp.next = head; + }else{ + tail = tmp; + } + head = tmp; + size++; + } + + public E removeFirst(){ + if(size <= 0){ + throw new NoSuchElementException(); + } + Node tmp = head; + head = head.next; + size--; + if(size == 0){ + tail = null; + } + return (E)tmp.element; + } + + // + public E remove(int index) { + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException(); + } + if(index == 0){ + return removeFirst(); + } + Node tmpBefore = this.getNode(index-1); + Node tmp = this.getNode(index); + Node tmpNext = this.getNode(index+1); + tmpBefore.next = tmpNext; + if(index == size - 1){ + tail = tmpBefore; + } + size--; + if(size == 0){ + tail = null; + } + return (E)tmp.element; + } + + // + public E get(int index){ + return (E)this.getNode(index).element; + } + + public int size() { + return size; + } + + @Override + public String toString() { + if(head == null){ + return "[]" + ", head:"+head+", tail:"+tail; + } + StringBuffer sb = new StringBuffer("["); + Node tmp = head; + while(tmp != null){ + sb.append(tmp.element.toString()); + sb.append("("); + if(tmp.next!=null){ + sb.append(tmp.next.element.toString()); + } + sb.append(")"); + sb.append(","); + + tmp = tmp.next; + } + String returnStr = sb.toString(); + returnStr = returnStr.substring(0, returnStr.length()-1); + return returnStr + "]" + ", head:"+head+", tail:"+tail; + } + + private Node getNode(int index) { + Node tmp = head; + for(int i=0;i7->10 , úΪ 10->7->3 + */ + public void reverse(){ + Stack stackCache = new Stack(); + Node currentNode = head; + for(int i=0;i5->7->8 , ɾԺֵΪ 7->8 + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + */ + public void removeFirstHalf(){ + Node halfNodeBefore = getNode((size/2)-1); + head = halfNodeBefore.next; + halfNodeBefore.next = null; + } + + /** + * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * list = 2->5->7->8->10 ,remove(2,2)ԺֵΪ2->5->10 + * @param i + * @param length + */ + public void remove(int i, int length){ + Node nodePointer = getNode(i-1); + Node nodeTargetBefore = getNode(i-1+length); + nodePointer.next = nodeTargetBefore.next; + nodeTargetBefore.next = null; + } + + /** + * ٶǰlistBе + * ӵǰȡЩlistBָԪ + * 統ǰ = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * صĽӦ[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + ArrayList cache = new ArrayList(); + for(int i=0;i101->201->301->401->501->601->701 + * listB = [11,201,501,701] + * صĽӦ[101,301,401,601] + * @param list + */ + public void subtract(LinkedList list){ + HashSet set = new HashSet(); + for(int i=0;i101->201->301->401->501->601->701 + * listB = [11,201,801,901] + * صĽӦ[11,201] + * @param list + */ + public LinkedList intersection(LinkedList list){ + HashSet set = new HashSet(); + for(int i=0;i { + private int size = 0; + private int initialSize; + private Object[] elements = null;//ԸΪԼArrayListʵ + + public Stack(int initialSize){ + this.initialSize = initialSize; + elements = new Object[initialSize]; + } + + //ѹջ + public void push(E element){ + //ﵽޣinitialSize100% + if(++size == elements.length){ + elements = Arrays.copyOf(elements, size + initialSize); + } + elements[size - 1] = element; + } + + //жջǷΪ + public boolean empty(){ + return size <= 0? true : false; + } + + public int size(){ + return size; + } + + //鿴ջԪ + public E peek(){ + if(size == 0){ + throw new EmptyStackException(); + } + return (E)elements[size - 1]; + } + + //ջԪ + public E pop(){ + if(size == 0){ + throw new EmptyStackException(); + } + E removed = (E)elements[size -1]; + elements[size -1] = null; + size--; + return removed; + } + + public int search(E element) { + int index = 0; + for (int i = 0; i < size - 1; i++) { + if (element.equals(elements[i])) { + index = (size -1) - i; + break; + } + } + return index; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java new file mode 100644 index 0000000000..d5d7ecc223 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java @@ -0,0 +1,45 @@ +package com.github.chaoswang.learning.java.stack; + +public class StackUtil { + + /** + * ջеԪInteger, ջջ : 5,4,3,2,1 ø÷ ԪشΪ: 1,2,3,4,5 + * ע⣺ֻʹStackĻpush,pop,peek,isEmpty ʹһջ + */ + public static void reverse(Stack s) { + + } + + /** + * ɾջеijԪ ע⣺ֻʹStackĻpush,pop,peek,isEmpty ʹһջ + * + * @param o + */ + public static void remove(Stack s, Object o) { + + } + + /** + * ջȡlenԪ, ԭջԪرֲ ע⣺ֻʹStackĻpush,pop,peek,isEmpty + * ʹһջ + * + * @param len + * @return + */ + public static Object[] getTop(Stack s, int len) { + return null; + } + + /** + * ַs ܰЩַ ( ) [ ] { }, a,b,c... x,yz ʹöջַsеDzdzɶԳֵġ s = + * "([e{d}f])" , ַеdzɶԳ֣ ÷true s = "([b{x]y})", + * ַеŲdzɶԳֵģ ÷false; + * + * @param s + * @return + */ + public static boolean isValidPairs(String s) { + return false; + } + +} \ No newline at end of file diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java deleted file mode 100644 index cdbbcf2812..0000000000 --- a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.github.chaoswang.learning.java.collection.myown; - -import org.junit.Assert; -import org.junit.Test; - -public class MyLinkedListTest { - - @Test - public void testAdd(){ - MyLinkedList myList = new MyLinkedList(); - myList.add("1"); - myList.add("2"); - myList.add("3"); - Assert.assertEquals(3, myList.size()); - myList.add("4"); - Assert.assertEquals(4, myList.size()); - System.out.println(myList); - String str = myList.get(2); - Assert.assertEquals("3", str); - - } - - @Test - public void testInsert(){ - MyLinkedList myList = new MyLinkedList(); - myList.add("1"); - myList.add("2"); - myList.add("4"); - String str = myList.get(2); - Assert.assertEquals("4", str); - myList.add(2,"3"); - str = myList.get(2); - Assert.assertEquals("3", str); - } - - @Test - public void testAddFirst(){ - MyLinkedList myList = new MyLinkedList(); - myList.add("2"); - myList.add("3"); - myList.add("4"); - System.out.println(myList); - Assert.assertEquals("2", myList.get(0)); - myList.addFirst("1"); - Assert.assertEquals("1", myList.get(0)); - System.out.println(myList); - } - - @Test - public void testRemoveFirst(){ - MyLinkedList myList = new MyLinkedList(); - myList.add("1"); - myList.add("2"); - myList.add("3"); - myList.add("4"); - String str = myList.removeFirst(); - System.out.println(myList); - Assert.assertEquals("1", str); - Assert.assertEquals("2", myList.get(0)); - } - - @Test - public void testRemove(){ - MyLinkedList myList = new MyLinkedList(); - myList.add("1"); - myList.add("2"); - myList.add("3"); - myList.add("4"); - String str = myList.remove(2); - Assert.assertEquals("3", str); - str = myList.get(2); - Assert.assertEquals("4", str); - Assert.assertEquals(3, myList.size()); - } - -} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java index 4ae0c84989..71336bd868 100644 --- a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java @@ -7,6 +7,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; +import com.github.chaoswang.learning.java.stack.Stack; + public class MyStackTest { @Rule @@ -14,7 +16,7 @@ public class MyStackTest { @Test public void testPushAndPop(){ - MyStack myStack = new MyStack(3); + Stack myStack = new Stack(3); myStack.push("1"); myStack.push("2"); myStack.push("3"); @@ -27,7 +29,7 @@ public void testPushAndPop(){ @Test public void testPopWhenQueueIsEmpty(){ thrown.expect(EmptyStackException.class); - MyStack myStack = new MyStack(3); + Stack myStack = new Stack(3); myStack.push("1"); myStack.pop(); //Ƴ쳣 @@ -36,7 +38,7 @@ public void testPopWhenQueueIsEmpty(){ @Test public void testSearch(){ - MyStack myStack = new MyStack(3); + Stack myStack = new Stack(3); myStack.push("1"); myStack.push("2"); myStack.push("3"); diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/downloader/FileDownloaderTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/downloader/FileDownloaderTest.java new file mode 100644 index 0000000000..f47232bf2e --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/downloader/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.github.chaoswang.learning.java.downloader; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.chaoswang.learning.java.downloader.api.ConnectionManager; +import com.github.chaoswang.learning.java.downloader.api.DownloadListener; +import com.github.chaoswang.learning.java.downloader.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://desk.fd.zol-img.com.cn/t_s2560x1600c5/g5/M00/02/09/ChMkJ1bKzeqIXxeTACOMfnPW4wsAALJFgHb1LMAI4yW109.jpg"; + + FileDownloader downloader = new FileDownloader(url, 1); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // ȴ߳سִ + while (!downloadFinished) { + try { + System.out.println("ûɣ"); + //5 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("ɣ"); + + + + } + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/jvm/ClassFileloaderTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/jvm/ClassFileloaderTest.java new file mode 100644 index 0000000000..25f2d1afb8 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/jvm/ClassFileloaderTest.java @@ -0,0 +1,76 @@ +package com.github.chaoswang.learning.java.jvm; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // ע⣺ֽܺJVM汾йϵ Կõൽж + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/jvm/EmployeeV1.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/jvm/EmployeeV1.java new file mode 100644 index 0000000000..1855f34274 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/jvm/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.github.chaoswang.learning.java.jvm; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/linkedlist/LRUPageFrameTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/linkedlist/LRUPageFrameTest.java new file mode 100644 index 0000000000..eb0700e609 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/linkedlist/LRUPageFrameTest.java @@ -0,0 +1,33 @@ +package com.github.chaoswang.learning.java.linkedlist; + +import org.junit.Assert; + +import org.junit.Test; + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} \ No newline at end of file diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/linkedlist/LinkedListTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/linkedlist/LinkedListTest.java new file mode 100644 index 0000000000..1b47f86544 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/linkedlist/LinkedListTest.java @@ -0,0 +1,239 @@ +package com.github.chaoswang.learning.java.linkedlist; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +import com.github.chaoswang.learning.java.linkedlist.LinkedList; + +public class LinkedListTest { + + @Test + public void testAdd(){ + LinkedList myList = new LinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + System.out.println(myList); + Assert.assertEquals(3, myList.size()); + myList.add("4"); + System.out.println(myList); + Assert.assertEquals(4, myList.size()); + String str = myList.get(2); + Assert.assertEquals("3", str); + System.out.println(myList); + } + + @Test + public void testInsert(){ + LinkedList myList = new LinkedList(); + myList.add("2"); + myList.add("4"); + System.out.println(myList); + myList.add(0,"1"); + System.out.println(myList); + String str = myList.get(2); + Assert.assertEquals("4", str); + myList.add(2,"3"); + str = myList.get(2); + System.out.println(myList); + Assert.assertEquals("3", str); + } + + @Test + public void testAddFirst(){ + LinkedList myList = new LinkedList(); + myList.add("2"); + myList.add("3"); + myList.add("4"); + System.out.println(myList); + Assert.assertEquals("2", myList.get(0)); + myList.addFirst("1"); + Assert.assertEquals("1", myList.get(0)); + System.out.println(myList); + } + + @Test + public void testRemoveFirst(){ + LinkedList myList = new LinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.removeFirst(); + Assert.assertEquals("1", str); + Assert.assertEquals("2", myList.get(0)); + System.out.println(myList); + } + + @Test + public void testRemove(){ + LinkedList myList = new LinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.remove(2); + System.out.println(myList); + Assert.assertEquals("3", str); + str = myList.get(2); + Assert.assertEquals("4", str); + Assert.assertEquals(3, myList.size()); + System.out.println(myList); + } + + @Test + public void testRemoveAll(){ + LinkedList myList = new LinkedList(); + myList.add("1"); + myList.add("2"); + System.out.println(myList); + String str = myList.removeFirst(); + System.out.println(myList); + Assert.assertEquals("1", str); + str = myList.removeFirst(); + Assert.assertEquals("2", str); + Assert.assertEquals(0, myList.size()); + System.out.println(myList); + } + + /** + * Ѹ + * Ϊ 3->7->10 , úΪ 10->7->3 + */ + @Test + public void testReverse(){ + LinkedList myList = new LinkedList(); + myList.add("1"); + myList.add("3"); + myList.add("5"); + myList.add("7"); + myList.add("9"); + System.out.println(myList); + myList.reverse(); + System.out.println(myList); + } + + /** + * ɾһǰ벿 + * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + */ + @Test + public void testRemoveFirstHalf(){ + LinkedList myList = new LinkedList(); + myList.add("2"); + myList.add("5"); + myList.add("7"); + myList.add("8"); + myList.add("10"); + System.out.println(myList); + myList.removeFirstHalf(); + System.out.println(myList); + } + + /** + * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * list = 2->5->7->8->10 ,remove(2,2)ԺֵΪ2->5->10 + * @param i + * @param length + */ + @Test + public void testRemoveBySpecificLength(){ + LinkedList myList = new LinkedList(); + myList.add("2"); + myList.add("5"); + myList.add("7"); + myList.add("8"); + myList.add("10"); + System.out.println(myList); + myList.remove(2,2); + System.out.println(myList); + } + + /** + * ٶǰlistBе + * ӵǰȡЩlistBָԪ + * 統ǰ = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * صĽӦ[101,301,401,601] + * @param list + */ + @Test + public void testGetElements(){ + LinkedList myList = new LinkedList(); + myList.add("11"); + myList.add("101"); + myList.add("201"); + myList.add("301"); + myList.add("401"); + myList.add("501"); + myList.add("601"); + myList.add("701"); + System.out.println(myList); + LinkedList testList = new LinkedList(); + testList.add(1); + testList.add(3); + testList.add(4); + testList.add(6); + System.out.println(Arrays.toString(myList.getElements(testList))); + } + + /** + * ֪еԪֵУԵ洢ṹ + * ӵǰɾlistBгֵԪ + * 統ǰ = 11->101->201->301->401->501->601->701 + * listB = [11,201,501,701] + * صĽӦ[101,301,401,601] + * @param list + */ + @Test + public void subtract(){ + LinkedList myList = new LinkedList(); + myList.add("11"); + myList.add("101"); + myList.add("201"); + myList.add("301"); + myList.add("401"); + myList.add("501"); + myList.add("601"); + myList.add("701"); + System.out.println(myList); + LinkedList testList = new LinkedList(); + testList.add(11); + testList.add(201); + testList.add(501); + testList.add(701); + myList.subtract(testList); + System.out.println(myList); + } + + /** + * 赱ǰͲlistָԪֵУͬһеԪֵͬ + * ҪCԪΪǰlistԪصĽұCеԪֵ + * 統ǰ = 11->101->201->301->401->501->601->701 + * listB = [11,201,801,901] + * صĽӦ[11,201] + * @param list + */ + @Test + public void intersection(){ + LinkedList myList = new LinkedList(); + myList.add("11"); + myList.add("101"); + myList.add("201"); + myList.add("301"); + myList.add("401"); + myList.add("501"); + myList.add("601"); + myList.add("701"); + System.out.println(myList); + LinkedList testList = new LinkedList(); + testList.add(11); + testList.add(201); + testList.add(801); + testList.add(901); + System.out.println(myList.intersection(testList)); + } +} diff --git a/group06/547958234/src/com/coderising/array/ArrayUtil.java b/group06/547958234/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..2f0d91bad1 --- /dev/null +++ b/group06/547958234/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,163 @@ +package com.coderising.array; + + +import sun.reflect.generics.tree.VoidDescriptor; + +import java.awt.event.InputMethodListener; +import java.awt.image.AreaAveragingScaleFilter; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + int mid; + if (origin.length % 2 == 0) { + mid = origin.length / 2 - 1; + } else { + mid = origin.length / 2; + } + for (int i = 0; i <= mid; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + size++; + } + } + int[] newArray = new int[size]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) newArray[j++] = oldArray[i]; + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int[] mergedArray = new int[array1.length + array2.length]; + int i = 0; + int j = 0; + int k = 0; + while (i < array1.length) { + while (j < array2.length) { + if (array1[i] > array2[j]) { + mergedArray[k++] = array2[j++]; + } else { + mergedArray[k++] = array1[i++]; + } + } + } + return mergedArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int newSize = oldArray.length + size; + int[] newArray = new int[newSize]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String s = ""; + for (int i = 0; i < array.length; i++) { + if (i != array.length - 1) { + s += array[i] + seperator; + } + } + s += array[array.length -1]; + return s; + } + + public static void main(String[] args) { + int[] array = {1, 2, 3, 4, 0, 1, 0, 2}; + reverseArray(array); + for (int i = 0; i < array.length; i++) System.out.print(array[i]); + int[] newArray = removeZero(array); + for (int i = 0; i < newArray.length; i++) System.out.print(newArray[i]); + } +} diff --git a/group06/547958234/src/com/coderising/download/DownloadThread.java b/group06/547958234/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/group06/547958234/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group06/547958234/src/com/coderising/download/FileDownloader.java b/group06/547958234/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/group06/547958234/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group12/446031103/src/com/coderising/download/FileDownloaderTest.java b/group06/547958234/src/com/coderising/download/FileDownloaderTest.java similarity index 100% rename from group12/446031103/src/com/coderising/download/FileDownloaderTest.java rename to group06/547958234/src/com/coderising/download/FileDownloaderTest.java diff --git a/group06/547958234/src/com/coderising/download/api/Connection.java b/group06/547958234/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group06/547958234/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group06/547958234/src/com/coderising/download/api/ConnectionException.java b/group06/547958234/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group06/547958234/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group06/547958234/src/com/coderising/download/api/ConnectionManager.java b/group06/547958234/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group06/547958234/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group06/547958234/src/com/coderising/download/api/DownloadListener.java b/group06/547958234/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group06/547958234/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group06/547958234/src/com/coderising/download/impl/ConnectionImpl.java b/group06/547958234/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/group06/547958234/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group06/547958234/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group06/547958234/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/group06/547958234/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group06/547958234/src/com/coderising/litestruts/LoginAction.java b/group06/547958234/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group06/547958234/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group06/547958234/src/com/coderising/litestruts/Struts.java b/group06/547958234/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group06/547958234/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group06/547958234/src/com/coderising/litestruts/StrutsTest.java b/group06/547958234/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group06/547958234/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/View.java b/group06/547958234/src/com/coderising/litestruts/View.java similarity index 100% rename from group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/View.java rename to group06/547958234/src/com/coderising/litestruts/View.java diff --git a/group06/949319266/Test/src/com/ecust/test/GArrayList.java b/group06/949319266/Test/src/com/ecust/test/GArrayList.java new file mode 100644 index 0000000000..e0282ec416 --- /dev/null +++ b/group06/949319266/Test/src/com/ecust/test/GArrayList.java @@ -0,0 +1,148 @@ +package com.ecust.test; +import java.util.*; +public class GArrayList implements GList { + + private int size; + private Object[] dataArray= new Object[0]; + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return this.size == 0; + } + + @Override + public boolean contains(Object o) { + for(Object obj:dataArray) { + if(Objects.equals(obj, o)) + return true; + } + return false; + } + + @Override + public Object[] toArray() { + Object[] array = new Object[size]; + System.arraycopy(dataArray, 0, array, 0, size); + return array; + } + + @Override + public boolean add(T t) { + ensureCapacity(size+1); + dataArray[size] = t; + size++; + return true; + } + + + + @Override + public boolean remove(T t) { + int index = indexof(t); + if(index < 0) { + return false; + } + System.arraycopy(dataArray, index+1, dataArray, index, size-1-index); + dataArray[size-1] = null; + size--; + return true; + } + + @Override + public void clear() { + dataArray = new Object[size]; + size = 0; + } + + @Override + public T get(int index) { + if(index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + return (T)dataArray[index]; + } + + @Override + public T set(int index, T t) { + if(index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + dataArray[index] = t; + return t; + } + + @Override + public void add(int index, T t) { + if(index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + ensureCapacity(size+1); + System.arraycopy(dataArray, index, dataArray, index+1, size-index); + dataArray[index] = t; + size++; + } + + @Override + public T remove(int index) { + if(index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + T element = (T)dataArray[index]; + System.arraycopy(dataArray, index+1, dataArray, index, size-1-index); + dataArray[size-1] = null; + size--; + return element; + } + + @Override + public int indexof(T t) { + for(int i = 0;i iterator() { + return new ArrayListIterator(this); + } + private void ensureCapacity(int i) { + if(i > dataArray.length) { + int newlength = Math.max(i, dataArray.length*2); + Object[] newDataArray = new Object[newlength]; + System.arraycopy(dataArray, 0, newDataArray, 0, dataArray.length); + dataArray = newDataArray; + } + } + private class ArrayListIterator implements GIterator { + private int position; + private GArrayList list; + + ArrayListIterator(GArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + + return position < list.size; + } + + @Override + public T next() { + if(hasNext()) { + return list.get(position++); + } + return null; + } + + } + +} diff --git a/group06/949319266/Test/src/com/ecust/test/GIterator.java b/group06/949319266/Test/src/com/ecust/test/GIterator.java new file mode 100644 index 0000000000..08ae05f395 --- /dev/null +++ b/group06/949319266/Test/src/com/ecust/test/GIterator.java @@ -0,0 +1,6 @@ +package com.ecust.test; + +public interface GIterator { + boolean hasNext(); + T next(); +} diff --git a/group06/949319266/Test/src/com/ecust/test/GList.java b/group06/949319266/Test/src/com/ecust/test/GList.java new file mode 100644 index 0000000000..00098b2d55 --- /dev/null +++ b/group06/949319266/Test/src/com/ecust/test/GList.java @@ -0,0 +1,18 @@ +package com.ecust.test; + +public interface GList { + int size(); + boolean isEmpty(); + boolean contains(Object o); + Object[] toArray(); + boolean add(T t); + boolean remove(T t); + void clear(); + T get (int index); + T set (int index,T t); + void add(int index,T t); + T remove(int index); + int indexof(T t); + GIterator iterator(); + +} diff --git a/group06/949319266/homework/src/com/coding/basic/LinkedList.java b/group06/949319266/homework/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..b891cdd2d6 --- /dev/null +++ b/group06/949319266/homework/src/com/coding/basic/LinkedList.java @@ -0,0 +1,680 @@ +package com.coding.basic; + +import java.util.Iterator; + + +public class LinkedList implements List { + + + + private Node head; + + private int size; + + + + private static class Node { + + Object data; + + Node next; + + + + public Node(Object data){ + + this.data = data; + + this.next = null; + + } + + } + + + + public LinkedList(){ + + this.head = new Node(null); + + this.size = 0; + + } + + + + public void add(Object o){ + + Node newNode = new Node(o); + + Node pNode = head; + + while(pNode.next != null){ + + pNode = pNode.next; + + } + + + + pNode.next = newNode; + + size++; + + } + + public void add(int index , Object o){ + + checkIndex(index); + + + + Node newNode = new Node(o); + + Node node = new Node(null); + + Node pNode = head; + + for(int i = 0; i < index; i++){ + + node = pNode; + + pNode = pNode.next; + + } + + + + node.next = newNode; + + newNode.next = pNode; + + size++; + + } + + public Object get(int index){ + + checkIndex(index); + + + + Node pNode = head; + + for(int i = 0; i < index; i++){ + + pNode = pNode.next; + + } + + + + return pNode.data; + + } + + public Object remove(int index){ + + checkIndex(index); + + if(size == 0){ + + return null; + + } + + + + Node node = new Node(null); + + Node pNode = head; + + for(int i = 0; i < index; i++){ + + node = pNode; + + pNode = pNode.next; + + } + + node.next = pNode.next; + + size--; + + + + return pNode; + + } + + + + public int size(){ + + Node pNode = head; + + while(pNode.next != null){ + + pNode = pNode.next; + + size++; + + } + + return size; + + } + + + + public void addFirst(Object o){ + + if(size == 0){ + + head.data = o; + + } + + + + Node newNode = new Node(o); + + Node pNode = head; + + head = newNode; + + newNode.next = pNode.next; + + size++; + + } + + public void addLast(Object o){ + + if(size == 0){ + + head.data = o; + + } + + + + Node newNode = new Node(o); + + Node pNode = head; + + while(pNode.next != null){ + + pNode = pNode.next; + + } + + pNode.next = newNode; + + newNode.next = null; + + size++; + + } + + public Object removeFirst(){ + + if(size == 0){ + + return null; + + } + + + + Node pNode = head; + + head = pNode.next; + + head.next = pNode.next.next; + + size--; + + return pNode; + + } + + public Object removeLast(){ + + if(size == 0){ + + return null; + + } + + + + Node pNode = head; + + Node node = new Node(null); + + while(pNode.next != null){ + + node = pNode; + + pNode = pNode.next; + + } + + + + node.next = null; + + size--; + + return pNode; + + } + + public Iterator iterator(){ + + return new LinkedListIterator(); + + } + + + + //עһ + + public class LinkedListIterator implements Iterator { + + private int position; + + + + @Override + + public boolean hasNext() { + + return position < size(); + + } + + + + @Override + + public Object next() { + + if(hasNext()){ + + return get(position++); + + } + + return null; + + } + + + + } + + + + public void checkIndex(int index){ + + if(index < 0 || index >= size){ + + throw new IndexOutOfBoundsException(); + + } + + } + + + + /** + + * Ѹ + + * Ϊ 3->7->10 , úΪ 10->7->3 + + */ + + public void reverse(Node head){ + + if(head.next == null || head.next.next == null){ + + return; + + } + + + + Node p = head.next; + + Node q = head.next.next; + + Node t = null; + + + + while(q.next != null){ + + t = q.next; + + q.next = p; + + p = q; + + q = t; + + } + + + + head.next.next = null;//β + + head.next = p;//ͷ + + } + + + + /** + + * ɾһǰ벿 + + * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 + + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + + + + */ + + public void removeFirstHalf(){ + + if(size == 0 || head.next == null || head.next.next == null){ + + return; + + } + + + + Node pNode = head; + + Node node = null; + + for(int i = 0; i < size/2; i++){ + + node = pNode; + + pNode = pNode.next; + + } + + + + if(size %2 == 0){ + + head.next = pNode; + + }else{ + + head.next = node; + + } + + } + + + + /** + + * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + + * @param i + + * @param length + + */ + + public void remove(int i, int length){ + + if(size == 0 || head.next == null){ + + return; + + } + + + + for(int k = i; k < i + length; k++){ + + checkIndex(k); + + remove(k); + + } + + } + + /** + + * ٶǰlistе + + * ӵǰȡЩlistָԪ + + * 統ǰ = 11->101->201->301->401->501->601->701 + + * list = 1->3->4->6 + + * صĽӦ[101,301,401,601] + + * @param list + + */ + + public int[] getElements(LinkedList list){ + + if(list.size == 0 || list == null){ + + return new int[0]; + + } + + + + int[] array = new int[list.size]; + + int k = 0; + + for(int i = 0; i < list.size; i++){ + + int index = (int) list.get(i); + + array[k] = (int) get(index); + + } + + return array; + + } + + + + /** + + * ֪еԪֵУԵ洢ṹ + + * ӵǰɾlistгֵԪ + + + + * @param list + + */ + + + + public void subtract(LinkedList list,LinkedList oldList){ + + if(oldList == null || oldList.size ==0 || list == null || list.size == 0){ + + return; + + } + + + + for(int i = 0; i < oldList.size; i++){ + + for(int j = 0; j < list.size; j++){ + + if(list.get(j) == oldList.get(i)){ + + oldList.remove(i); + + } + + } + + } + + } + + + + /** + + * ֪ǰеԪֵУԵ洢ṹ + + * ɾֵͬĶԪأʹòԱԪصֵͬ + + */ + + public void removeDuplicateValues(LinkedList list){ + + if(list == null || list.size == 0){ + + return; + + } + + + + int count = 0; + + Node pNode = head; + + while(pNode.next != null){ + + pNode = pNode.next; + + count++; + + if(pNode.data == pNode.next.data){ + + list.remove(count+1); + + } + + } + + } + + + + /** + + * ֪еԪֵУԵ洢ṹ + + * дһЧ㷨ɾֵminСmaxԪأдԪأ + + * @param min + + * @param max + + */ + + public void removeRange(int min, int max){ + + if(size == 0){ + + return; + + } + + + + int count = 0; + + Node pNode = head; + + while(pNode.next != null){ + + pNode = pNode.next; + + count++; + + if(min < (int)pNode.data || (int)pNode.data < max){ + + remove(count); + + } + + } + + } + + + + /** + + * 赱ǰͲlistָԪֵУͬһеԪֵͬ + + * ҪCԪΪǰlistԪصĽұCеԪֵ + + * @param list + + */ + + public LinkedList intersection( LinkedList list){ + + if(list.size == 0){ + + return null; + + } + + + + LinkedList listC = new LinkedList(); + + Node p = head; + + Node q = list.head; + + + + while(p.next != null){ + + p = p.next; + + while(q.next !=null){ + + q = q.next; + + if(p.data.equals(q.data)){ + + listC.add(p); + + } + + } + + } + + return listC; + + } + +} \ No newline at end of file diff --git a/group06/949319266/homework/src/com/coding/basic/List.java b/group06/949319266/homework/src/com/coding/basic/List.java new file mode 100644 index 0000000000..22573a2ced --- /dev/null +++ b/group06/949319266/homework/src/com/coding/basic/List.java @@ -0,0 +1,17 @@ +package com.coding.basic; + + + +public interface List { + + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); + +} \ No newline at end of file diff --git "a/group06/949319266/homework/src/\346\226\207\347\253\240\344\270\211" "b/group06/949319266/homework/src/\346\226\207\347\253\240\344\270\211" new file mode 100644 index 0000000000..9b7a0eadcc --- /dev/null +++ "b/group06/949319266/homework/src/\346\226\207\347\253\240\344\270\211" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/blog_c20b18280102x3ol.html \ No newline at end of file diff --git a/group06/949319266/homework003/src/DownloadThread.java b/group06/949319266/homework003/src/DownloadThread.java new file mode 100644 index 0000000000..e084ee3109 --- /dev/null +++ b/group06/949319266/homework003/src/DownloadThread.java @@ -0,0 +1,154 @@ + +import java.io.IOException; + +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; + +import com.coderising.download.api.ConnectionException; + +import com.coderising.download.api.ConnectionManager; + +import com.coderising.download.api.DownloadListener; + + + +public class DownloadThread extends Thread { + + + + private int endPos; + + private int startPos; + + private String url; + + private String destFilePath; + + private ConnectionManager connManager; + + private DownloadListener downloadListener; + + + + public DownloadThread(ConnectionManager connManager, String url, int startPos, int endPos, String destFilePath, + + DownloadListener downloadListener) { + + + + this.url = url; + + this.endPos = endPos; + + this.startPos = startPos; + + this.connManager = connManager; + + this.destFilePath = destFilePath; + + this.downloadListener = downloadListener; + + } + + + + @Override + + public void run() { + + Connection conn = null; + + RandomAccessFile randomAccessFile = null; + + try { + + doLog("BIN"); + + conn = connManager.open(url, startPos, endPos); + + byte[] read = conn.read(startPos, endPos); + + String _filePath = destFilePath; + + if (_filePath == null || _filePath.length() == 0) { + + _filePath = conn.getFileName(); + + } + + randomAccessFile = new RandomAccessFile(_filePath, "rw"); + + randomAccessFile.seek(startPos); + + randomAccessFile.write(read); + + doLog("END"); + + } catch (IOException e) { + + doLog("EXP"); + + e.printStackTrace(); + + } catch (ConnectionException e) { + + doLog("EXP"); + + e.printStackTrace(); + + } finally { + + if (randomAccessFile != null) { + + try { + + randomAccessFile.close(); + + } catch (IOException e) { + + e.printStackTrace(); + + } + + } + + if (conn != null) { + + conn.close(); + + } + + if (downloadListener != null) { + + downloadListener.notifyFinished(); + + } + + } + + } + + + + private void doLog(String action) { + + System.out.println( + + "*********** " + action + + + " [" + + + startPos + + + "-" + + + endPos + + + "]" + + + " ***********"); + + } + +} diff --git a/group06/949319266/homework003/src/FileDownloader.java b/group06/949319266/homework003/src/FileDownloader.java new file mode 100644 index 0000000000..39793ef0c6 --- /dev/null +++ b/group06/949319266/homework003/src/FileDownloader.java @@ -0,0 +1,158 @@ + +import java.util.concurrent.atomic.AtomicInteger; + +import com.coderising.download.api.ConnectionException; + +import com.coderising.download.api.ConnectionManager; + +import com.coderising.download.api.DownloadListener; + + + +public class FileDownloader { + + + + private String url; + + + + private DownloadListener listener; + + + + private ConnectionManager cm; + + + + private AtomicInteger atomicInteger; + + + + public FileDownloader(String _url) { + + this.url = _url; + + atomicInteger = new AtomicInteger(); + + } + + + + /** + + * 在这里实现你的代码, 注意: 需要用多线程实现下载 + + * 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + + * (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + + * (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + + * 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + + * 具体的实现思路: + + * 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + + * 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + + * 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + + * 3. 把byte数组写入到文件中 + + * 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + * + + * 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + */ + + public void execute() { + + try { + + + + int threadCount = 5; + + int length = this.cm.getContentLength(this.url); + + for (int i = 0; i < threadCount; i++) { + + + + int threadLoadLength = length / threadCount; + + int startPos = threadLoadLength * i; + + int endPos; + + if (i != threadCount - 1) { + + endPos = threadLoadLength * (i + 1) - 1; + + } else { + + endPos = length - 1; + + } + + atomicInteger.getAndIncrement(); + + new DownloadThread(cm, this.url, startPos, endPos, null, new DownloadListener() { + + @Override + + public void notifyFinished() { + + if (atomicInteger.decrementAndGet() == 0) { + + if (FileDownloader.this.listener != null) { + + FileDownloader.this.listener.notifyFinished(); + + } + + } + + } + + }).start(); + + } + + } catch (ConnectionException e) { + + e.printStackTrace(); + + } + + } + + + + public void setConnectionManager(ConnectionManager ucm) { + + this.cm = ucm; + + } + + + + public DownloadListener getListener() { + + return this.listener; + + } + + + + public void setListener(DownloadListener listener) { + + this.listener = listener; + + } + +} diff --git a/group06/949319266/homework003/src/FileDownloaderTest.java b/group06/949319266/homework003/src/FileDownloaderTest.java new file mode 100644 index 0000000000..7e98354f3b --- /dev/null +++ b/group06/949319266/homework003/src/FileDownloaderTest.java @@ -0,0 +1,107 @@ +import static org.junit.Assert.*; + +import org.junit.Test; + +import org.junit.After; + +import org.junit.Before; + +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; + +import com.coderising.download.api.DownloadListener; + +import com.coderising.download.impl.ConnectionManagerImpl; + + + +public class FileDownloaderTest { + + + + boolean downloadFinished = false; + + + + @Before + + public void setUp() throws Exception { + + } + + + + @After + + public void tearDown() throws Exception { + + } + + + + @Test + + public void testDownload() { + + + + String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489721424&di=1fda6467501ab1d5e5bff43e801d14ee&imgtype=jpg&er=1&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201507%2F30%2F20150730163204_A24MX.thumb.700_0.jpeg"; + + //String url = "http://apache.fayea.com/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz"; + + + + FileDownloader downloader = new FileDownloader(url); + + + + ConnectionManager cm = new ConnectionManagerImpl(); + + downloader.setConnectionManager(cm); + + + + downloader.setListener(new DownloadListener() { + + @Override + + public void notifyFinished() { + + downloadFinished = true; + + } + + }); + + + + downloader.execute(); + + + + // 等待多线程下载程序执行完毕 + + while (!downloadFinished) { + + try { + + System.out.println("还没有下载完成,休眠五秒"); + + //休眠5秒 + + Thread.sleep(5000); + + } catch (InterruptedException e) { + + e.printStackTrace(); + + } + + } + + System.out.println("下载完成!"); + + } + +} diff --git a/group06/949319266/homework003/src/com/coderising/download/api/Connection.java b/group06/949319266/homework003/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..17ca1ce521 --- /dev/null +++ b/group06/949319266/homework003/src/com/coderising/download/api/Connection.java @@ -0,0 +1,63 @@ +package com.coderising.download.api; + +import java.io.IOException; + + + +public interface Connection { + + /** + + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + + * + + * @param startPos 开始位置, 从0开始 + + * @param endPos 结束位置 + + * @return 读取的字节数组 + + */ + + byte[] read(int startPos, int endPos) throws IOException; + + + + /** + + * 得到数据内容的长度 + + * + + * @return 数据内容长度 + + */ + + int getContentLength(); + + + + /** + + * 关闭连接 + + */ + + void close(); + + + + /** + + * 获取下载文件的文件名 + + * + + * @return 文件名 + + */ + + String getFileName(); + +} \ No newline at end of file diff --git a/group06/949319266/homework003/src/com/coderising/download/api/ConnectionException.java b/group06/949319266/homework003/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8639cf4fa1 --- /dev/null +++ b/group06/949319266/homework003/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,19 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + + public ConnectionException(Exception e) { + + super(e); + + } + + + + public ConnectionException(String msg) { + + super(msg); + + } + +} \ No newline at end of file diff --git a/group06/949319266/homework003/src/com/coderising/download/api/ConnectionManager.java b/group06/949319266/homework003/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..5f8f4ea1fd --- /dev/null +++ b/group06/949319266/homework003/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,14 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + + */ + Connection open(String url, int startPos, int endPos) throws ConnectionException; + /** + * 获取文件长度 + */ + int getContentLength(String url) throws ConnectionException; + +} diff --git a/group06/949319266/homework003/src/com/coderising/download/api/DownloadListener.java b/group06/949319266/homework003/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..6aa0e8237f --- /dev/null +++ b/group06/949319266/homework003/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,7 @@ +package com.coderising.download.api; + +public interface DownloadListener { + + void notifyFinished(); + +} diff --git a/group06/949319266/homework003/src/com/coderising/download/impl/ConnectionImpl.java b/group06/949319266/homework003/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..50ed931c8b --- /dev/null +++ b/group06/949319266/homework003/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,186 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +import java.io.ByteArrayOutputStream; + +import java.io.IOException; + +import java.io.InputStream; + +import java.net.HttpURLConnection; + + + + +public class ConnectionImpl implements Connection { + + + + private static final int BUFFER_SIZE = 4096; + + private HttpURLConnection httpConn; + + private String fileUrl; + + private InputStream inputStream; + + + + public ConnectionImpl(HttpURLConnection httpConn, String fileUrl) { + + this.httpConn = httpConn; + + this.fileUrl = fileUrl; + + } + + + + @Override + + public byte[] read(int startPos, int endPos) throws IOException { + + if (endPos < startPos) { + + throw new IllegalArgumentException("argument endPos[" + endPos + "] less than startPos[" + startPos + "]"); + + } + + int bytesNeed2Read = endPos - startPos + 1; + + if (bytesNeed2Read > getContentLength()) { + + throw new IllegalArgumentException( + + "endPos[" + endPos + "] is bigger than content length[" + getContentLength() + "]"); + + } + + + + inputStream = httpConn.getInputStream(); + + + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + byte[] buffer = new byte[Math.min(bytesNeed2Read, BUFFER_SIZE)]; + + int read; + + + + long startTime = System.currentTimeMillis(); + + final long progressInterval = 2000; + + while ((read = inputStream.read(buffer)) != -1) { + + byteArrayOutputStream.write(buffer, 0, read); + + + + if (System.currentTimeMillis() - startTime > progressInterval) { + + startTime = System.currentTimeMillis(); + + System.out.println(String.format(Thread.currentThread().getName() + + + " [%.2f%%]", byteArrayOutputStream.size() * 100.0 / bytesNeed2Read) + + ); + + } + + } + + System.out.println(String.format(Thread.currentThread().getName() + " [%.2f%%]", 100.0)); + + System.out.println("bytes read: " + byteArrayOutputStream.size()); + + + + return byteArrayOutputStream.toByteArray(); + + } + + + + @Override + + public int getContentLength() { + + if (httpConn != null) { + + return httpConn.getContentLength(); + + } + + return 0; + + } + + + + @Override + + public void close() { + + if (inputStream != null) { + + try { + + inputStream.close(); + + } catch (IOException e) { + + e.printStackTrace(); + + } + + } + + if (httpConn != null) { + + httpConn.disconnect(); + + } + + } + + + + @Override + + public String getFileName() { + + String disposition = httpConn.getHeaderField("Content-Disposition"); + + if (disposition != null) { + + // extracts file name from header field + + int index = disposition.indexOf("filename="); + + if (index > 0) { + + return disposition.substring(index + 10, + + disposition.length() - 1); + + } + + } + + // extracts file name from URL + + return fileUrl.substring(fileUrl.lastIndexOf("/") + 1, + + fileUrl.length()); + + } + +} \ No newline at end of file diff --git a/group06/949319266/homework003/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group06/949319266/homework003/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..47f348cd8c --- /dev/null +++ b/group06/949319266/homework003/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,117 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import java.net.HttpURLConnection; + +import java.net.URL; + +import com.coderising.download.api.Connection; + +import com.coderising.download.api.ConnectionException; + +import com.coderising.download.api.ConnectionManager; + + + +public class ConnectionManagerImpl implements ConnectionManager { + + + + @Override + + public Connection open(String fileURL, int startPos, int endPos) throws ConnectionException { + + try { + + System.out.println("try to open file url: " + fileURL); + + + + URL url = new URL(fileURL); + + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + + + + // 设定读取range + + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + System.out.println("Range: bytes=" + startPos + "-" + endPos); + + + + int responseCode = httpConn.getResponseCode(); + + + + System.out.println("server replied HTTP code: " + responseCode); + + if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) { + + System.out.println("return new ConnectionImpl"); + + return new ConnectionImpl(httpConn, fileURL); + + } else { + + throw new ConnectionException("server replied HTTP code: " + responseCode); + + } + + } catch (IOException e) { + + throw new ConnectionException(e); + + } + + } + + + + @Override + + public int getContentLength(String fileURL) throws ConnectionException { + + try { + + System.out.println("try to open file url: " + fileURL); + + + + URL url = new URL(fileURL); + + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + + int responseCode = httpConn.getResponseCode(); + + + + System.out.println("server replied HTTP code: " + responseCode); + + if (responseCode == HttpURLConnection.HTTP_OK) { + + System.out.println("return contentLength: " + httpConn.getContentLength()); + + int contentLength = httpConn.getContentLength(); + + httpConn.disconnect(); + + return contentLength; + + } else { + + throw new ConnectionException("server replied HTTP code: " + responseCode); + + } + + } catch (IOException e) { + + throw new ConnectionException(e); + + } + + } + +} \ No newline at end of file diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/Configuration.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/Configuration.java new file mode 100644 index 0000000000..6ae4adeed6 --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/Configuration.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +public class Configuration { + + Map actions = new HashMap<>(); + + public Configuration(String fileName) { + + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is){ + + SAXBuilder builder = new SAXBuilder(); + + try { + + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for(Element actionElement : root.getChildren("action")){ + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for(Element resultElement : actionElement.getChildren("result")){ + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + + } + + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig{ + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } + +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/ConfigurationException.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..97e286827f --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.coderising.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/ConfigurationTest.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..734649f37a --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,50 @@ +package com.coderising.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ConfigurationTest { + + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView(){ + String jsp = cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } + +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/LoginAction.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/ReflectionUtil.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..0bd53fea93 --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/ReflectionUtil.java @@ -0,0 +1,123 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for(String name : params.keySet() ){ + + String methodName = "set" + name; + + for(Method m: methods){ + + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + private static List getMethods(Class clz, String startWithName){ + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith(startWithName)){ + + methods.add(m); + + } + + } + + return methods; + } + + public static Map getParamterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for(Method m : methods){ + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + + return params; + } + + ////////////////////////Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("get")){ + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("set")){ + + methods.add(m); + + } + + } + + return methods; + + } + + + + +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/ReflectionUtilTest.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..cbe732d83f --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception{ + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + ReflectionUtil.setParameters(o,params); + + + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + @Test + public void testGetGetterMethod() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParamters() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + + + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/Struts.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..3e9678cbd8 --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,171 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + + + +public class Struts { + + + + /** + + * 0. 读取配置文件struts.xml + + * + + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + + * ("name"="test" , "password"="1234") , + + * 那就应该调用 setName和setPassword方法 + + * + + * 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + + * + + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + + * 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + + * 放到View对象的parameters + + * + + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + + * 放到View对象的jsp字段中。 + + */ + + public static View runAction(String actionName, Map parameters) { + + Map actionMap = StrutsParser.doParse(); + + StrutsAction action = actionMap.get(actionName); + + + + if (action == null) { + + System.out.println("couldn't get action: " + actionName + ", return"); + + return null; + + } + + + + try { + + // 通过反射, 创建实例对象 + + Class actionClass = Class.forName(action.getActionClassName()); + + Object actionObj = actionClass.newInstance(); + + + + // 调用 parameters 中的 set 方法 + + for (Map.Entry parameterEntry : parameters.entrySet()) { + + Method[] methods = actionClass.getMethods(); + + for (Method method : methods) { + + if (method.getName().equalsIgnoreCase("set" + parameterEntry.getKey())) { + + method.invoke(actionObj, parameterEntry.getValue()); + + } + + } + + } + + + + // 调用 execute 方法 + + Method executeMethod = actionClass.getMethod("execute"); + + Object executeResult = executeMethod.invoke(actionObj); + + + + // 根据 execute 方法的结果, 获取 xml 配置的 jsp 页面 + + String jsp = action.getAttributes().get(Objects.toString(executeResult)); + + + + // 调用 get 方法 + + Map actionFieldMap = new HashMap<>(); + + Field[] actionFields = actionClass.getDeclaredFields(); + + for (Field actionFiled : actionFields) { + + Method[] methods = actionClass.getMethods(); + + for (Method method : methods) { + + if (method.getName().equalsIgnoreCase("get" + actionFiled.getName())) { + + method.invoke(actionObj); + + actionFieldMap.put(actionFiled.getName(), Objects.toString(method.invoke(actionObj))); + + } + + } + + } + + + + View view = new View(); + + view.setParameters(actionFieldMap); + + view.setJsp(jsp); + + return view; + + } catch (ClassNotFoundException e) { + + e.printStackTrace(); + + } catch (InstantiationException e) { + + e.printStackTrace(); + + } catch (IllegalAccessException e) { + + e.printStackTrace(); + + } catch (InvocationTargetException e) { + + e.printStackTrace(); + + } catch (NoSuchMethodException e) { + + e.printStackTrace(); + + } + + return null; + + } + +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/StrutsTest.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group06/949319266/homework03/src/src/com/coderising/litestruts/View.java b/group06/949319266/homework03/src/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group06/949319266/homework03/src/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git "a/group06/949319266/homework03/\345\233\233\346\254\241\346\226\207\347\253\240\345\234\260\345\235\200\346\261\207\346\200\273" "b/group06/949319266/homework03/\345\233\233\346\254\241\346\226\207\347\253\240\345\234\260\345\235\200\346\261\207\346\200\273" new file mode 100644 index 0000000000..a13cc729cf --- /dev/null +++ "b/group06/949319266/homework03/\345\233\233\346\254\241\346\226\207\347\253\240\345\234\260\345\235\200\346\261\207\346\200\273" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/articlelist_3255506984_0_1.html \ No newline at end of file diff --git a/group06/949319266/homework04/src/ClassFileLoader/ClassFileLoader.java b/group06/949319266/homework04/src/ClassFileLoader/ClassFileLoader.java new file mode 100644 index 0000000000..da0d90a7e5 --- /dev/null +++ b/group06/949319266/homework04/src/ClassFileLoader/ClassFileLoader.java @@ -0,0 +1,88 @@ +package ClassFileLoader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + private static final int BUFFER_SIZE = 1024; + public byte[] readBinaryCode(String className) { + byte[] result = null; + for(String path : clzPaths){ + File file = new File(getPath(path, className)); + if(!file.exists()){ + continue; + } + result = readFile(file); + } + return result; + } + /** + * ļݴֽз + */ + private byte[] readFile(File file){ + FileInputStream fileInputStream; + byte[] buffer = new byte[BUFFER_SIZE]; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + try { + fileInputStream = new FileInputStream(file); + while(byteArrayOutputStream.size() < file.length()){ + int len = fileInputStream.read(buffer); + if(len < 0){ + break; + } + byteArrayOutputStream.write(buffer, 0, len); + } + } catch (Exception e) { + e.printStackTrace(); + } + if(byteArrayOutputStream.size() > file.length()){ + byte[] result = byteArrayOutputStream.toByteArray(); + return Arrays.copyOf(result, (int)file.length()); + } + return byteArrayOutputStream.toByteArray(); + } + /** + * ȡʵ·· + */ + private String getPath(String path ,String className){ + System.out.println(className); + String [] ways = className.split("\\."); + for (String string : ways) { + System.out.println(string); + } + StringBuilder builder = new StringBuilder(); + builder.append(path); + for (String string : ways) { + builder.append("\\"); + builder.append(string); + } + builder.append(".class"); + System.out.println(builder.toString()); + return builder.toString(); + } + private byte[] loadClassFile(String clzFileName) { + return null; + } + public void addClassPath(String path) { + clzPaths.add(path); +} + public String getClassPath_V1(){ + return null; + } + + public String getClassPath(){ + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < clzPaths.size(); i++){ + builder.append(clzPaths.get(i)); + if(i < clzPaths.size() - 1){ + builder.append(";"); + } + } + return builder.toString(); + } +} diff --git a/group06/949319266/homework04/src/ClassFileLoader/ClassFileloaderTest.java b/group06/949319266/homework04/src/ClassFileLoader/ClassFileloaderTest.java new file mode 100644 index 0000000000..a4fb150fa1 --- /dev/null +++ b/group06/949319266/homework04/src/ClassFileLoader/ClassFileloaderTest.java @@ -0,0 +1,5 @@ +package ClassFileLoader; + +public class ClassFileloaderTest { + +} diff --git a/group06/949319266/homework04/src/ClassFileLoader/EmployeeV1.java b/group06/949319266/homework04/src/ClassFileLoader/EmployeeV1.java new file mode 100644 index 0000000000..e3104ecd79 --- /dev/null +++ b/group06/949319266/homework04/src/ClassFileLoader/EmployeeV1.java @@ -0,0 +1,23 @@ +package ClassFileLoader; + +public class EmployeeV1 { + private String name; + private int age; + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + } +} \ No newline at end of file diff --git a/group06/949319266/homework04/src/ClassFileLoader/LRUPageFrame.java b/group06/949319266/homework04/src/ClassFileLoader/LRUPageFrame.java new file mode 100644 index 0000000000..97ea2e966f --- /dev/null +++ b/group06/949319266/homework04/src/ClassFileLoader/LRUPageFrame.java @@ -0,0 +1,110 @@ +package ClassFileLoader; + +public class LRUPageFrame { + + private static class Node { + Node prev; + Node next; + int pageNum; + Node() { + } + Node(int pageNum){ + this.pageNum = pageNum; + } + } + private int capacity; + private Node first;// ͷ + private Node last;// β + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + /** + * ȡж + */ + public void access(int pageNum) { + if(first == null){ //һ + first = new Node(pageNum); + first.next = first.prev = null; + last = first; + }else if(size() < capacity && first != null){ //ҳδʱ + Node node = new Node(pageNum); + if(last.prev == null){ + last.prev = node; + node.next = last; + }else{ + node.next = first; + first.prev = node; + node.prev = null; + } + first = node; + }else if(size() >= capacity){ //ҳ + Node node = getNode(pageNum); + LRU(node, pageNum); + } + } + /** + * lru㷨 + */ + private void LRU(Node node, int pageNum){ + if(node != null){ + if(last.pageNum == node.pageNum){ //last + last = node.prev; + last.next = null; + node.next = first; + first.prev = node; + node.prev = null; + first = node; + }else if(last.pageNum != node.pageNum && first.pageNum != node.pageNum){ + //firstlastм䷶Χ + node.prev.next = node.next; + node.next.prev = node.prev; + node.prev = null; + node.next = first; + first = node; + } + }else{ + //» + last = last.prev; + last.next = null; + node = new Node(pageNum); + node.next = first; + first.prev = node; + first = node; + } + } + /** + * ڻлȡڵ + */ + private Node getNode(int pageNum){ + Node node = first; + while(node != null){ + if(node.pageNum == pageNum){ + return node; + } + node = node.next; + } + return null; + } + public int size(){ + int num = 0; + Node node = first; + while(node != null){ + num++; + node = node.next; + } + return num; + } + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group06/949319266/homework04/src/Test/ClassFileloaderTest.java b/group06/949319266/homework04/src/Test/ClassFileloaderTest.java new file mode 100644 index 0000000000..12e49611ff --- /dev/null +++ b/group06/949319266/homework04/src/Test/ClassFileloaderTest.java @@ -0,0 +1,65 @@ +package Test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import ClassFileLoader.ClassFileLoader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + + static String path1 = "C:\\Users\\yanght\\Documents\\mygit\\coding2017-1\\group06\\1454385822\\bin"; + static String path2 = "D:\temp"; + @Before + public void setUp() throws Exception { + } + @After + public void tearDown() throws Exception { + } + @Test + public void testClassPath(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1+";"+path2,clzPath); + } + @Test + public void testClassFileLength() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coding.basic.homework_04.jvm.test.EmployeeV1"; +// String className = "EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + // ע⣺ֽܺJVM汾йϵ Կõൽж + Assert.assertEquals(1084, byteCodes.length); + } + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coding.basic.homework_04.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + String acctualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe", acctualValue); + } + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i actions = new HashMap<>(); + + public Configuration(String fileName) { + + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is){ + + SAXBuilder builder = new SAXBuilder(); + + try { + + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for(Element actionElement : root.getChildren("action")){ + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for(Element resultElement : actionElement.getChildren("result")){ + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + + } + + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig{ + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } + +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/ConfigurationException.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..97e286827f --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.coderising.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/ConfigurationTest.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..734649f37a --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,50 @@ +package com.coderising.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ConfigurationTest { + + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView(){ + String jsp = cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } + +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/LoginAction.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/ReflectionUtil.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..0bd53fea93 --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ReflectionUtil.java @@ -0,0 +1,123 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for(String name : params.keySet() ){ + + String methodName = "set" + name; + + for(Method m: methods){ + + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + private static List getMethods(Class clz, String startWithName){ + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith(startWithName)){ + + methods.add(m); + + } + + } + + return methods; + } + + public static Map getParamterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for(Method m : methods){ + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + + return params; + } + + ////////////////////////Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("get")){ + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("set")){ + + methods.add(m); + + } + + } + + return methods; + + } + + + + +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/ReflectionUtilTest.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..cbe732d83f --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception{ + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + ReflectionUtil.setParameters(o,params); + + + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + @Test + public void testGetGetterMethod() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParamters() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + + + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/Struts.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..3e9678cbd8 --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,171 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + + + +public class Struts { + + + + /** + + * 0. 读取配置文件struts.xml + + * + + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + + * ("name"="test" , "password"="1234") , + + * 那就应该调用 setName和setPassword方法 + + * + + * 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + + * + + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + + * 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + + * 放到View对象的parameters + + * + + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + + * 放到View对象的jsp字段中。 + + */ + + public static View runAction(String actionName, Map parameters) { + + Map actionMap = StrutsParser.doParse(); + + StrutsAction action = actionMap.get(actionName); + + + + if (action == null) { + + System.out.println("couldn't get action: " + actionName + ", return"); + + return null; + + } + + + + try { + + // 通过反射, 创建实例对象 + + Class actionClass = Class.forName(action.getActionClassName()); + + Object actionObj = actionClass.newInstance(); + + + + // 调用 parameters 中的 set 方法 + + for (Map.Entry parameterEntry : parameters.entrySet()) { + + Method[] methods = actionClass.getMethods(); + + for (Method method : methods) { + + if (method.getName().equalsIgnoreCase("set" + parameterEntry.getKey())) { + + method.invoke(actionObj, parameterEntry.getValue()); + + } + + } + + } + + + + // 调用 execute 方法 + + Method executeMethod = actionClass.getMethod("execute"); + + Object executeResult = executeMethod.invoke(actionObj); + + + + // 根据 execute 方法的结果, 获取 xml 配置的 jsp 页面 + + String jsp = action.getAttributes().get(Objects.toString(executeResult)); + + + + // 调用 get 方法 + + Map actionFieldMap = new HashMap<>(); + + Field[] actionFields = actionClass.getDeclaredFields(); + + for (Field actionFiled : actionFields) { + + Method[] methods = actionClass.getMethods(); + + for (Method method : methods) { + + if (method.getName().equalsIgnoreCase("get" + actionFiled.getName())) { + + method.invoke(actionObj); + + actionFieldMap.put(actionFiled.getName(), Objects.toString(method.invoke(actionObj))); + + } + + } + + } + + + + View view = new View(); + + view.setParameters(actionFieldMap); + + view.setJsp(jsp); + + return view; + + } catch (ClassNotFoundException e) { + + e.printStackTrace(); + + } catch (InstantiationException e) { + + e.printStackTrace(); + + } catch (IllegalAccessException e) { + + e.printStackTrace(); + + } catch (InvocationTargetException e) { + + e.printStackTrace(); + + } catch (NoSuchMethodException e) { + + e.printStackTrace(); + + } + + return null; + + } + +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/StrutsTest.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group06/949319266/lite-struts2/src/com/coderising/litestruts/View.java b/group06/949319266/lite-struts2/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group06/949319266/lite-struts2/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group08/1144989424/secondPractice/readme.md b/group08/1144989424/secondPractice/readme.md new file mode 100644 index 0000000000..f5644a6ce2 --- /dev/null +++ b/group08/1144989424/secondPractice/readme.md @@ -0,0 +1,3 @@ +### 第二次作业 +1. 完成数组练习题 +2. 读取xml文件,在利用反射生成类对象,反射获取get和set方法 \ No newline at end of file diff --git a/group08/1144989424/thirdPractice/readme.md b/group08/1144989424/thirdPractice/readme.md new file mode 100644 index 0000000000..d72cafdb2e --- /dev/null +++ b/group08/1144989424/thirdPractice/readme.md @@ -0,0 +1,9 @@ +### 第三次作业 +1. 完成链表练习题 +2. 实现多线程下载文件 + +博客更新 + +[数据库查询连接(JOIN)用法](http://blog.csdn.net/qq1332479771/article/details/62104624) + +[log4j日志级别](http://blog.csdn.net/qq1332479771/article/details/61927227) diff --git a/group08/1144989424/thirdPractice/src/download/DownloadThread.java b/group08/1144989424/thirdPractice/src/download/DownloadThread.java new file mode 100644 index 0000000000..5b3de789c6 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/DownloadThread.java @@ -0,0 +1,26 @@ +package download; + +import java.io.FileOutputStream; + +import download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + byte [] buff; + FileOutputStream file; + + public DownloadThread( Connection conn, int startPos, int endPos, byte[] buff ,FileOutputStream file){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.file = file; + this.buff = buff; + } + public void run(){ + + } +} diff --git a/group08/1144989424/thirdPractice/src/download/FileDownloader.java b/group08/1144989424/thirdPractice/src/download/FileDownloader.java new file mode 100644 index 0000000000..670ce43188 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/FileDownloader.java @@ -0,0 +1,73 @@ +package download; + +import download.api.Connection; +import download.api.ConnectionException; +import download.api.ConnectionManager; +import download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String url) { + this.url = url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group08/1144989424/thirdPractice/src/download/FileDownloaderTest.java b/group08/1144989424/thirdPractice/src/download/FileDownloaderTest.java new file mode 100644 index 0000000000..c978cc43d2 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import download.api.ConnectionManager; +import download.api.DownloadListener; +import download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group08/1144989424/thirdPractice/src/download/api/Connection.java b/group08/1144989424/thirdPractice/src/download/api/Connection.java new file mode 100644 index 0000000000..1a467a8086 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/api/Connection.java @@ -0,0 +1,23 @@ +package download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group08/1144989424/thirdPractice/src/download/api/ConnectionException.java b/group08/1144989424/thirdPractice/src/download/api/ConnectionException.java new file mode 100644 index 0000000000..13cf1a4729 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group08/1144989424/thirdPractice/src/download/api/ConnectionManager.java b/group08/1144989424/thirdPractice/src/download/api/ConnectionManager.java new file mode 100644 index 0000000000..1519ebb787 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group08/1144989424/thirdPractice/src/download/api/DownloadListener.java b/group08/1144989424/thirdPractice/src/download/api/DownloadListener.java new file mode 100644 index 0000000000..4119e46144 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group08/1144989424/thirdPractice/src/download/impl/ConnectionImpl.java b/group08/1144989424/thirdPractice/src/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..24180cc013 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/impl/ConnectionImpl.java @@ -0,0 +1,39 @@ +package download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; + +import download.api.Connection; + +public class ConnectionImpl implements Connection{ + + URLConnection urlConn; + + public URLConnection getUrlConn() { + return urlConn; + } + + public void setUrlConn(URLConnection urlConn) { + this.urlConn = urlConn; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + InputStream input = urlConn.getInputStream(); + byte[] bytes = new byte[endPos-startPos]; + input.read(bytes, startPos, endPos); + return bytes; + } + + @Override + public int getContentLength() { + return urlConn.getContentLength(); + } + + @Override + public void close() { + urlConn = null; + } + +} diff --git a/group08/1144989424/thirdPractice/src/download/impl/ConnectionManagerImpl.java b/group08/1144989424/thirdPractice/src/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..6b28bf9b29 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,41 @@ +package download.impl; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import download.api.Connection; +import download.api.ConnectionException; +import download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + URL uu = null; + ConnectionImpl conn = null; + try { + uu = new URL(url); + URLConnection urlConn = uu.openConnection(); + conn.setUrlConn(urlConn); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + + return conn; + } + + public static void main(String [] args){ + ConnectionManagerImpl cmi = new ConnectionManagerImpl(); + try { + cmi.open("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"); + } catch (ConnectionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/group08/1144989424/thirdPractice/src/download/impl/DownloadListenerImpl.java b/group08/1144989424/thirdPractice/src/download/impl/DownloadListenerImpl.java new file mode 100644 index 0000000000..a18e1262b6 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/download/impl/DownloadListenerImpl.java @@ -0,0 +1,11 @@ +package download.impl; + +import download.api.DownloadListener; + +public class DownloadListenerImpl implements DownloadListener{ + + @Override + public void notifyFinished(){ + + } +} diff --git a/group08/1144989424/thirdPractice/src/linkedlist/MyIterator.java b/group08/1144989424/thirdPractice/src/linkedlist/MyIterator.java new file mode 100644 index 0000000000..fb71445850 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/linkedlist/MyIterator.java @@ -0,0 +1,7 @@ +package linkedlist; + +public interface MyIterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group08/1144989424/thirdPractice/src/linkedlist/MyLinkedList.java b/group08/1144989424/thirdPractice/src/linkedlist/MyLinkedList.java new file mode 100644 index 0000000000..31d45f02c5 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/linkedlist/MyLinkedList.java @@ -0,0 +1,328 @@ +package linkedlist; + +import java.util.Comparator; + +/** + * 实现LinkedList基本功能 + * @author Wayss + * 2017-02-23 + */ + +public class MyLinkedList implements MyList { + + private Node head; + private int size = 0; + + public void add(Object o){ + Node n = new Node(o); + head.next = n; + size++; + } + public void add(int index , Object o){ + //1.index校验 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2. 查找index位置的前一个节点 + //tempNode为当前链表的第一个节点 + Node tempNode = head.next; + for(int i = 0; i < index - 1 ; i++){ + tempNode = tempNode.next; + } + Node behindNode = tempNode.next; + Node insertNode = new Node(o); + tempNode.next = insertNode; + insertNode.next = behindNode; + size++; + } + public Object get(int index){ + //1.index校验 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2. 查找当前节点 + Node tempNode = head.next; + for(int i = 0; i < index; i++){ + tempNode = tempNode.next; + } + return tempNode.data; + } + public Object remove(int index){ + //1.index校验 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2. 查找当前节点的上一个节点 + Node tempNode = head.next; + for(int i = 0; i < index - 1; i++){ + tempNode = tempNode.next; + } + Node deleteNode = tempNode.next; + Node behideNode = tempNode.next.next; + tempNode.next = behideNode; + size--; + return deleteNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node insertNode = new Node(o); + insertNode.next = head.next; + head.next = insertNode; + size++; + } + public void addLast(Object o){ + Node insertNode = new Node(o); + Node tempNode = head.next; + for(int i = 0; i < size; i++){ + tempNode = tempNode.next; + } + tempNode.next = insertNode; + size++; + } + public Object removeFirst(){ + Node firstNode = head.next; + head = firstNode.next; + size--; + return firstNode; + } + public Object removeLast(){ + Node tempNode = head.next; + //1.移除需要找到最后一个点的前一个点 + for(int i = 0; i < size - 1; i++){ + tempNode = tempNode.next; + } + Node deleteNode = tempNode.next; + tempNode.next = null; + size--; + return deleteNode; + } + + public MyIterator iterator(){ + return new MyLinkedListIterator(this); + } + + private class MyLinkedListIterator implements MyIterator{ + private MyLinkedList list = null; + private int index = 0; + + private MyLinkedListIterator(MyLinkedList list){ + this.list = list; + } + + @Override + public boolean hasNext(){ + if(index < size){ + return true; + } + return false; + } + + @Override + public Object next(){ + return list.get(index++); + } + } + + private static class Node{ + Object data; + Node next; + public Node(Object data){ + this.data = data; + } + } + public void set(int i, Object obj){ + remove(i); + add(i,obj); + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + //单链表的实现方法,遍历了一遍 + //如果是双向链表的话,逆置就简单多了 + int half = size/2; + for(int i = 0; i < half; i ++){ + Object o1 = get(i); + set(i,o1); + Object o2 = get(size - 1 - i); + set(size - 1 - i, o2); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + int half = size/2; + Node tempNode = head.next; + for(int i = 0; i < half; i++){ + tempNode = tempNode.next; + } + //通过移动head指针来实现移除前半部分 + head.next = tempNode; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(i < 0 || i > size || i < length || length < 0 || length > size){ + return; + } + + Node tempNode = head.next; + for(int j = 0; j < i; j++){ + tempNode = tempNode.next; + } + Node n1 = tempNode; + + for(int j = i; j < length; j++){ + tempNode = tempNode.next; + } + Node n2 = tempNode; + + //移除n1到n2中间的元素 + n1.next = n2; + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(MyLinkedList list){ + if(list == null){ + return null; + } + + Node tempNode = head.next; + Node result = head; + int[] res = new int[list.size()]; + + for(int j = 0,i = 0; j < size; j++){ + if(j == (int)list.removeFirst()){ + result.next = tempNode; + res[i++] = (int) tempNode.data; + }else{ + tempNode = tempNode.next; + } + } + +// return result; + return res; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * @param list + */ + + public void subtract(MyLinkedList list){ + + //方法一 + //1.对list递增排序 + //2.遍历当前链表,同时和list做比较,向后移动list指针,注意是递增的 + + //方法二 + //1.两层遍历嵌套 + for(int i = 0; i < list.size(); i++){ + for(int j = 0; j < size; j++){ + if(list.get(i).equals(this.get(j))){ + this.remove(j); + } + } + } + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + //1.遍历链表,比较后一个和当前的大小, + //2.相等则,删除后一个,同时再比较原先删除的节点的后一个(可能需要while) + for(int i = 0; i < size; i++){ + while(this.get(i).equals(this.get(i+1))){ + this.remove(i+1); + //由于remove会把size的大小减一,所以,不会出现数组越界 + i++; + //i++的目的是为了判断连续多个值相等的情况 + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + //1.找到第一个值大于min的节点,的前一个节点 + //2.找到第一个值小于max的节点 + //3.用第一步找出的节点指向第二步找出的节点的next + Node minNode = head.next; + Node maxNode = head.next; + + int first = 0; + int last = 0; + //循环停止的条件是,找到了第一个节点不小于min的点了。 + //所以,minNode也就没有再往后移动了。即,在不小于min的第一个点的前一个 + while((int)this.get(first++) < min){ + minNode = minNode.next; + } + + while((int)this.get(last++) < max){ + maxNode = maxNode.next; + } + //maxNode往后再移动一个位置,表示的是第一个不小于max的点 + maxNode = maxNode.next; + + minNode.next = maxNode; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public MyLinkedList intersection( MyLinkedList list){ + //1.假设当前链表节点个数是m,list中的节点个数是n + //2.现在需要遍历m+n次,依次把值给链表C传递,同时,给C链表add的时候,注意比较大小 + MyLinkedList result = new MyLinkedList(); + int i = 0; + int j = 0; + while(i < size){ + while(j < list.size()){ + if((int)this.get(i) < (int)list.get(j)){ + result.add(this.get(i)); + i++; + }else if((int)this.get(i) == (int)list.get(j)){ + result.add(this.get(i)); + i++; + j++; + }else{ + result.add(list.get(j)); + j++; + } + } + } + + return result; + } + +} diff --git a/group08/1144989424/thirdPractice/src/linkedlist/MyList.java b/group08/1144989424/thirdPractice/src/linkedlist/MyList.java new file mode 100644 index 0000000000..f5cd0f5731 --- /dev/null +++ b/group08/1144989424/thirdPractice/src/linkedlist/MyList.java @@ -0,0 +1,9 @@ +package linkedlist; + +public interface MyList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/1425809544/03-05/com/array/ArrayUtil.java b/group08/1425809544/03-05/com/array/ArrayUtil.java new file mode 100644 index 0000000000..f370e38dd4 --- /dev/null +++ b/group08/1425809544/03-05/com/array/ArrayUtil.java @@ -0,0 +1,257 @@ +package com.util_1; + +import java.util.*; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] oldArr = origin; + int newLength = oldArr.length; + int[] newArr = new int[newLength]; + for (int i = 0; i < newLength; i++) { + newArr[newLength - i - 1] = oldArr[i]; + } + for (int s : newArr) { + System.out.println(s); + } + + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int[] oldArr = oldArray; + int oldLength = oldArr.length; + int[] newArr = new int[oldLength]; + int index = 0; + int zeroCount = 0; + for (int i = 0; i < oldLength; i++) { + if (oldArr[i] == 0) { + zeroCount++; + } else { + newArr[index++] = oldArr[i]; + } + } + int[] newArrs = Arrays.copyOf(newArr, index); + return newArrs; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int newLength = array1.length + array2.length; + int[] newArr = new int[newLength]; + int index = 0; + for (int i = 0; i < array1.length; i++) { + newArr[index++] = array1[i]; + } + for (int i = 0; i < array2.length; i++) { + newArr[index++] = array2[i]; + } + //冒泡排序 + for (int i = 0; i < newArr.length; i++) { + for (int j = i + 1; j < newArr.length; j++) { + if (newArr[i] > newArr[j]) { + int temp = newArr[i]; + newArr[i] = newArr[j]; + newArr[j] = temp; + } + } + } + //数组去重 + boolean[] b = new boolean[newArr.length]; + int counts = newArr.length; + for (int i = 0; i < newArr.length; i++) { + for (int j = i + 1; j < newArr.length; j++) { + if (newArr[i] == newArr[j] && b[i] == false) { + b[j] = true; + counts--; + } + } + } + int[] result = new int[counts]; + int j = 0; + for (int i = 0; i < newArr.length; i++) { + if (b[i] == false) { + result[j] = newArr[i]; + j++; + } + } + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int oldLength = oldArray.length; + int newLength = oldLength + size; + int[] result = Arrays.copyOf(oldArray, newLength); + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int a = 1, b = 1, c = 2; + int[] arr = new int[max + 1]; + int i = 2; + if (max == 1) { + return Arrays.copyOf(arr, 0); + } else if (max <= 0) { + throw new IllegalArgumentException("不能输入<=0的参数:" + max); + } else { + arr[0] = 1; + arr[1] = 1; + do { + c = a + b; + a = b; + b = c; + arr[i++] = c; + } while (c < max); + } + + if (arr[i - 1] >= max) { + return Arrays.copyOf(arr, i - 1); + } + return Arrays.copyOf(arr, i); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + class IsPrime { + // 判断某整数是否为素数 + public boolean isPrimes(int n) { + if (n < 2) { + return false; + } + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + return false; + } + } + return true; + + } + } + List list = new ArrayList(); + IsPrime isPrime = new IsPrime(); + for (int i = 2; i < max; i++) { + if (isPrime.isPrimes(i)) { + list.add(i); + } + } + + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = (int) list.get(i); + } + + return arr; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + //保存每组的分解因子 + List list = new ArrayList(); + List pm = new ArrayList(); + int sum = 0; + //除数 + for (int i = 2; i < max; i++) { + //被除数 + sum=0; + for (int j = 1; j < i / 2 + 1; j++) { + if (i % j == 0) { + list.add(j); + sum += j; + } + } + + if (sum == i) { + pm.add(i); + } + + list.clear(); + } + + int[] pmaArr = new int[pm.size()]; + for (int i = 0; i < pm.size(); i++) { + pmaArr[i] = (int) pm.get(i); + } + return pmaArr; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param + * @return + */ + + public String join(int[] array, String seperator) { + + String s = new String(); + for (int i = 0; i < array.length; i++) { + if (i < array.length - 1) { + s += array[i] + seperator; + } else { + s += array[i]; + } + } + return s; + } + + +} diff --git a/group08/1425809544/03-05/com/array/ArrayUtilTest.java b/group08/1425809544/03-05/com/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ad348045f9 --- /dev/null +++ b/group08/1425809544/03-05/com/array/ArrayUtilTest.java @@ -0,0 +1,99 @@ +package com.util_1; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by 14258 on 2017/2/28. + */ +public class ArrayUtilTest { + ArrayUtil arrayUtil = new ArrayUtil(); + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testReverseArray() throws Exception { + + int[] testArr = {7, 9, 30, 3}; + + arrayUtil.reverseArray(testArr); + } + + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] newArr = arrayUtil.removeZero(oldArr); + for (int s : newArr) { + System.out.println(s); + } + + } + + @Test + public void testMerge() throws Exception { + int[] a1 = {3, 5, 7}; + int[] a2 = {4, 5, 6, 7}; + int[] newArr = arrayUtil.merge(a1, a2); + for (int s : newArr) { + System.out.println(s); + } + } + + @Test + public void testGrow() throws Exception { + int[] oldArray = {2, 3, 6}; + + int[] newArr = arrayUtil.grow(oldArray, 3); + + for (int s : newArr) { + System.out.println(s); + } + } + + @Test + public void testFibonacci() throws Exception { + + int[] newArr = arrayUtil.fibonacci(16); + System.out.print("["); + for (int i : newArr) { + System.out.print(i+","); + } + System.out.print("]"); + } + + @Test + public void testGetPrimes() throws Exception { + int[] prime = arrayUtil.getPrimes(23); + + for (int i :prime){ + System.out.print(i+" "); + } + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] prime = arrayUtil.getPerfectNumbers(10000); + + for (int i :prime){ + System.out.print(i+" "); + } + } + + @Test + public void testJoin() throws Exception { + int[] array = {3, 8, 9}; + String s = arrayUtil.join(array, "-"); + System.out.println(s); + } +} \ No newline at end of file diff --git "a/group08/1425809544/03-05/\346\226\207\347\253\240\351\223\276\346\216\245-\350\256\241\347\256\227\346\234\272\345\255\230\345\202\250\345\231\250\347\273\223\346\236\204.txt" "b/group08/1425809544/03-05/\346\226\207\347\253\240\351\223\276\346\216\245-\350\256\241\347\256\227\346\234\272\345\255\230\345\202\250\345\231\250\347\273\223\346\236\204.txt" new file mode 100644 index 0000000000..ec8fa593de --- /dev/null +++ "b/group08/1425809544/03-05/\346\226\207\347\253\240\351\223\276\346\216\245-\350\256\241\347\256\227\346\234\272\345\255\230\345\202\250\345\231\250\347\273\223\346\236\204.txt" @@ -0,0 +1 @@ +[洢ṹ](http://note.youdao.com/noteshare?id=a2da59b294d277d0e828f4f566015014&sub=04848681007A4C4A8649A411729AEADC) \ No newline at end of file diff --git a/group08/1425809544/03-12/code/com/xyy/baselinked/Iterator.java b/group08/1425809544/03-12/code/com/xyy/baselinked/Iterator.java new file mode 100644 index 0000000000..72320aade3 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/baselinked/Iterator.java @@ -0,0 +1,12 @@ +package xyy.baselinked; + +/** + * Created by 14258 on 2017/3/14. + */ +public interface Iterator { + + public boolean hasNext(); + + public Object next(); + +} diff --git a/group08/1425809544/03-12/code/com/xyy/baselinked/LinkedList.java b/group08/1425809544/03-12/code/com/xyy/baselinked/LinkedList.java new file mode 100644 index 0000000000..096b73c311 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/baselinked/LinkedList.java @@ -0,0 +1,372 @@ +package xyy.baselinked; + +/** + * Created by 14258 on 2017/3/14. + */ +public class LinkedList implements List { + + private Node head; + private int size; + + + @Override + public void add(Object o) { + addLast(o); + } + + @Override + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (index == 0) { + addFirst(o); + return; + } + + if (index == size) { + addLast(o); + return; + } + + Node newNode = new Node(o); + Node node = head; + for (int i = 1; i < index; i++) { + node = node.next; + } + newNode.next = node.next; + node.next = newNode; + size++; + } + + @Override + public Object remove(int index) { + + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + if (index == 0) { + return removeFirst(); + } + Node node = head; + Node pre = head; + for (int i = 1; i < index; i++) { + if (node.next != null) { + pre = node; + node = node.next; + } + } + + Object obj = node.data; + if (head.next == null) { + head = null; + pre = null; + } else if (node.next == null) { + pre.next = null; + node = null; + } else { + pre.next = node.next; + node.next = null; + node = null; + } + size--; + return obj; + } + + @Override + public int size() { + return size; + } + + @Override + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + Object data = node.data; + return data; + } + + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o); + if (head == null) { + head = newNode; + } else { + if (head.next == null) { + head.next = newNode; + } else { + Node node = head; + for (int i = 1; i < size; i++) { + node = node.next; + } + node.next = newNode; + } + } + } + + public Object removeFirst() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + head = node.next; + node.next = null; + size--; + return node.data; + + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + for (int i = 1; i < size; i++) { + node = node.next; + } + Object data = node.next.data; + node.next = null; + size--; + return data; + } + + + private class LinkedListIterator implements Iterator { + private Node node = head; + + public boolean hasNext() { + return node != null; + } + + public Object next() { + Object data = node.data; + node = node.next; + return data; + } + + public void moveFirst() { + node = head; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + } + + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { +// Node node = head.next; +// Object[] arr = new Object[size]; +// int i = size - 1; +// while (i >= 0) { +// arr[i--] = node.data; +// node = node.next; +// } +// node = head.next; +// for (int j = 0; j < size; j++) { +// node.data = arr[j]; +// node = node.next; +// } + if (size <= 0) { + throw new IndexOutOfBoundsException("链表下表越界" + size); + } + Node node = head; + Node minNode = node; + int length = size; + for (int i = 0; i < length; i++) { + if (node.next != null) { + node = node.next; + addFirst(node.data); + } + } + minNode.next = null; + node = null; + size = length; + + } + + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + + + public void removeFirstHalf() { + if (size <= 0) { + throw new IndexOutOfBoundsException("链表下标越界" + size); + } + Node node = head; + Node pre = head; + int count = 0; + for (int i = 0; i < size / 2; i++) { + pre = node; + node = node.next; + count++; + } + + head = node; + pre.next = null; + pre = null; + size = size - count; + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + + public void remove(int i, int length) { + + if (i < 0 || i >= size || length < 0 || length > size || (i + length > size)) { + throw new IndexOutOfBoundsException(); + } + + Node node = head; + Node pre = head; + Node iNode = head; + for (int j = 0; j < i + length; j++) { + if (node.next != null) { + pre = node; + if (j == (i - 1)) { + iNode = node; + } + node = node.next; + } + } + + if (i == 0) { + head = node; + } else { + iNode.next = node; + } + + pre.next = null; + pre = null; + size = size() - length; + + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + if (list == null || list.size() == 0) { + throw new IndexOutOfBoundsException(); + } + int[] arr = new int[list.size()]; + for (int i = 1; i < list.size(); i++) { + int index = (Integer) list.get(i); + arr[i] = (Integer) get(index); + } + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedList list) { + if (list == null || list.size() == 0) { + return; + } + for (int i = 0; i < list.size(); i++) { + int data = (Integer) list.get(i); + LinkedListIterator iterator = (LinkedListIterator) this.iterator(); + int index = 0; + while (iterator.hasNext()) { + int obj = (Integer) iterator.next(); + if (obj == data) { + remove(index); + iterator.moveFirst(); + break; + } + index++; + } + } + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if (head == null) { + return; + } + Node node = head; + Node pre = head; + while (node.next != null) { + node = node.next; + int value = (Integer) pre.data; + if ((Integer) node.data == value) { + //如果node 下一个是null.直接把node前一个pre的next指向空 + if (node.next == null) { + pre.next = null; + size--; + break; + } + pre.next = node.next; + node = node.next; + size--; + } + pre = pre.next; + } + } + + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + + + + + + + + + + + + + + +} + diff --git a/group08/1425809544/03-12/code/com/xyy/baselinked/List.java b/group08/1425809544/03-12/code/com/xyy/baselinked/List.java new file mode 100644 index 0000000000..39a0e1ba83 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/baselinked/List.java @@ -0,0 +1,17 @@ +package xyy.baselinked; + +/** + * Created by 14258 on 2017/3/14. + */ +public interface List { + + public void add(Object o); + + public void add(int index, Object o); + + public Object remove(int index); + + public int size(); + + public Object get(int index); +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/DownloadThread.java b/group08/1425809544/03-12/code/com/xyy/download/DownloadThread.java new file mode 100644 index 0000000000..f81c42098d --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/DownloadThread.java @@ -0,0 +1,67 @@ +package xyy.download; + +import xyy.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * Created by 14258 on 2017/3/14. + */ +public class DownloadThread extends Thread { + + private int startPos; + private int endPos; + private boolean isDownloadEnd; + private String threadName; + private Connection connection; + private FileDownloader fileDownLoader; + + public DownloadThread(Connection connection, int startPos, int endPos, String threadName, FileDownloader fileDownloader) { + this.startPos = startPos; + this.endPos = endPos; + this.threadName = threadName; + this.connection = connection; + this.fileDownLoader = fileDownloader; + this.setName(threadName); + } + + + @Override + public void run(){ + try { + byte [] data = connection.read(startPos,endPos); + connection.close(); + System.out.println("下载线程名字"+threadName+"正在读取开始位置"+startPos+"结束位置"+endPos); + + int writelen=-1; + RandomAccessFile randomAccessFile = null; + randomAccessFile = new RandomAccessFile(fileDownLoader.fileName,"rw" ); + randomAccessFile.seek(startPos); + randomAccessFile.write(data,0,data.length); + writelen = data.length; + + isDownloadEnd = true; + fileDownLoader.addDownNumber(); + + + + + + + + + + + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + + + +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/FileDownloader.java b/group08/1425809544/03-12/code/com/xyy/download/FileDownloader.java new file mode 100644 index 0000000000..44ed7aba52 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/FileDownloader.java @@ -0,0 +1,116 @@ +package xyy.download; + +import vvv.download.api.ConnectionException; +import xyy.download.api.Connection; +import xyy.download.api.ConnectionManager; +import xyy.download.api.DownloadListener; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * Created by 14258 on 2017/3/14. + */ +public class FileDownloader { + + private static final int threadNumber = 3;//下载线程数 + private String url;//传入的url地址 + public String fileName = "D://download"; + + private DownloadListener listener;//下载监听器; + private ConnectionManager connectionManager;//下载管理器; + //设置url + public FileDownloader(String url) { + this.url = url; + } + //设置下载链接管理 + public void setConnectionManager(ConnectionManager connectionManager) { + this.connectionManager = connectionManager; + } + //设置下载监听器 + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + //执行下载 + public void execute() { + + Connection conn = null; + try { + try { + conn = connectionManager.open(this.url);//又连接管理器打开根据url打开来连接 + } catch (ConnectionException e) { + e.printStackTrace(); + } + int length = conn.getContentLength();//获取conn长度 + this.fileName = fileName + "//" + conn.getFileName();//获取文件名字 + conn.close(); + startDownload(length, threadNumber); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + + } + + private void startDownload(int length, int i) { + if (length <= 0) { + listener.notifyFinished(); + return; + } + + //设置一个和将要下载的文件一个同样大小的临时文件 + RandomAccessFile randomAccessFile = null; + try { + randomAccessFile = new RandomAccessFile(this.fileName, "rw"); + randomAccessFile.setLength(length); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + randomAccessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + int block = length / threadNumber; + block = block == 0 ? block : block + 1; + System.out.println("length"+length+"block"+block); + for (i=0;i=threadNumber){ + if (listener!=null){ + listener.notifyFinished(); + } + } + + + + + } +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/FileDownloaderTest.java b/group08/1425809544/03-12/code/com/xyy/download/FileDownloaderTest.java new file mode 100644 index 0000000000..d0c533bd00 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/FileDownloaderTest.java @@ -0,0 +1,53 @@ +package xyy.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import xyy.download.api.ConnectionManager; +import xyy.download.api.DownloadListener; +import xyy.download.impl.ConnectionManagerImpl; + +/** + * Created by 14258 on 2017/3/14. + */ +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://img.71lady.com/uploads/allimg/1701/2-1F11GKT4.jpg"; + FileDownloader fileDownloader = new FileDownloader(url); + ConnectionManager connectionManager = new ConnectionManagerImpl(); + fileDownloader.setConnectionManager(connectionManager); + fileDownloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + fileDownloader.execute(); + + while (!downloadFinished) { + System.out.print("还没有下载完成,休眠五秒"); + try { + Thread.sleep(5 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + } + +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/api/Connection.java b/group08/1425809544/03-12/code/com/xyy/download/api/Connection.java new file mode 100644 index 0000000000..de6a9f339f --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/api/Connection.java @@ -0,0 +1,32 @@ +package xyy.download.api; + +import java.io.IOException; + +/** + * Created by 14258 on 2017/3/14. + */ +public interface Connection { + + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + public String getFileName(); + + /** + * 关闭连接 + */ + public void close(); + + +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/api/ConnectionException.java b/group08/1425809544/03-12/code/com/xyy/download/api/ConnectionException.java new file mode 100644 index 0000000000..9877e4bf14 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/api/ConnectionException.java @@ -0,0 +1,11 @@ +package xyy.download.api; + +/** + * Created by 14258 on 2017/3/14. + */ +public interface ConnectionException { + + + + +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/api/ConnectionManager.java b/group08/1425809544/03-12/code/com/xyy/download/api/ConnectionManager.java new file mode 100644 index 0000000000..bd2c9fff44 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/api/ConnectionManager.java @@ -0,0 +1,25 @@ +package xyy.download.api; + +import vvv.download.api.ConnectionException; + +import java.io.IOException; + +/** + * Created by 14258 on 2017/3/14. + */ +public interface ConnectionManager { + + + /** + * 给定一个url , 打开一个连接 + * + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException, IOException; + + + + + +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/api/DownloadListener.java b/group08/1425809544/03-12/code/com/xyy/download/api/DownloadListener.java new file mode 100644 index 0000000000..ded3895179 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/api/DownloadListener.java @@ -0,0 +1,9 @@ +package xyy.download.api; + +/** + * Created by 14258 on 2017/3/14. + */ +public interface DownloadListener { + + public void notifyFinished(); +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/impl/ConnectionImpl.java b/group08/1425809544/03-12/code/com/xyy/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..b40b123a1f --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/impl/ConnectionImpl.java @@ -0,0 +1,120 @@ +package xyy.download.impl; + +import xyy.download.api.Connection; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLDecoder; + +/** + * Created by 14258 on 2017/3/14. + */ +public class ConnectionImpl implements Connection { + + + private HttpURLConnection httpUrlConnection;//连接 + private String url;//url + private String contentType;//类型 + private String contentFileName;//文件名 + private int contentLength;//文件长度 + + + public ConnectionImpl(String url) throws IOException { + this.url = url; + httpUrlConnection = createConn(this.url); + if (httpUrlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { + this.contentLength = httpUrlConnection.getContentLength(); + this.contentType = httpUrlConnection.getContentType(); + this.contentFileName = getName(); + System.out.println("contentType" + httpUrlConnection.getContentType() + "fileName" + this.contentFileName + "contentType" + contentType); + } + } + + public ConnectionImpl(String url, boolean b) throws IOException { + close(); + this.url = url; + httpUrlConnection = createConn(this.url); + } + + private String getName() { + String fileName; + String disposition = httpUrlConnection.getHeaderField("Content-Disposition"); + if (disposition != null && !"".equals(disposition)) { + fileName = disposition.split(";")[1].split("=")[1].replaceAll("\"", ""); + } else { + fileName = url.substring(url.lastIndexOf("/") + 1); + } + + if (fileName != null && !"".equals(fileName)) { + try { + fileName = URLDecoder.decode(fileName, "utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } else { + fileName = "file_" + (int) (Math.random() * 10); + } + return fileName; + } + + + private HttpURLConnection createConn(String url) throws IOException { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(5 * 1000); + conn.setReadTimeout(10 * 1000); + conn.setRequestMethod("GET"); + conn.setRequestProperty("User-Agent", "vvv download"); + conn.setRequestProperty("Connection", "Keep-Alive"); + conn.setRequestProperty("Keep-Alive", "300"); + return conn; + } + + + //读链接 + @Override + public byte[] read(int startPos, int endPos) throws IOException { + return new byte[0]; + } + + //获取链接长度 + @Override + public int getContentLength() { + return this.contentLength; + } + + //获取文件名字 + @Override + public String getFileName() { + return this.contentFileName; + } + + //关闭连接 + @Override + public void close() { + if (httpUrlConnection != null) { + httpUrlConnection.disconnect(); + httpUrlConnection = null; + } + } + + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public String getContentFileName() {return contentFileName;} + + public void setContentFileName(String contentFileName) { + this.contentFileName = contentFileName; + } + + public void setContentLength(int contentLength) { + this.contentLength = contentLength; + } +} diff --git a/group08/1425809544/03-12/code/com/xyy/download/impl/ConnectionManagerImpl.java b/group08/1425809544/03-12/code/com/xyy/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..05f9f401c6 --- /dev/null +++ b/group08/1425809544/03-12/code/com/xyy/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,31 @@ +package xyy.download.impl; + + +import vvv.download.api.ConnectionException; +import xyy.download.api.Connection; +import xyy.download.api.ConnectionManager; + +import java.io.IOException; + +/** + * Created by 14258 on 2017/3/14. + */ +public class ConnectionManagerImpl implements ConnectionManager { + private String url; + + @Override + public Connection open(String url) throws ConnectionException, IOException { + + Connection conn = null; + if (!url.equals(this.url)){ + conn = new ConnectionImpl(url); + this.url = url; + }else { + conn = new ConnectionImpl(url, false); + } + + + + return conn; + } +} diff --git "a/group08/1425809544/1425809544-\345\215\232\345\256\242\345\234\260\345\235\200.md" "b/group08/1425809544/1425809544-\345\215\232\345\256\242\345\234\260\345\235\200.md" new file mode 100644 index 0000000000..d314d00620 --- /dev/null +++ "b/group08/1425809544/1425809544-\345\215\232\345\256\242\345\234\260\345\235\200.md" @@ -0,0 +1,4 @@ +## 博客 +- [文章链接-java集合 容器 简单概述](http://blog.csdn.net/qq_25385555/article/month/2017/02) +- [文章链接-计算机存储器结构](http://blog.csdn.net/qq_25385555/article/month/2017/03) +-[文章链接-2017-3月- 工作-随想](http://blog.csdn.net/qq_25385555/article/details/62226463) diff --git a/group08/1425809544/1425809544.md b/group08/1425809544/1425809544.md deleted file mode 100644 index 326924a615..0000000000 --- a/group08/1425809544/1425809544.md +++ /dev/null @@ -1,3 +0,0 @@ -## 博客 -- [文章链接-java集合 容器 简单概述](http://blog.csdn.net/qq_25385555/article/month/2017/02) -- [文章链接-计算机存储器结构](http://blog.csdn.net/qq_25385555/article/month/2017/03) diff --git a/group08/286060098/3-27/src/com/coderising/jvm/loader/ClassFileLoader.java b/group08/286060098/3-27/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..fe086e2d67 --- /dev/null +++ b/group08/286060098/3-27/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,48 @@ +package com.coderising.jvm.loader; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import com.google.common.base.Joiner; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + private static final Joiner JOINER = Joiner.on(";").skipNulls(); + + public byte[] readBinaryCode(String className) throws FileNotFoundException { + byte[] codeBytes; + for (String path : clzPaths) { + String clzPath = path + className.replace(".", "/") + ".class"; + try { + byte[] buffer = new byte[1024]; + int size = 0; + int index = 0; + InputStream in = new FileInputStream(clzPath); + codeBytes = new byte[in.available()]; + while ((size = in.read(buffer)) != -1) { + for (int i = 0; i < size; i++) { + codeBytes[index++] = buffer[i]; + } + } + return codeBytes; + } catch (Exception e) { + + } + } + return null; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + return JOINER.join(clzPaths); + } + +} diff --git a/group08/286060098/3-27/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group08/286060098/3-27/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..843e5a06a2 --- /dev/null +++ b/group08/286060098/3-27/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,80 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + +import java.io.FileNotFoundException; + +public class ClassFileloaderTest { + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() throws FileNotFoundException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(ClassFileloaderTest.class.getResource("/").getPath()); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber() throws FileNotFoundException { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(ClassFileloaderTest.class.getResource("/").getPath()); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} diff --git a/group08/286060098/3-27/src/com/coderising/jvm/test/EmployeeV1.java b/group08/286060098/3-27/src/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..55cf214fe2 --- /dev/null +++ b/group08/286060098/3-27/src/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group08/286060098/3-27/src/com/coding/basic/linklist/LRUPageFrame.java b/group08/286060098/3-27/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..3a79c1d5b5 --- /dev/null +++ b/group08/286060098/3-27/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,142 @@ +package com.coding.basic.linklist; + +import java.util.Objects; + +/** + * 用双向链表实现LRU算法 + */ +@SuppressWarnings("unchecked") +public class LRUPageFrame { + + // 容量 + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param pagNum + * @return + */ + public void access(int pagNum) { + if (first == null) { + first = new Node(); + first.setPageNum(pagNum); + return; + } + if (last == null) { + last = new Node(); + last.pageNum = first.pageNum; + first.pageNum = pagNum; + first.next = last; + last.prev = first; + return; + } + addNode(pagNum); + } + + private void addNode(int pagNum) { + // 找得到 + Node node = findNode(pagNum); + if (node == null) { + node = new Node(); + node.setPageNum(first.getPageNum()); + first.pageNum = pagNum; + first.next.prev = node; + node.next = first.next; + first.next = node; + node.prev = first; + } else { + if (node.prev == null) { + return; + } else if (node.next == null) { + node.prev.next = null; + } else { + node.next.prev = node.prev; + node.prev.next = node.next; + } + node = new Node(); + node.pageNum = first.pageNum; + node.next = first.next; + first.next.prev = node; + node.prev = first; + first.next = node; + first.pageNum = pagNum; + } + Node tmp = first; + int i = 1; + while (tmp.next != null && i < capacity) { + tmp = tmp.next; + i++; + } + tmp.next = null; + last = tmp; + } + + private Node findNode(int pageNum) { + Node tmp = first; + while (tmp != null) { + if (Objects.equals(tmp.getPageNum(), pageNum)) { + return tmp; + } + tmp = tmp.next; + } + return null; + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.getPageNum()); + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + + private static class Node { + + Node prev; + + Node next; + + int pageNum; + + Node() { + } + + public int getPageNum() { + return pageNum; + } + + public Node getNext() { + return next; + } + + public Node getPrev() { + return prev; + } + + public void setNext(Node next) { + this.next = next; + } + + public void setPageNum(int pageNum) { + this.pageNum = pageNum; + } + + public void setPrev(Node prev) { + this.prev = prev; + } + } + +} diff --git a/group08/286060098/3-27/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group08/286060098/3-27/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..4fcac1cdb3 --- /dev/null +++ b/group08/286060098/3-27/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,29 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group08/619057560/4-2/code/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java b/group08/619057560/4-2/code/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..6c0b2dcade --- /dev/null +++ b/group08/619057560/4-2/code/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,154 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + private int length;// 链表长度 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + this.length = 0; + + } + + private Node findNode(int pageNum) { + for (Node pNode = first; pNode != null; pNode = pNode.next) { + if (pNode.pageNum == pageNum) { + return pNode; + } + } + + return null; + } + + private void ensureLength() { + while (length > capacity && last != null) { + last = last.prev; + length--; + } + + if (last == null) { + first = null; + } + else { + last.next = null; + } + } + + private void addFirstNode(Node pNode) { + if (pNode == null) { + return; + } + + pNode.next = first; + + if (first != null) { + first.prev = pNode; + } + + first = pNode; + + if (last == null) { + last = pNode; + } + + length++; + } + + private Node removeNode(Node pNode) { + if (pNode == null) { + return null; + } + + Node prevN = pNode.prev; + Node nextN = pNode.next; + + if (pNode == first) { + first = nextN; + } + if (pNode == last) { + last = prevN; + } + if (prevN != null) { + prevN.next = nextN; + } + if (nextN != null) { + nextN.prev = prevN; + } + + pNode.prev = null; + pNode.next = null; + length--; + + return pNode; + } + + private void addNewPage(int pageNum) { + Node pNode = new Node(); + pNode.pageNum = pageNum; + addFirstNode(pNode); + } + + private void movePageToFirst(Node pNode) { + if (pNode == null) { + return; + } + + addFirstNode(removeNode(pNode)); + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + Node pNode = findNode(pageNum); + if (pNode == null) { + addNewPage(pageNum); + ensureLength(); + return; + } + + movePageToFirst(pNode); + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group08/619057560/4-2/code/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group08/619057560/4-2/code/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..67cf36067b --- /dev/null +++ b/group08/619057560/4-2/code/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group08/619057560/4-2/code/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/group08/619057560/4-2/code/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..86c0e63054 --- /dev/null +++ b/group08/619057560/4-2/code/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,87 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + for (String path: clzPaths) { + String fileName = path + '/' + className.replace('.', '/') + ".class"; + System.out.println(fileName); + File file = new File(fileName); + if (file.exists()) { + return loadClassFile(fileName); + } + } + return null; + + + } + + private byte[] loadClassFile(String clzFileName) { + File file = new File(clzFileName); + int len; + int bufferLen = 100; + byte[] buffer = new byte[bufferLen]; + FileInputStream fis = null; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + fis = new FileInputStream(file); + while ((len = fis.read(buffer, 0, bufferLen)) >= 0) { + bos.write(buffer, 0, len); + } + return bos.toByteArray(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath_V1(){ + + return null; + } + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + for (String path: clzPaths) { + sb.append(path).append(";"); + } + sb.deleteCharAt(sb.length()-1); + return sb.toString(); + } + + + + + +} diff --git a/group08/619057560/4-2/code/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group08/619057560/4-2/code/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..50eb1be6fa --- /dev/null +++ b/group08/619057560/4-2/code/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;ihttp://blog.csdn.net/qq1332479771/article/details/57597710 - -##121027265 ->https://github.com/Greastate/coding2017/blob/master/group08/121027265/0226/cpu_%E5%86%85%E5%AD%98_%E7%A1%AC%E7%9B%98_%E6%8C%87%E4%BB%A4%E4%B9%8B%E9%97%B4%E5%85%B3%E7%B3%BB.md - -##1425809544 ->http://blog.csdn.net/qq_25385555/article/details/60324265 - -##283677872 ->https://github.com/Greastate/coding2017/blob/master/group08/283677872/2-26/CPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E5%85%B3%E7%B3%BB.pdf - -##286060098 ->https://github.com/Greastate/coding2017/blob/master/group08/286060098/2-26/blong/%E5%9F%BA%E6%9C%AC%E7%BB%93%E6%9E%84.md - -##406166841 -https://github.com/Greastate/coding2017/blob/master/group08/406166841/2-26/CPU.md - -##529757467 ->https://github.com/Greastate/coding2017/blob/master/group08/529757467/2017-02-26%E4%BD%9C%E4%B8%9A/README.md - -##619057560 ->https://github.com/Greastate/coding2017/blob/master/group08/619057560/2-26/article/CPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB.pdf - -##649859235 -> http://note.youdao.com/noteshare?id=a0345eff655b1cfc5877cc267452eed0&sub=FE5CD301B3C742269D04973B0DD2393C - -##729770920 ->https://github.com/Greastate/coding2017/blob/master/group08/729770920/2-26/README.md - -##782476895 ->https://github.com/Greastate/coding2017/blob/master/group08/782476895/20170225/README.md - -#第二周文章 -##1425809544 ->http://m.blog.csdn.net/article/details?id=56674070 - -##1509102580 ->https://github.com/Greastate/coding2017/blob/master/group08/1509102580/3.5/%E6%96%87%E7%AB%A0-%E4%B8%80%E5%91%A8%E7%AC%94%E8%AE%B0 - -##286060098 ->http://www.jianshu.com/p/ccea84c2a6ba# - -##529757467 ->https://github.com/Greastate/coding2017/blob/master/group08/529757467/2017-03-05%E4%BD%9C%E4%B8%9A/README.md - -##619057560 ->https://github.com/Greastate/coding2017/blob/master/group08/619057560/3-5/article/%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%90%E8%A1%8C%E5%92%8C%E6%B1%87%E7%BC%96%E4%BB%A3%E7%A0%81.pdf - -##649859235 -> http://note.youdao.com/noteshare?id=1ad407f0dec5587138d9af273e491bca&sub=468E975854CE4B8F8CEF4D564E21842F - -##782476895 ->https://github.com/Greastate/coding2017/blob/master/group08/782476895/20170305/README.md - -#第三周文章 -##619057560 ->https://github.com/Greastate/coding2017/blob/master/group08/619057560/3-12/article/Java%E6%B5%81%E7%9A%84%E4%BB%8B%E7%BB%8D.pdf - -##649859235 -> http://note.youdao.com/noteshare?id=a0e00ce473692d46ac87881ad77c3b11&sub=C46D2B83C8074C6A8BF4C024F3B057D3 - -##529757467 ->http://www.jianshu.com/p/b773756f741f \ No newline at end of file diff --git "a/group08/\345\205\253\347\273\204\344\275\234\344\270\232\345\256\214\346\210\220\346\203\205\345\206\265.xlsx" "b/group08/\345\205\253\347\273\204\344\275\234\344\270\232\345\256\214\346\210\220\346\203\205\345\206\265.xlsx" deleted file mode 100644 index 46d6fa61e4..0000000000 Binary files "a/group08/\345\205\253\347\273\204\344\275\234\344\270\232\345\256\214\346\210\220\346\203\205\345\206\265.xlsx" and /dev/null differ diff --git a/group09/41689722.eulerlcs/2.code/jmr-01-aggregator/pom.xml b/group09/41689722.eulerlcs/2.code/jmr-01-aggregator/pom.xml index b04166b31c..3edeb21d2e 100644 --- a/group09/41689722.eulerlcs/2.code/jmr-01-aggregator/pom.xml +++ b/group09/41689722.eulerlcs/2.code/jmr-01-aggregator/pom.xml @@ -10,8 +10,11 @@ ../jmr-02-parent ../jmr-11-challenge - ../jmr-61-170226-collection - ../jmr-61-170305-litestruts - ../jmr-61-170312-multiThreadDownload + ../jmr-51-liuxin-question + ../jmr-52-liuxin-answer + ../jmr-61-collection + ../jmr-62-litestruts + ../jmr-63-download + ../jmr-64-minijvm \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/data/shoppingcart/shoppingcart.data b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/data/shoppingcart/shoppingcart.data new file mode 100644 index 0000000000..336fd732b4 Binary files /dev/null and b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/data/shoppingcart/shoppingcart.data differ diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/classloader/core/NetworkClassLoader.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/classloader/core/NetworkClassLoader.java index 0241e92a5c..c0f524a1b4 100644 --- a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/classloader/core/NetworkClassLoader.java +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/classloader/core/NetworkClassLoader.java @@ -5,9 +5,9 @@ import java.net.URL; public class NetworkClassLoader extends ClassLoader { - + private String rootUrl; - + public NetworkClassLoader(String rootUrl) { this.rootUrl = rootUrl; } @@ -16,12 +16,11 @@ protected Class findClass(String name) throws ClassNotFoundException { byte[] classData = getClassData(name); if (classData == null) { throw new ClassNotFoundException(); - } - else { + } else { return defineClass(name, classData, 0, classData.length); } } - + private byte[] getClassData(String className) { String path = classNameToPath(className); try { @@ -40,9 +39,8 @@ private byte[] getClassData(String className) { } return null; } - + private String classNameToPath(String className) { - return rootUrl + "/" - + className.replace('.', '/') + ".class"; + return rootUrl + "/" + className.replace('.', '/') + ".class"; } } diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170205.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170205.java new file mode 100644 index 0000000000..b18db9d387 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170205.java @@ -0,0 +1,132 @@ +package com.github.eulerlcs.jmr.challenge.zzz.master170219; + +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; + +public class Try170205 { + public static void main(String[] args) throws Exception { + task94(); + task93(); + task84(); + task83(); + task65(); + task64(); + + ArrayList list = new ArrayList(); + show(list); + } + + public static void show(ArrayList list) { + // .... + } + + public static void task64() { + DataInputStream dis = null; + double price = 0; + int count = 0; + double sum = 0; + String disp = ""; + + try { + dis = new DataInputStream(new FileInputStream("data/shoppingcart.data")); + while (dis.available() > 0) { + price = dis.readDouble(); + count = dis.readInt(); + disp = dis.readUTF(); + System.out.println(disp); + sum += price * count; + } + + System.out.println("sum=" + sum); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + dis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public static void task65() { + DataInputStream dis = null; + byte[] magic = { (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe }; + boolean ret = true; + + try { + dis = new DataInputStream(new FileInputStream("data/sc.class")); + for (int i = 0; i < 4; i++) { + if (magic[i] != dis.readByte()) { + ret = false; + break; + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + dis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if (ret) { + System.out.println("it is cafebabe"); + } else { + System.out.println("it is not cafebabe"); + } + } + + public static void task83() throws Exception { + Class clazz = Class.forName("shoppingcart.Employee"); + Constructor ct = clazz.getConstructor(String.class, int.class); + Object obj = ct.newInstance("ref", 22); + + Method sayHello = clazz.getDeclaredMethod("sayHello"); + sayHello.invoke(obj); + + Method getID = clazz.getDeclaredMethod("getID"); + getID.setAccessible(true); + String ids = (String) getID.invoke(obj); + System.out.println("getID=" + ids); + + Field[] flds = clazz.getDeclaredFields(); + for (Field fld : flds) { + System.out.println(fld); + } + } + + public static void task84() throws Exception { + ArrayList list = new ArrayList<>(); + list.add(3232); + + Class clazz = ArrayList.class; + + Field elementDataField = clazz.getDeclaredField("elementData"); + elementDataField.setAccessible(true); + Object[] elementData = (Object[]) elementDataField.get(list); + if (elementData.length > 1) { + elementData[1] = "added by reflection"; + } + } + + public static void task93() { + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + System.out.println(list1.getClass().equals(list2.getClass())); + } + + public static void task94() { + ArrayList numbers = new ArrayList(); + numbers.add(new Integer(10)); + numbers.add(new Double(10.0d)); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170212.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170212.java new file mode 100644 index 0000000000..5492441572 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170212.java @@ -0,0 +1,34 @@ +package com.github.eulerlcs.jmr.challenge.zzz.master170219; + +public class Try170212 { + + public static void main(String[] args) { + t28(); + } + + public static void changeStr(String str) { + str = "welcome"; + } + + public static void t28() { + String str = "1234"; + changeStr(str); + System.out.println(str); + } + + public static void t34() { + Try170212 x = new Try170212(); + Try170212.Hello obj = x.new Hello(""); + obj.msg += ",World!"; + System.out.println(obj.msg); + } + + class Hello { + public String msg = "Hello"; + + public Hello(String msg) { + this.msg = msg; + } + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170219.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170219.java new file mode 100644 index 0000000000..559dc44a7c --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/github/eulerlcs/jmr/challenge/zzz/master170219/Try170219.java @@ -0,0 +1,47 @@ +package com.github.eulerlcs.jmr.challenge.zzz.master170219; + +import java.util.ArrayList; +import java.util.Date; + +public class Try170219 { + public static void main(String[] args) { + Integer[] a = new Integer[10]; + case003(a); + } + + public static void case001() { + Fruit f = new Apple(); + f.setDate(new Date()); + } + + public static void case002() { + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + System.out.println(list1.getClass().equals(list2.getClass())); + } + + public static void case003(Number[] n) { + // nop + } + +} + +class Fruit { + public void setDate(Object d) { + System.out.println("Fruit.setDate(Object d)"); + } + + // public void setDate2(Object d) { + // System.out.println("Fruit.setDate(Object d)"); + // } +} + +class Apple extends Fruit { + public void setDate(Date d) { + System.out.println("Apple.setDate(Date d)"); + } + + public void setDate2(Date d) { + System.out.println("Apple.setDate(Date d)"); + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/BatchDownloadFile.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/BatchDownloadFile.java new file mode 100644 index 0000000000..48b4f67670 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/BatchDownloadFile.java @@ -0,0 +1,227 @@ +package com.hoo.download; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.hoo.entity.DownloadInfo; +import com.hoo.util.LogUtils; + +/** + * function: 分批量下载文件 + * + * @author hoojo + * @createDate 2011-9-22 下午05:51:54 + * @file BatchDownloadFile.java + * @package com.hoo.download + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public class BatchDownloadFile implements Runnable { + // 下载文件信息 + private DownloadInfo downloadInfo; + // 一组开始下载位置 + private long[] startPos; + // 一组结束下载位置 + private long[] endPos; + // 休眠时间 + private static final int SLEEP_SECONDS = 500; + // 子线程下载 + private DownloadFile[] fileItem; + // 文件长度 + private int length; + // 是否第一个文件 + private boolean first = true; + // 是否停止下载 + private boolean stop = false; + // 临时文件信息 + private File tempFile; + + public BatchDownloadFile(DownloadInfo downloadInfo) { + this.downloadInfo = downloadInfo; + String tempPath = this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName() + ".position"; + tempFile = new File(tempPath); + // 如果存在读入点位置的文件 + if (tempFile.exists()) { + first = false; + // 就直接读取内容 + try { + readPosInfo(); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + // 数组的长度就要分成多少段的数量 + startPos = new long[downloadInfo.getSplitter()]; + endPos = new long[downloadInfo.getSplitter()]; + } + } + + @Override + public void run() { + // 首次下载,获取下载文件长度 + if (first) { + length = this.getFileSize();// 获取文件长度 + if (length == -1) { + LogUtils.log("file length is know!"); + stop = true; + } else if (length == -2) { + LogUtils.log("read file length is error!"); + stop = true; + } else if (length > 0) { + /** + * eg start: 1, 3, 5, 7, 9 end: 3, 5, 7, 9, length + */ + for (int i = 0, len = startPos.length; i < len; i++) { + int size = i * (length / len); + startPos[i] = size; + + // 设置最后一个结束点的位置 + if (i == len - 1) { + endPos[i] = length; + } else { + size = (i + 1) * (length / len); + endPos[i] = size; + } + LogUtils.log("start-end Position[" + i + "]: " + startPos[i] + "-" + endPos[i]); + } + } else { + LogUtils.log("get file length is error, download is stop!"); + stop = true; + } + } + + // 子线程开始下载 + if (!stop) { + // 创建单线程下载对象数组 + fileItem = new DownloadFile[startPos.length];// startPos.length = + // downloadInfo.getSplitter() + for (int i = 0; i < startPos.length; i++) { + try { + // 创建指定个数单线程下载对象,每个线程独立完成指定块内容的下载 + fileItem[i] = new DownloadFile(downloadInfo.getUrl(), + this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName(), startPos[i], + endPos[i], i); + fileItem[i].start();// 启动线程,开始下载 + LogUtils.log("Thread: " + i + ", startPos: " + startPos[i] + ", endPos: " + endPos[i]); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // 循环写入下载文件长度信息 + while (!stop) { + try { + writePosInfo(); + LogUtils.log("downloading……"); + Thread.sleep(SLEEP_SECONDS); + stop = true; + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int i = 0; i < startPos.length; i++) { + if (!fileItem[i].isDownloadOver()) { + stop = false; + break; + } + } + } + LogUtils.info("Download task is finished!"); + } + } + + /** + * 将写入点数据保存在临时文件中 + * + * @author hoojo + * @createDate 2011-9-23 下午05:25:37 + * @throws IOException + */ + private void writePosInfo() throws IOException { + DataOutputStream dos = new DataOutputStream(new FileOutputStream(tempFile)); + dos.writeInt(startPos.length); + for (int i = 0; i < startPos.length; i++) { + dos.writeLong(fileItem[i].getStartPos()); + dos.writeLong(fileItem[i].getEndPos()); + // LogUtils.info("[" + fileItem[i].getStartPos() + "#" + + // fileItem[i].getEndPos() + "]"); + } + dos.close(); + } + + /** + * function:读取写入点的位置信息 + * + * @author hoojo + * @createDate 2011-9-23 下午05:30:29 + * @throws IOException + */ + private void readPosInfo() throws IOException { + DataInputStream dis = new DataInputStream(new FileInputStream(tempFile)); + int startPosLength = dis.readInt(); + startPos = new long[startPosLength]; + endPos = new long[startPosLength]; + for (int i = 0; i < startPosLength; i++) { + startPos[i] = dis.readLong(); + endPos[i] = dis.readLong(); + } + dis.close(); + } + + /** + * function: 获取下载文件的长度 + * + * @author hoojo + * @createDate 2011-9-26 下午12:15:08 + * @return + */ + private int getFileSize() { + int fileLength = -1; + try { + URL url = new URL(this.downloadInfo.getUrl()); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + DownloadFile.setHeader(conn); + + int stateCode = conn.getResponseCode(); + // 判断http status是否为HTTP/1.1 206 Partial Content或者200 OK + if (stateCode != HttpURLConnection.HTTP_OK && stateCode != HttpURLConnection.HTTP_PARTIAL) { + LogUtils.log("Error Code: " + stateCode); + return -2; + } else if (stateCode >= 400) { + LogUtils.log("Error Code: " + stateCode); + return -2; + } else { + // 获取长度 + fileLength = conn.getContentLength(); + LogUtils.log("FileLength: " + fileLength); + } + + // 读取文件长度 + /* + * for (int i = 1; ; i++) { String header = + * conn.getHeaderFieldKey(i); if (header != null) { if + * ("Content-Length".equals(header)) { fileLength = + * Integer.parseInt(conn.getHeaderField(i)); break; } } else { + * break; } } + */ + + DownloadFile.printHeader(conn); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return fileLength; + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/DownloadFile.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/DownloadFile.java new file mode 100644 index 0000000000..784efa1d88 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/DownloadFile.java @@ -0,0 +1,176 @@ +package com.hoo.download; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.hoo.util.LogUtils; + +/** + * function: 单线程下载文件 + * + * @author hoojo + * @createDate 2011-9-22 下午02:55:10 + * @file DownloadFile.java + * @package com.hoo.download + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public class DownloadFile extends Thread { + + // 下载文件url + private String url; + // 下载文件起始位置 + private long startPos; + // 下载文件结束位置 + private long endPos; + // 线程id + private int threadId; + + // 下载是否完成 + private boolean isDownloadOver = false; + + private SaveItemFile itemFile; + + private static final int BUFF_LENGTH = 1024 * 8; + + /** + * @param url + * 下载文件url + * @param name + * 文件名称 + * @param startPos + * 下载文件起点 + * @param endPos + * 下载文件结束点 + * @param threadId + * 线程id + * @throws IOException + */ + public DownloadFile(String url, String name, long startPos, long endPos, int threadId) throws IOException { + super(); + this.url = url; + this.startPos = startPos; + this.endPos = endPos; + this.threadId = threadId; + // 分块下载写入文件内容 + this.itemFile = new SaveItemFile(name, startPos); + } + + @Override + public void run() { + while (endPos > startPos && !isDownloadOver) { + try { + URL url = new URL(this.url); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + // 设置连接超时时间为10000ms + conn.setConnectTimeout(10000); + // 设置读取数据超时时间为10000ms + conn.setReadTimeout(10000); + + setHeader(conn); + + String property = "bytes=" + startPos + "-"; + conn.setRequestProperty("RANGE", property); + + // 输出log信息 + LogUtils.log("开始 " + threadId + ":" + property + endPos); + // printHeader(conn); + + // 获取文件输入流,读取文件内容 + InputStream is = conn.getInputStream(); + + byte[] buff = new byte[BUFF_LENGTH]; + int length = -1; + LogUtils.log("#start#Thread: " + threadId + ", startPos: " + startPos + ", endPos: " + endPos); + while ((length = is.read(buff)) > 0 && startPos < endPos && !isDownloadOver) { + // 写入文件内容,返回最后写入的长度 + startPos += itemFile.write(buff, 0, length); + } + LogUtils.log("#over#Thread: " + threadId + ", startPos: " + startPos + ", endPos: " + endPos); + LogUtils.log("Thread " + threadId + " is execute over!"); + this.isDownloadOver = true; + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (itemFile != null) { + itemFile.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + if (endPos < startPos && !isDownloadOver) { + LogUtils.log("Thread " + threadId + " startPos > endPos, not need download file !"); + this.isDownloadOver = true; + } + if (endPos == startPos && !isDownloadOver) { + LogUtils.log("Thread " + threadId + " startPos = endPos, not need download file !"); + this.isDownloadOver = true; + } + } + + /** + * function: 打印下载文件头部信息 + * + * @author hoojo + * @createDate 2011-9-22 下午05:44:35 + * @param conn + * HttpURLConnection + */ + public static void printHeader(URLConnection conn) { + int i = 1; + while (true) { + String header = conn.getHeaderFieldKey(i); + i++; + if (header != null) { + LogUtils.info(header + ":" + conn.getHeaderField(i)); + } else { + break; + } + } + } + + /** + * function: 设置URLConnection的头部信息,伪装请求信息 + * + * @author hoojo + * @createDate 2011-9-28 下午05:29:43 + * @param con + */ + public static void setHeader(URLConnection conn) { + conn.setRequestProperty("User-Agent", + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3"); + conn.setRequestProperty("Accept-Language", "en-us,en;q=0.7,zh-cn;q=0.3"); + conn.setRequestProperty("Accept-Encoding", "utf-8"); + conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); + conn.setRequestProperty("Keep-Alive", "300"); + conn.setRequestProperty("connnection", "keep-alive"); + conn.setRequestProperty("If-Modified-Since", "Fri, 02 Jan 2009 17:00:05 GMT"); + conn.setRequestProperty("If-None-Match", "\"1261d8-4290-df64d224\""); + conn.setRequestProperty("Cache-conntrol", "max-age=0"); + conn.setRequestProperty("Referer", "https://www.github.com"); + } + + public boolean isDownloadOver() { + return isDownloadOver; + } + + public long getStartPos() { + return startPos; + } + + public long getEndPos() { + return endPos; + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/SaveItemFile.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/SaveItemFile.java new file mode 100644 index 0000000000..c0dcb62ac3 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/download/SaveItemFile.java @@ -0,0 +1,68 @@ +package com.hoo.download; + +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * function: 写入文件、保存文件 + * + * @author hoojo + * @createDate 2011-9-21 下午05:44:02 + * @file SaveItemFile.java + * @package com.hoo.download + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public class SaveItemFile { + // 存储文件 + private RandomAccessFile itemFile; + + public SaveItemFile() throws IOException { + this("", 0); + } + + /** + * @param name + * 文件路径、名称 + * @param pos + * 写入点位置 position + * @throws IOException + */ + public SaveItemFile(String name, long pos) throws IOException { + itemFile = new RandomAccessFile(name, "rw"); + // 在指定的pos位置开始写入数据 + itemFile.seek(pos); + } + + /** + * function: 同步方法写入文件 + * + * @author hoojo + * @createDate 2011-9-26 下午12:21:22 + * @param buff + * 缓冲数组 + * @param start + * 起始位置 + * @param length + * 长度 + * @return + */ + public synchronized int write(byte[] buff, int start, int length) { + int i = -1; + try { + itemFile.write(buff, start, length); + i = length; + } catch (IOException e) { + e.printStackTrace(); + } + return i; + } + + public void close() throws IOException { + if (itemFile != null) { + itemFile.close(); + } + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/entity/DownloadInfo.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/entity/DownloadInfo.java new file mode 100644 index 0000000000..7ef4ba5477 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/entity/DownloadInfo.java @@ -0,0 +1,125 @@ + +package com.hoo.entity; + +/** + * function: 下载文件信息类 + * + * @author hoojo + * @createDate 2011-9-21 下午05:14:58 + * @file DownloadInfo.java + * @package com.hoo.entity + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public class DownloadInfo { + // 下载文件url + private String url; + // 下载文件名称 + private String fileName; + // 下载文件路径 + private String filePath; + // 分成多少段下载, 每一段用一个线程完成下载 + private int splitter; + + // 下载文件默认保存路径 + private final static String FILE_PATH = "C:/temp"; + // 默认分块数、线程数 + private final static int SPLITTER_NUM = 5; + + public DownloadInfo() { + super(); + } + + /** + * @param url + * 下载地址 + */ + public DownloadInfo(String url) { + this(url, null, null, SPLITTER_NUM); + } + + /** + * @param url + * 下载地址url + * @param splitter + * 分成多少段或是多少个线程下载 + */ + public DownloadInfo(String url, int splitter) { + this(url, null, null, splitter); + } + + /*** + * @param url + * 下载地址 + * @param fileName + * 文件名称 + * @param filePath + * 文件保存路径 + * @param splitter + * 分成多少段或是多少个线程下载 + */ + public DownloadInfo(String url, String fileName, String filePath, int splitter) { + super(); + if (url == null || "".equals(url)) { + throw new RuntimeException("url is not null!"); + } + this.url = url; + this.fileName = (fileName == null || "".equals(fileName)) ? getFileName(url) : fileName; + this.filePath = (filePath == null || "".equals(filePath)) ? FILE_PATH : filePath; + this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter; + } + + /** + * function: 通过url获得文件名称 + * + * @author hoojo + * @createDate 2011-9-30 下午05:00:00 + * @param url + * @return + */ + private String getFileName(String url) { + return url.substring(url.lastIndexOf("/") + 1, url.length()); + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + if (url == null || "".equals(url)) { + throw new RuntimeException("url is not null!"); + } + this.url = url; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = (fileName == null || "".equals(fileName)) ? getFileName(url) : fileName; + } + + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = (filePath == null || "".equals(filePath)) ? FILE_PATH : filePath; + } + + public int getSplitter() { + return splitter; + } + + public void setSplitter(int splitter) { + this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter; + } + + @Override + public String toString() { + return this.url + "#" + this.fileName + "#" + this.filePath + "#" + this.splitter; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/DownloadUtils.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/DownloadUtils.java new file mode 100644 index 0000000000..3ca0384319 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/DownloadUtils.java @@ -0,0 +1,40 @@ +package com.hoo.util; + +import com.hoo.download.BatchDownloadFile; +import com.hoo.entity.DownloadInfo; + +/** + * function: 分块多线程下载工具类 + * + * @author hoojo + * @createDate 2011-9-28 下午05:22:18 + * @file DownloadUtils.java + * @package com.hoo.util + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public abstract class DownloadUtils { + + public static void download(String url) { + DownloadInfo bean = new DownloadInfo(url); + LogUtils.info(bean); + BatchDownloadFile down = new BatchDownloadFile(bean); + new Thread(down).start(); + } + + public static void download(String url, int threadNum) { + DownloadInfo bean = new DownloadInfo(url, threadNum); + LogUtils.info(bean); + BatchDownloadFile down = new BatchDownloadFile(bean); + new Thread(down).start(); + } + + public static void download(String url, String fileName, String filePath, int threadNum) { + DownloadInfo bean = new DownloadInfo(url, fileName, filePath, threadNum); + LogUtils.info(bean); + BatchDownloadFile down = new BatchDownloadFile(bean); + new Thread(down).start(); + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/DownloadUtilsTest.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/DownloadUtilsTest.java new file mode 100644 index 0000000000..562a0ec047 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/DownloadUtilsTest.java @@ -0,0 +1,39 @@ +/** + * copy from http://blog.csdn.net/ibm_hoojo/article/details/6838222 + */ +package com.hoo.util; + +/** + * function: 下载测试 + * + * @author hoojo + * @createDate 2011-9-23 下午05:49:46 + * @file TestDownloadMain.java + * @package com.hoo.download + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public class DownloadUtilsTest { + public static void main(String[] args) { + /* + * DownloadInfo bean = new DownloadInfo( + * "http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg" + * ); System.out.println(bean); BatchDownloadFile down = new + * BatchDownloadFile(bean); new Thread(down).start(); + */ + + // DownloadUtils.download("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg"); + DownloadUtils.download("https://github.com/dracome/coding2017/archive/master.zip", 5); + + try { + Thread.sleep(30 * 1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + int i = 3; + System.out.println(i); + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/LogUtils.java b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/LogUtils.java new file mode 100644 index 0000000000..62d48bd0f9 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-11-challenge/src/main/java/com/hoo/util/LogUtils.java @@ -0,0 +1,40 @@ +package com.hoo.util; + +/** + * function: 日志工具类 + * + * @author hoojo + * @createDate 2011-9-21 下午05:21:27 + * @file LogUtils.java + * @package com.hoo.util + * @project MultiThreadDownLoad + * @blog http://blog.csdn.net/IBM_hoojo + * @email hoojo_@126.com + * @version 1.0 + */ +public abstract class LogUtils { + + public static void log(Object message) { + System.err.println(message); + } + + public static void log(String message) { + System.err.println(message); + } + + public static void log(int message) { + System.err.println(message); + } + + public static void info(Object message) { + System.out.println(message); + } + + public static void info(String message) { + System.out.println(message); + } + + public static void info(int message) { + System.out.println(message); + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/Connection.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..76dc0f3a40 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,28 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * + * @param startPos + * 开始位置, 从0开始 + * @param endPos + * 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/ConnectionException.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/ConnectionManager.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..787984f170 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,11 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/DownloadListener.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/core/DownloadThread.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/core/DownloadThread.java new file mode 100644 index 0000000000..ba94bee146 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/core/DownloadThread.java @@ -0,0 +1,21 @@ +package com.coderising.download.core; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread { + + Connection conn; + int startPos; + int endPos; + + public DownloadThread(Connection conn, int startPos, int endPos) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + public void run() { + + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/core/FileDownloader.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/core/FileDownloader.java new file mode 100644 index 0000000000..23ee19ab02 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/core/FileDownloader.java @@ -0,0 +1,68 @@ +package com.coderising.download.core; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + // 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn, 0, length - 1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..1831118927 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,26 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection { + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..6585b835c4 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..7347ab0a88 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,19 @@ +package com.coderising.jvm.loader; + +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + return null; + } + + public void addClassPath(String path) { + } + + public String getClassPath() { + return null; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/LoginAction.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5f41f42c62 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/Struts.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..5ad5ccb352 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,30 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + * + */ + + return null; + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/View.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f1e7fcfa19 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/BinaryTreeNode.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..2f944d3b91 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Iterator.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f30dfc8edf --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/List.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..03fa879b2e --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Queue.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b2908512c5 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o) { + } + + public Object deQueue() { + return null; + } + + public boolean isEmpty() { + return false; + } + + public int size() { + return -1; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Stack.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..988b174e55 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + } + + public Object pop() { + return null; + } + + public Object peek() { + return null; + } + + public boolean isEmpty() { + return false; + } + + public int size() { + return -1; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/array/ArrayList.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..81dfe5bdfe --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,36 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + + } + + public void add(int index, Object o) { + + } + + public Object get(int index) { + return null; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return -1; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/array/ArrayUtil.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..0e8e077db7 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + return null; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + return null; + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..f6732b68e8 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,50 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin + */ +public class LRUPageFrame { + private static class Node { + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + + return buffer.toString(); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/linklist/LinkedList.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..6c8b4a3315 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,129 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o) { + + } + + public void add(int index, Object o) { + + } + + public Object get(int index) { + return null; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return -1; + } + + public void addFirst(Object o) { + + } + + public void addLast(Object o) { + + } + + public Object removeFirst() { + return null; + } + + public Object removeLast() { + return null; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 从当前链表中取出那些listB所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在listB中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/resources/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/resources/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/main/resources/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/download/core/FileDownloaderTest.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/download/core/FileDownloaderTest.java new file mode 100644 index 0000000000..8e171cff93 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/download/core/FileDownloaderTest.java @@ -0,0 +1,56 @@ +package com.coderising.download.core; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + // 休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/jvm/loader/ClassFileloaderTest.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/jvm/loader/ClassFileloaderTest.java new file mode 100644 index 0000000000..d43e4e5d54 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/jvm/loader/ClassFileloaderTest.java @@ -0,0 +1,76 @@ +package com.coderising.jvm.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/jvm/loader/EmployeeV1.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..2b80092ecb --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/jvm/loader/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.coderising.jvm.loader; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/litestruts/StrutsTest.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..f2426db6ea --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 密码和预设的不一致 + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coding/basic/linklist/LRUPageFrameTest.java b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..ff765f90b1 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/resources/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/test/resources/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-51-liuxin-question/src/test/resources/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/basic/LinkedList.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/basic/LinkedList.java new file mode 100644 index 0000000000..5894d89630 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/basic/LinkedList.java @@ -0,0 +1,449 @@ +package com.coderising.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.Stack; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + size = 0; + head = null; + } + + public void add(Object o) { + Node node = new Node(o); + if (head == null) { + head = node; + } else { + // p为游标 从头遍历到尾 + Node p = head; + while (p.next != null) { + p = p.next; + } + p.next = node; + } + size++; + } + + public void add(int index, Object o) { + // 判断不为空链表 + if (head != null) { + Node p = head; + int k = 0; + // 扫描单链表查找第index-1个节点 + while (k < index - 1 && p.next != null) { + k++; + p = p.next; + } + // 判断是否找到第index-1个节点 + if (p != null) { + Node node = new Node(o); + node.next = p.next; + p.next = node; + } + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } else { + Node p = head; + int k = 0; + while (k < index && p.next != null) { + k++; + p = p.next; + } + return p.data; + } + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + if (head == null) { + return null; + } + if (index == 0) { + head = head.next; + size--; + return head.data; + } else { + if (head != null) { + int k = 0; + Node p = head; + while (k < index - 1 && p != null) { + k++; + p = p.next; + } + Node pn = p.next; + if (pn != null) { + p.next = pn.next; + size--; + return pn.data; + } + } + } + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + + public void addLast(Object o) { + Node node = new Node(o); + if (head == null) { + head = node; + } else { + Node p = head; + while (p.next != null) { + p = p.next; + } + p.next = node; + } + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node node = head; + head = node.next; + size--; + return node.data; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } else { + Node p = head; + int k = 0; + while (k < size - 1 && p.next != null) { + k++; + p = p.next; + } + Node last = p.next; + p.next = null; + size--; + return last.data; + } + } + + private static class Node { + Object data; + Node next; + + private Node(Object o) { + this.data = o; + this.next = null; + } + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + if (null == head || null == head.next) { + return; + } + Stack s = new Stack(); + + Node currentNode = head; + while (currentNode != null) { + + s.push(currentNode); + + Node nextNode = currentNode.next; + currentNode.next = null; // 把链接断开 + currentNode = nextNode; + } + + head = s.pop(); + + currentNode = head; + while (!s.isEmpty()) { + Node nextNode = s.pop(); + currentNode.next = nextNode; + currentNode = nextNode; + } + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int num = size / 2; + for (int i = 0; i < num; i++) { + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException(); + } + + int len = size - i >= length ? length : size - i; + + int k = 0; + while (k < len) { + remove(i); + k++; + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + + int[] arr = new int[list.size()]; + + for (int i = 0; i < list.size(); i++) { + arr[i] = (int) this.get((int) list.get(i)); + } + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + for (int i = 0; i < list.size(); i++) { + this.remove(list.get(i)); + } + } + + /** + * 传入数据删除节点 + * + * @param obj + */ + public void remove(Object obj) { + if (head == null) { + throw new RuntimeException("LinkedList is empty!"); + } + // 如果要删除的结点是第一个,则把下一个结点赋值给第一个结点 + if (head.data.equals(obj)) { + head = head.next; + size--; + } else { + Node pre = head; // 上一节点 + Node cur = head.next; // 当前结点 + while (cur != null) { + if (cur.data.equals(obj)) { + pre.next = cur.next; + size--; + } + pre = pre.next; + cur = cur.next; + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if (head == null) { + throw new RuntimeException("LinkedList is empty!"); + } + + Node pre = head; + Node cur = head; + while (cur.next != null) { + cur = cur.next; + Object data = pre.data; + while (cur.data == data) { + if (cur.next == null) { + pre.next = null; + break; + } + pre.next = cur.next; + size--; + cur = cur.next; + if (cur == null) { + break; + } + } + pre = pre.next; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + if (head == null) { + return; + } + + Node node = head; + int start = -1; + int end = -1; + int i = 0; + while (node != null) { + if ((start == -1) && (int) node.data <= min) { + start = i; + } + if ((int) node.data >= max) { + end = i; + break; + } + node = node.next; + i++; + } + + if (start == -1) { + start = 0; + } + if (end == -1) { + end = size; + } + this.remove(start, end - start); + + /* + * if(head == null){ throw new RuntimeException("LinkedList is empty!"); + * }else{ Node q = head; //头判断 if((int)q.data>min && (int)q.datamin && + * (int)p.data totalLen) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + + } + + @Override + public void close() { + + } + +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..5e98063eaa --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/Configuration.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/Configuration.java new file mode 100644 index 0000000000..5b0f60c148 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/Configuration.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +public class Configuration { + + Map actions = new HashMap<>(); + + public Configuration(String fileName) { + + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is) { + + SAXBuilder builder = new SAXBuilder(); + + try { + + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for (Element actionElement : root.getChildren("action")) { + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for (Element resultElement : actionElement.getChildren("result")) { + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + + } + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig { + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + + public String getClassName() { + return clzName; + } + + public void addViewResult(String name, String viewName) { + viewResult.put(name, viewName); + } + + public String getViewName(String resultName) { + return viewResult.get(resultName); + } + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/ConfigurationException.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..97e286827f --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.coderising.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/LoginAction.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5f41f42c62 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/ReflectionUtil.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..1ba13d5245 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/ReflectionUtil.java @@ -0,0 +1,120 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz, "set"); + + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for (String name : params.keySet()) { + + String methodName = "set" + name; + + for (Method m : methods) { + + if (m.getName().equalsIgnoreCase(methodName)) { + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz, "get"); + } + + private static List getMethods(Class clz, String startWithName) { + + List methods = new ArrayList<>(); + + for (Method m : clz.getDeclaredMethods()) { + + if (m.getName().startsWith(startWithName)) { + + methods.add(m); + + } + + } + + return methods; + } + + public static Map getParamterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for (Method m : methods) { + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + + return params; + } + + //////////////////////// Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for (Method m : clz.getDeclaredMethods()) { + + if (m.getName().startsWith("get")) { + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for (Method m : clz.getDeclaredMethods()) { + + if (m.getName().startsWith("set")) { + + methods.add(m); + + } + + } + + return methods; + + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/Struts.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..b3fe556ebc --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,56 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.Map; + +public class Struts { + private final static Configuration cfg = new Configuration("struts.xml"); + + public static View runAction(String actionName, Map parameters) { + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + * + */ + + String clzName = cfg.getClassName(actionName); + + if (clzName == null) { + return null; + } + + try { + Class clz = Class.forName(clzName); + Object action = clz.newInstance(); + + ReflectionUtil.setParameters(action, parameters); + + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String) m.invoke(action); + + Map params = ReflectionUtil.getParamterMap(action); + String resultView = cfg.getResultView(actionName, resultName); + View view = new View(); + view.setParameters(params); + view.setJsp(resultView); + return view; + + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/View.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f1e7fcfa19 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/resources/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/main/resources/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/basic/LinkedListTest.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/basic/LinkedListTest.java new file mode 100644 index 0000000000..953a9a215b --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/basic/LinkedListTest.java @@ -0,0 +1,197 @@ +package com.coderising.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverse() { + LinkedList l = new LinkedList(); + + Assert.assertEquals("[]", l.toString()); + + l.add(1); + l.reverse(); + Assert.assertEquals("[1]", l.toString()); + + l.add(2); + l.add(3); + l.add(4); + + l.reverse(); + Assert.assertEquals("[4,3,2,1]", l.toString()); + } + + @Test + public void testRemoveFirstHalf() { + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.removeFirstHalf(); + Assert.assertEquals("[3,4]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.removeFirstHalf(); + Assert.assertEquals("[3,4,5]", linkedList.toString()); + } + } + + @Test + public void testRemoveIntInt() { + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(0, 2); + Assert.assertEquals("[3,4]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(3, 2); + Assert.assertEquals("[1,2,3]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(2, 2); + Assert.assertEquals("[1,2]", linkedList.toString()); + } + } + + @Test + public void testGetElements() { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(6); + Assert.assertArrayEquals(new int[] { 101, 301, 401, 601 }, linkedList.getElements(list)); + } + + @Test + public void testSubtract() { + LinkedList list1 = new LinkedList(); + list1.add(101); + list1.add(201); + list1.add(301); + list1.add(401); + list1.add(501); + list1.add(601); + list1.add(701); + + LinkedList list2 = new LinkedList(); + + list2.add(101); + list2.add(201); + list2.add(301); + list2.add(401); + list2.add(501); + + list1.subtract(list2); + + Assert.assertEquals("[601,701]", list1.toString()); + } + + @Test + public void testRemoveDuplicateValues() { + LinkedList list = new LinkedList(); + list.add(1); + list.add(1); + list.add(2); + list.add(2); + list.add(3); + list.add(5); + list.add(5); + list.add(6); + list.removeDuplicateValues(); + + Assert.assertEquals("[1,2,3,5,6]", list.toString()); + } + + @Test + public void testRemoveRange() { + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(16); + linkedList.add(16); + linkedList.add(19); + + linkedList.removeRange(10, 19); + Assert.assertEquals("[19]", linkedList.toString()); + } + + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(16); + linkedList.add(16); + linkedList.add(19); + + linkedList.removeRange(10, 14); + Assert.assertEquals("[14,16,16,19]", linkedList.toString()); + } + } + + @Test + public void testIntersection() { + LinkedList list1 = new LinkedList(); + list1.add(1); + list1.add(6); + list1.add(7); + + LinkedList list2 = new LinkedList(); + list2.add(2); + list2.add(5); + list2.add(6); + + LinkedList newList = list1.intersection(list2); + Assert.assertEquals("[6]", newList.toString()); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/download/api/ConnectionTest.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/download/api/ConnectionTest.java new file mode 100644 index 0000000000..5e4259bddf --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/download/api/ConnectionTest.java @@ -0,0 +1,42 @@ +package com.coderising.download.api; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.impl.ConnectionManagerImpl; + +public class ConnectionTest { + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testContentLength() throws Exception { + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception { + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + Assert.assertEquals(1000, data.length); + + // 测试不充分,没有断言内容是否正确 + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/download/core/FileDownloaderTest.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/download/core/FileDownloaderTest.java new file mode 100644 index 0000000000..2631a1f90b --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/download/core/FileDownloaderTest.java @@ -0,0 +1,56 @@ +package com.coderising.download.core; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + // String url = + // "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + + FileDownloader downloader = new FileDownloader(url, "c:\\coderising\\tmp\\test.jpg"); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + // 休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + System.out.println("下载完成!"); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/ConfigurationTest.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..b8ab6c04b9 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,46 @@ +package com.coderising.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ConfigurationTest { + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView() { + String jsp = cfg.getResultView("login", "success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login", "fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout", "success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout", "error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/ReflectionUtilTest.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..4362ae0ac7 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,104 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ReflectionUtilTest { + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for (Method m : methods) { + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + ReflectionUtil.setParameters(o, params); + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + + @Test + public void testGetGetterMethod() throws Exception { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for (Method m : methods) { + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParamters() throws Exception { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction) clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + Assert.assertEquals(null, params.get("messaage")); + Assert.assertEquals("test", params.get("name")); + Assert.assertEquals("123456", params.get("password")); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/StrutsTest.java b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..5174fc47f1 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 密码和预设的不一致 + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/resources/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-52-liuxin-answer/src/test/resources/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/pom.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/pom.xml deleted file mode 100644 index 90b5cc3818..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - com.github.eulerlcs - jmr-02-parent - 0.0.1-SNAPSHOT - ../jmr-02-parent/pom.xml - - jmr-61-170226-collection - eulerlcs master java road collection - - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-log4j12 - - - junit - junit - - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/java/com/github/eulerlcs/jmr/collection/core/ArrayList.java b/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/java/com/github/eulerlcs/jmr/collection/core/ArrayList.java deleted file mode 100644 index 1c039b8c62..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/java/com/github/eulerlcs/jmr/collection/core/ArrayList.java +++ /dev/null @@ -1,438 +0,0 @@ -/** - * 90% or more copy from jdk - */ -package com.github.eulerlcs.jmr.collection.core; - -import java.util.Arrays; -import java.util.Collection; -import java.util.ConcurrentModificationException; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.NoSuchElementException; -import java.util.Objects; -import java.util.RandomAccess; -import java.util.function.Consumer; - -public class ArrayList implements List, RandomAccess { - private static final int MAX_ARRAY_SIZE = 1 << 10; - private transient Object[] elementData = new Object[0]; - private int size; - private transient int modCount = 0; - - @Override - public int size() { - return size; - } - - @Override - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(Object o) { - if (o == null) { - for (Object obi : elementData) { - if (obi == null) { - return true; - } - } - } else { - for (Object obj : elementData) { - if (o.equals(obj)) { - return true; - } - } - } - return false; - } - - @Override - public boolean containsAll(Collection c) { - for (Object e : c) - if (!contains(e)) - return false; - return true; - } - - @Override - public Object[] toArray() { - return Arrays.copyOf(elementData, size, elementData.getClass()); - } - - @SuppressWarnings("unchecked") - @Override - public T[] toArray(T[] a) { - if (a.length < size) { - return (T[]) Arrays.copyOf(elementData, size, a.getClass()); - } else { - System.arraycopy(elementData, 0, a, 0, size); - if (a.length > size) - a[size] = null; - return a; - } - } - - @Override - public boolean add(E e) { - ensureExplicitCapacity(size + 1); // Increments modCount!! - elementData[size] = e; - size++; - return true; - } - - @Override - public void add(int index, E element) { - if (index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); - ensureExplicitCapacity(size + 1); // Increments modCount!! - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = element; - size++; - } - - @Override - public E remove(int index) { - if (index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); - - modCount++; - @SuppressWarnings("unchecked") - E oldValue = (E) elementData[index]; - int numMoved = size - index - 1; - if (numMoved > 0) - System.arraycopy(elementData, index + 1, elementData, index, numMoved); - elementData[--size] = null;// clear to let GC do its work - - return oldValue; - } - - @Override - public boolean remove(Object o) { - int index = -1; - - if (o == null) { - for (int i = 0; i < size; i++) - if (elementData[i] == null) { - index = i; - break; - } - } else { - for (int i = 0; i < size; i++) - if (o.equals(elementData[i])) { - index = i; - break; - } - } - - if (index > 0) { - modCount++; - int numMoved = size - index - 1; - if (numMoved > 0) - System.arraycopy(elementData, index + 1, elementData, index, numMoved); - elementData[--size] = null;// clear to let GC do its work - - return true; - } - - return false; - } - - @Override - public boolean removeAll(Collection c) { - boolean modified = false; - for (Object obj : c) { - modified |= remove(obj); - } - - return modified; - } - - @Override - public boolean addAll(Collection c) { - Object[] a = c.toArray(); - int numNew = a.length; - ensureExplicitCapacity(size + numNew);// Increments modCount - System.arraycopy(a, 0, elementData, size, numNew); - size += numNew; - return numNew != 0; - } - - @Override - public boolean addAll(int index, Collection c) { - if (index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); - - Object[] a = c.toArray(); - int numNew = a.length; - ensureExplicitCapacity(size + numNew);// Increments modCount - - int numMoved = size - index; - if (numMoved > 0) - System.arraycopy(elementData, index, elementData, index + numNew, numMoved); - - System.arraycopy(a, 0, elementData, index, numNew); - size += numNew; - return numNew != 0; - } - - @Override - public boolean retainAll(Collection c) { - final Object[] elementData = this.elementData; - int r = 0, w = 0; - boolean modified = false; - for (; r < size; r++) - if (c.contains(elementData[r])) - elementData[w++] = elementData[r]; - - if (w != size) { - // clear to let GC do its work - for (int i = w; i < size; i++) - elementData[i] = null; - modCount += size - w; - size = w; - modified = true; - } - - return modified; - } - - @Override - public void clear() { - modCount++; - for (int i = 0; i < size; i++) - elementData[i] = null; - - size = 0; - } - - @Override - public List subList(int fromIndex, int toIndex) { - throw new UnsupportedOperationException(); - } - - @SuppressWarnings("unchecked") - @Override - public E get(int index) { - if (index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); - - return (E) elementData[index]; - } - - @SuppressWarnings("unchecked") - @Override - public E set(int index, E element) { - if (index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); - - E oldValue = (E) elementData[index]; - elementData[index] = element; - return oldValue; - } - - @Override - public int indexOf(Object o) { - if (o == null) { - for (int i = 0; i < size; i++) - if (elementData[i] == null) - return i; - } else { - for (int i = 0; i < size; i++) - if (o.equals(elementData[i])) - return i; - } - - return -1; - } - - @Override - public int lastIndexOf(Object o) { - if (o == null) { - for (int i = size - 1; i >= 0; i--) - if (elementData[i] == null) - return i; - } else { - for (int i = size - 1; i >= 0; i--) - if (o.equals(elementData[i])) - return i; - } - - return -1; - } - - private void ensureExplicitCapacity(int minCapacity) { - modCount++; - - if (elementData.length > minCapacity) { - return; - } else if (minCapacity > MAX_ARRAY_SIZE) { - throw new OutOfMemoryError(); - } - - int oldCapacity = elementData.length; - - int newCapacity = oldCapacity == 0 ? 10 : (oldCapacity + (oldCapacity >> 1)); - if (newCapacity > MAX_ARRAY_SIZE) { - newCapacity = MAX_ARRAY_SIZE; - } - - elementData = Arrays.copyOf(elementData, newCapacity); - } - - @Override - public Iterator iterator() { - return new Itr(); - } - - @Override - public ListIterator listIterator() { - return new ListItr(0); - } - - @Override - public ListIterator listIterator(int index) { - if (index < 0 || index > size) - throw new IndexOutOfBoundsException("Index: " + index); - return new ListItr(index); - } - - /** - * fully copy from jdk ArrayList.Itr - */ - private class Itr implements Iterator { - int cursor; // index of next element to return - int lastRet = -1; // index of last element returned; -1 if no such - int expectedModCount = modCount; - - @Override - public boolean hasNext() { - return cursor != size; - } - - @Override - @SuppressWarnings("unchecked") - public E next() { - checkForComodification(); - int i = cursor; - if (i >= size) - throw new NoSuchElementException(); - Object[] elementData = ArrayList.this.elementData; - if (i >= elementData.length) - throw new ConcurrentModificationException(); - cursor = i + 1; - return (E) elementData[lastRet = i]; - } - - @Override - public void remove() { - if (lastRet < 0) - throw new IllegalStateException(); - checkForComodification(); - - try { - ArrayList.this.remove(lastRet); - cursor = lastRet; - lastRet = -1; - expectedModCount = modCount; - } catch (IndexOutOfBoundsException ex) { - throw new ConcurrentModificationException(); - } - } - - @Override - @SuppressWarnings("unchecked") - public void forEachRemaining(Consumer consumer) { - Objects.requireNonNull(consumer); - final int size = ArrayList.this.size; - int i = cursor; - if (i >= size) { - return; - } - final Object[] elementData = ArrayList.this.elementData; - if (i >= elementData.length) { - throw new ConcurrentModificationException(); - } - while (i != size && modCount == expectedModCount) { - consumer.accept((E) elementData[i++]); - } - // update once at end of iteration to reduce heap write traffic - cursor = i; - lastRet = i - 1; - checkForComodification(); - } - - final void checkForComodification() { - if (modCount != expectedModCount) - throw new ConcurrentModificationException(); - } - } - - /** - * fully copy from jdk ArrayList.ListItr - */ - private class ListItr extends Itr implements ListIterator { - ListItr(int index) { - super(); - cursor = index; - } - - @Override - public boolean hasPrevious() { - return cursor != 0; - } - - @Override - public int nextIndex() { - return cursor; - } - - @Override - public int previousIndex() { - return cursor - 1; - } - - @Override - @SuppressWarnings("unchecked") - public E previous() { - checkForComodification(); - int i = cursor - 1; - if (i < 0) - throw new NoSuchElementException(); - Object[] elementData = ArrayList.this.elementData; - if (i >= elementData.length) - throw new ConcurrentModificationException(); - cursor = i; - return (E) elementData[lastRet = i]; - } - - @Override - public void set(E e) { - if (lastRet < 0) - throw new IllegalStateException(); - checkForComodification(); - - try { - ArrayList.this.set(lastRet, e); - } catch (IndexOutOfBoundsException ex) { - throw new ConcurrentModificationException(); - } - } - - @Override - public void add(E e) { - checkForComodification(); - - try { - int i = cursor; - ArrayList.this.add(i, e); - cursor = i + 1; - lastRet = -1; - expectedModCount = modCount; - } catch (IndexOutOfBoundsException ex) { - throw new ConcurrentModificationException(); - } - } - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/resources/log4j.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/resources/log4j.xml deleted file mode 100644 index 831b8d9ce3..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/main/resources/log4j.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/test/java/com/github/eulerlcs/jmr/collection/core/TestArrayList.java b/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/test/java/com/github/eulerlcs/jmr/collection/core/TestArrayList.java deleted file mode 100644 index 96e1049d73..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170226-collection/src/test/java/com/github/eulerlcs/jmr/collection/core/TestArrayList.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.github.eulerlcs.jmr.collection.core; - -import java.util.List; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.github.eulerlcs.jmr.collection.core.ArrayList; - -public class TestArrayList { - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test_foreach() { - List list = new ArrayList<>(); - list.add(1); - list.add(2); - list.add(3); - - int sum = 0; - for (Integer item : list) { - sum += item; - } - - Assert.assertEquals(sum, 6); - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/data/struts.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/data/struts.xml deleted file mode 100644 index a7a77b73df..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/data/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/pom.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/pom.xml deleted file mode 100644 index 6ce14ac574..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - 4.0.0 - - com.github.eulerlcs - jmr-02-parent - 0.0.1-SNAPSHOT - ../jmr-02-parent/pom.xml - - jmr-61-170305-litestruts - eulerlcs master java road lite struts - - - - commons-digester - commons-digester - - - org.projectlombok - lombok - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-log4j12 - - - junit - junit - - - com.github.stefanbirkner - system-rules - - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/algorithm/ArrayUtil.java b/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/algorithm/ArrayUtil.java deleted file mode 100644 index 36894f6e5b..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/algorithm/ArrayUtil.java +++ /dev/null @@ -1,279 +0,0 @@ -/** - * 问题点: 没写注释,代码比较难读。尤其 merge方法。 - */ -package com.github.eulerlcs.jmr.litestruts.algorithm; - -import java.util.Arrays; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = - * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * - * @param origin - * @return - */ - public static void reverseArray(int[] origin) { - if (origin == null || origin.length < 2) { - return; - } - - for (int head = 0, tail = origin.length - 1; head < tail; head++, tail--) { - origin[head] = origin[head] ^ origin[tail]; - origin[tail] = origin[head] ^ origin[tail]; - origin[head] = origin[head] ^ origin[tail]; - } - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} - * - * @param oldArray - * @return - */ - - public static int[] removeZero(int[] oldArray) { - if (oldArray == null) { - return new int[0]; - } - - int count = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) - count++; - } - - int[] newArray = new int[count]; - int newIndex = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) { - newArray[newIndex] = oldArray[i]; - newIndex++; - } - } - - return newArray; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = - * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * - * @param array1 - * @param array2 - * @return - */ - - public static int[] merge(int[] array1, int[] array2) { - if (array1 == null || array1.length == 0) { - if (array2 == null || array2.length == 0) { - return new int[0]; - } else { - return Arrays.copyOf(array2, array2.length); - } - } else if (array2 == null || array2.length == 0) { - return Arrays.copyOf(array1, array1.length); - } - - int[] result = new int[array1.length + array2.length]; - int idxResult = 0; - int idx1 = 0; - int idx2 = 0; - - for (; idx1 < array1.length; idx1++) { - if (array1[idx1] < array2[idx2]) { - result[idxResult] = array1[idx1]; - idxResult++; - } else if (array1[idx1] == array2[idx2]) { - result[idxResult] = array1[idx1]; - idxResult++; - idx2++; - } else { - for (; idx2 < array2.length; idx2++) { - if (array2[idx2] < array1[idx1]) { - result[idxResult] = array2[idx2]; - idxResult++; - } else { - if (array2[idx2] == array1[idx1]) { - idx2++; - } - - break; - } - } - - if (idx2 == array2.length) { - break; - } else { - idx1--; - } - } - } - - if (idx1 < array1.length) { - System.arraycopy(array1, idx1, result, idxResult, array1.length - idx1); - idxResult += array1.length - idx1; - } - - if (idx2 < array2.length) { - System.arraycopy(array2, idx2, result, idxResult, array2.length - idx2); - idxResult += array2.length - idx2; - } - - result = removeZero(result); - return result; - } - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param increaseCapacity - * @return - */ - public static int[] grow(int[] oldArray, int increaseCapacity) { - if (oldArray == null || increaseCapacity < 0) { - return new int[0]; - } - - int newCapacity = oldArray.length + increaseCapacity; - - int[] newArray = Arrays.copyOf(oldArray, newCapacity); - - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , - * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] - * - * @param max - * @return - */ - public static int[] fibonacci(int max) { - if (max <= 1) { - return new int[0]; - } - - int[] result = new int[10]; - result[0] = 1; - result[1] = 1; - int idx = 2; - int sum = 2; - while (sum < max) { - if (idx >= result.length) { - grow(result, result.length * 2); - } - - result[idx] = sum; - sum = result[idx - 1] + result[idx]; - idx++; - } - - result = removeZero(result); - return result; - } - - /** - * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * - * @param max - * @return - */ - public static int[] getPrimes(int max) { - if (max < 2) { - return new int[0]; - } - - int[] all = new int[max]; - int index = 0; - int temp = 0; - - for (int i = 0; i < max; i++) { - all[i] = i; - } - - all[0] = 0; - all[1] = 0; - index = 2; - - // 筛法 - loops: for (; index < max;) { - for (int i = 2;; i++) { - temp = index * i; - if (temp >= max) { - break; - } - all[temp] = 0; - } - - for (int i = index + 1; i < max; i++) { - if (all[i] != 0) { - index = i; - continue loops; - } - } - - break; - } - - int[] result = removeZero(all); - return result; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * - * @param max - * @return - */ - public static long[] getPerfectNumbers(long max) { - long[] perfect = new long[49];// 到2016年1月为止,共发现了49个完全数 - int idx = 0; - long sum = 1; - long sqrt = 0; - - for (long n = 2; n < max; n++) { - sum = 1; - sqrt = (long) Math.sqrt(n); - for (long i = 2; i <= sqrt; i++) { - if (n % i == 0) - sum += i + n / i; - } - - if (sum == n) { - perfect[idx] = n; - idx++; - } - } - - // return removeZero(perfect); - return perfect; - } - - /** - * 用separator 把数组 array给连接起来 例如array= [3,8,9], separator = "-" 则返回值为"3-8-9" - * - * @param array - * @param separator - * @return - */ - public static String join(int[] array, String separator) { - if (array == null || array.length == 0) { - return ""; - } - - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < array.length - 1; i++) { - sb.append(array[i] + separator); - } - sb.append(String.valueOf(array[array.length - 1])); - - return sb.toString(); - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/resources/log4j.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/resources/log4j.xml deleted file mode 100644 index 831b8d9ce3..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/resources/log4j.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/util/ArrayUtilTest.java b/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/util/ArrayUtilTest.java deleted file mode 100644 index 5e88599842..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/util/ArrayUtilTest.java +++ /dev/null @@ -1,268 +0,0 @@ -/** - * 问题点: 没有全分支覆盖。只简单的测了关键或者关心的case - */ -package com.github.eulerlcs.jmr.litestruts.util; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import com.github.eulerlcs.jmr.litestruts.algorithm.ArrayUtil; - -public class ArrayUtilTest { - - @Test - public void reverseArray_null() { - int[] actuals = null; - int[] expecteds = null; - - ArrayUtil.reverseArray(actuals); - - assertArrayEquals(actuals, expecteds); - } - - @Test - public void reverseArray_0() { - int[] actuals = {}; - int[] expecteds = {}; - - ArrayUtil.reverseArray(actuals); - - assertArrayEquals(actuals, expecteds); - } - - @Test - public void reverseArray_1() { - int[] actuals = { 2 }; - int[] expecteds = { 2 }; - - ArrayUtil.reverseArray(actuals); - - assertArrayEquals(actuals, expecteds); - } - - @Test - public void reverseArray_2() { - int[] actuals = { 7, 9 }; - int[] expecteds = { 9, 7 }; - - ArrayUtil.reverseArray(actuals); - - assertArrayEquals(actuals, expecteds); - } - - @Test - public void reverseArray_4() { - int[] actuals = { 7, 9, 30, 3 }; - int[] expecteds = { 3, 30, 9, 7 }; - - ArrayUtil.reverseArray(actuals); - - assertArrayEquals(actuals, expecteds); - } - - @Test - public void reverseArray_5() { - int[] actuals = { 7, 9, 30, 3, 4 }; - int[] expecteds = { 4, 3, 30, 9, 7 }; - - ArrayUtil.reverseArray(actuals); - - assertArrayEquals(actuals, expecteds); - } - - @Test - public void removeZero_null() { - int oldArr[] = null; - int[] expecteds = {}; - - int[] newArr = ArrayUtil.removeZero(oldArr); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void removeZero_0() { - int oldArr[] = {}; - int[] expecteds = {}; - - int[] newArr = ArrayUtil.removeZero(oldArr); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void removeZero_1() { - int oldArr[] = { 0 }; - int[] expecteds = {}; - - int[] newArr = ArrayUtil.removeZero(oldArr); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void removeZero_2() { - int oldArr[] = { 3, 5 }; - int[] expecteds = { 3, 5 }; - - int[] newArr = ArrayUtil.removeZero(oldArr); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void removeZero_3() { - int oldArr[] = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; - int[] expecteds = { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }; - - int[] newArr = ArrayUtil.removeZero(oldArr); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void merge_1() { - int[] a1 = { 3, 5, 7, 8 }; - int[] a2 = { 4, 5, 6, 7 }; - int[] expecteds = { 3, 4, 5, 6, 7, 8 }; - - int[] newArr = ArrayUtil.merge(a1, a2); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void merge_2() { - int[] a1 = { 4, 5, 6, 7 }; - int[] a2 = { 3, 5, 7, 8 }; - int[] expecteds = { 3, 4, 5, 6, 7, 8 }; - - int[] newArr = ArrayUtil.merge(a1, a2); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void grow_1() { - int[] oldArray = { 2, 3, 6 }; - int increaseCapacity = 3; - int[] expecteds = { 2, 3, 6, 0, 0, 0 }; - - int[] newArr = ArrayUtil.grow(oldArray, increaseCapacity); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void fibonacci_1() { - int max = 1; - int[] expecteds = {}; - - int[] newArr = ArrayUtil.fibonacci(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void fibonacci_2() { - int max = 2; - int[] expecteds = { 1, 1 }; - - int[] newArr = ArrayUtil.fibonacci(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void fibonacci_15() { - int max = 15; - int[] expecteds = { 1, 1, 2, 3, 5, 8, 13 }; - - int[] newArr = ArrayUtil.fibonacci(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPrimes_1() { - int max = 1; - int[] expecteds = {}; - - int[] newArr = ArrayUtil.getPrimes(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPrimes_2() { - int max = 2; - int[] expecteds = {}; - - int[] newArr = ArrayUtil.getPrimes(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPrimes_3() { - int max = 3; - int[] expecteds = { 2 }; - - int[] newArr = ArrayUtil.getPrimes(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPrimes_4() { - int max = 4; - int[] expecteds = { 2, 3 }; - - int[] newArr = ArrayUtil.getPrimes(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPrimes_23() { - int max = 23; - int[] expecteds = { 2, 3, 5, 7, 11, 13, 17, 19 }; - - int[] newArr = ArrayUtil.getPrimes(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPrimes_24() { - int max = 24; - int[] expecteds = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; - - int[] newArr = ArrayUtil.getPrimes(max); - - assertArrayEquals(newArr, expecteds); - } - - @Test - public void getPerfectNumbers_max() { - long max = Long.MAX_VALUE; - max = 10000; - long[] expecteds = { 6, 28, 496, 8128, 33550336, 8589869056L, 137438691328L, 2305843008139952128L }; - - long[] newArr = ArrayUtil.getPerfectNumbers(max); - - assertEquals(newArr[3], expecteds[3]); - } - - @Test - public void join_0() { - int[] array = { 3, 8, 9 }; - String separator = "-"; - String expected = "3-8-9"; - - String actual = ArrayUtil.join(array, separator); - assertEquals(expected, actual); - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/pom.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/pom.xml deleted file mode 100644 index 8f300f8306..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - 4.0.0 - - com.github.eulerlcs - jmr-02-parent - 0.0.1-SNAPSHOT - ../jmr-02-parent/pom.xml - - jmr-61-170312-multiThreadDownload - eulerlcs master java road - download file by multiple thread - - - - commons-digester - commons-digester - - - org.projectlombok - lombok - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-log4j12 - - - junit - junit - - - com.github.stefanbirkner - system-rules - - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/DownloadThread.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/DownloadThread.java deleted file mode 100644 index b3fb699944..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/DownloadThread.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL; - -import com.github.eulerlcs.jmr.multiDL.api.Connection; - -public class DownloadThread extends Thread { - Connection conn; - int startPos; - int endPos; - - public DownloadThread(Connection conn, int startPos, int endPos) { - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - - @Override - public void run() { - - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/FileDownloader.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/FileDownloader.java deleted file mode 100644 index fca00437d7..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/FileDownloader.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL; - -import com.github.eulerlcs.jmr.multiDL.api.Connection; -import com.github.eulerlcs.jmr.multiDL.api.ConnectionException; -import com.github.eulerlcs.jmr.multiDL.api.ConnectionManager; -import com.github.eulerlcs.jmr.multiDL.api.DownloadListener; - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - public FileDownloader(String _url) { - this.url = _url; - } - - public void execute() { - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, - // endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, - // 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; - try { - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn, 0, length - 1).start(); - - } catch (ConnectionException e) { - e.printStackTrace(); - } finally { - if (conn != null) { - conn.close(); - } - } - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - public void setConnectionManager(ConnectionManager ucm) { - this.cm = ucm; - } - - public DownloadListener getListener() { - return this.listener; - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/Iterator.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/Iterator.java deleted file mode 100644 index 2d937d0c53..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.algorithm; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/LinkedList.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/LinkedList.java deleted file mode 100644 index fdae519c0a..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/LinkedList.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.algorithm; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/List.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/List.java deleted file mode 100644 index ad07c355b4..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/algorithm/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.algorithm; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/Connection.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/Connection.java deleted file mode 100644 index d0037a877b..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/Connection.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * - * @param startPos - * 开始位置, 从0开始 - * @param endPos - * 结束位置 - * @return - */ - public byte[] read(int startPos, int endPos) throws IOException; - - /** - * 得到数据内容的长度 - * - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/ConnectionException.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/ConnectionException.java deleted file mode 100644 index 92bf290f8e..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.api; - -public class ConnectionException extends Exception { - private static final long serialVersionUID = 1L; -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/ConnectionManager.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/ConnectionManager.java deleted file mode 100644 index a350054dd4..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/ConnectionManager.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/DownloadListener.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/DownloadListener.java deleted file mode 100644 index a2dc179298..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.api; - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/impl/ConnectionImpl.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/impl/ConnectionImpl.java deleted file mode 100644 index 613c692750..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/impl/ConnectionImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.impl; - -import java.io.IOException; - -import com.github.eulerlcs.jmr.multiDL.api.Connection; - -public class ConnectionImpl implements Connection { - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/impl/ConnectionManagerImpl.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/impl/ConnectionManagerImpl.java deleted file mode 100644 index d50036b8c8..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/java/com/github/eulerlcs/jmr/multiDL/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL.impl; - -import com.github.eulerlcs.jmr.multiDL.api.Connection; -import com.github.eulerlcs.jmr.multiDL.api.ConnectionException; -import com.github.eulerlcs.jmr.multiDL.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/resources/log4j.xml b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/resources/log4j.xml deleted file mode 100644 index 831b8d9ce3..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/main/resources/log4j.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/test/java/com/github/eulerlcs/jmr/multiDL/FileDownloaderTest.java b/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/test/java/com/github/eulerlcs/jmr/multiDL/FileDownloaderTest.java deleted file mode 100644 index cfb3274124..0000000000 --- a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/test/java/com/github/eulerlcs/jmr/multiDL/FileDownloaderTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.github.eulerlcs.jmr.multiDL; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.github.eulerlcs.jmr.multiDL.api.ConnectionManager; -import com.github.eulerlcs.jmr.multiDL.api.DownloadListener; -import com.github.eulerlcs.jmr.multiDL.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - String url = "http://localhost:8080/test.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - }); - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - // 休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - System.out.println("下载完成!"); - } -} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/main/java/com/github/eulerlcs/jmr/algorithm/ArrayList.java b/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/main/java/com/github/eulerlcs/jmr/algorithm/ArrayList.java new file mode 100644 index 0000000000..555f5ea954 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/main/java/com/github/eulerlcs/jmr/algorithm/ArrayList.java @@ -0,0 +1,438 @@ +/** + * 90% or more copy from jdk + */ +package com.github.eulerlcs.jmr.algorithm; + +import java.util.Arrays; +import java.util.Collection; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.RandomAccess; +import java.util.function.Consumer; + +public class ArrayList implements List, RandomAccess { + private static final int MAX_ARRAY_SIZE = 1 << 10; + private transient Object[] elementData = new Object[0]; + private int size; + private transient int modCount = 0; + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + if (o == null) { + for (Object obi : elementData) { + if (obi == null) { + return true; + } + } + } else { + for (Object obj : elementData) { + if (o.equals(obj)) { + return true; + } + } + } + return false; + } + + @Override + public boolean containsAll(Collection c) { + for (Object e : c) + if (!contains(e)) + return false; + return true; + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size, elementData.getClass()); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] a) { + if (a.length < size) { + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + } else { + System.arraycopy(elementData, 0, a, 0, size); + if (a.length > size) + a[size] = null; + return a; + } + } + + @Override + public boolean add(E e) { + ensureExplicitCapacity(size + 1); // Increments modCount!! + elementData[size] = e; + size++; + return true; + } + + @Override + public void add(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + ensureExplicitCapacity(size + 1); // Increments modCount!! + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + @Override + public E remove(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + modCount++; + @SuppressWarnings("unchecked") + E oldValue = (E) elementData[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null;// clear to let GC do its work + + return oldValue; + } + + @Override + public boolean remove(Object o) { + int index = -1; + + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) { + index = i; + break; + } + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) { + index = i; + break; + } + } + + if (index > 0) { + modCount++; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null;// clear to let GC do its work + + return true; + } + + return false; + } + + @Override + public boolean removeAll(Collection c) { + boolean modified = false; + for (Object obj : c) { + modified |= remove(obj); + } + + return modified; + } + + @Override + public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + System.arraycopy(a, 0, elementData, size, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean addAll(int index, Collection c) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + + int numMoved = size - index; + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, index + numNew, numMoved); + + System.arraycopy(a, 0, elementData, index, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean retainAll(Collection c) { + final Object[] elementData = this.elementData; + int r = 0, w = 0; + boolean modified = false; + for (; r < size; r++) + if (c.contains(elementData[r])) + elementData[w++] = elementData[r]; + + if (w != size) { + // clear to let GC do its work + for (int i = w; i < size; i++) + elementData[i] = null; + modCount += size - w; + size = w; + modified = true; + } + + return modified; + } + + @Override + public void clear() { + modCount++; + for (int i = 0; i < size; i++) + elementData[i] = null; + + size = 0; + } + + @Override + public List subList(int fromIndex, int toIndex) { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + return (E) elementData[index]; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + E oldValue = (E) elementData[index]; + elementData[index] = element; + return oldValue; + } + + @Override + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) + return i; + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + @Override + public int lastIndexOf(Object o) { + if (o == null) { + for (int i = size - 1; i >= 0; i--) + if (elementData[i] == null) + return i; + } else { + for (int i = size - 1; i >= 0; i--) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + private void ensureExplicitCapacity(int minCapacity) { + modCount++; + + if (elementData.length > minCapacity) { + return; + } else if (minCapacity > MAX_ARRAY_SIZE) { + throw new OutOfMemoryError(); + } + + int oldCapacity = elementData.length; + + int newCapacity = oldCapacity == 0 ? 10 : (oldCapacity + (oldCapacity >> 1)); + if (newCapacity > MAX_ARRAY_SIZE) { + newCapacity = MAX_ARRAY_SIZE; + } + + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public ListIterator listIterator() { + return new ListItr(0); + } + + @Override + public ListIterator listIterator(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index); + return new ListItr(index); + } + + /** + * fully copy from jdk ArrayList.Itr + */ + private class Itr implements Iterator { + int cursor; // index of next element to return + int lastRet = -1; // index of last element returned; -1 if no such + int expectedModCount = modCount; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + checkForComodification(); + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + @SuppressWarnings("unchecked") + public void forEachRemaining(Consumer consumer) { + Objects.requireNonNull(consumer); + final int size = ArrayList.this.size; + int i = cursor; + if (i >= size) { + return; + } + final Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) { + throw new ConcurrentModificationException(); + } + while (i != size && modCount == expectedModCount) { + consumer.accept((E) elementData[i++]); + } + // update once at end of iteration to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); + } + + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + /** + * fully copy from jdk ArrayList.ListItr + */ + private class ListItr extends Itr implements ListIterator { + ListItr(int index) { + super(); + cursor = index; + } + + @Override + public boolean hasPrevious() { + return cursor != 0; + } + + @Override + public int nextIndex() { + return cursor; + } + + @Override + public int previousIndex() { + return cursor - 1; + } + + @Override + @SuppressWarnings("unchecked") + public E previous() { + checkForComodification(); + int i = cursor - 1; + if (i < 0) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i; + return (E) elementData[lastRet = i]; + } + + @Override + public void set(E e) { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.set(lastRet, e); + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + public void add(E e) { + checkForComodification(); + + try { + int i = cursor; + ArrayList.this.add(i, e); + cursor = i + 1; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/main/resources/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/resources/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-61-collection/src/main/resources/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/test/java/com/github/eulerlcs/jmr/algorithm/TestArrayList.java b/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/test/java/com/github/eulerlcs/jmr/algorithm/TestArrayList.java new file mode 100644 index 0000000000..47ed2e0bef --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/test/java/com/github/eulerlcs/jmr/algorithm/TestArrayList.java @@ -0,0 +1,44 @@ +package com.github.eulerlcs.jmr.algorithm; + +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestArrayList { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test_foreach() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + + int sum = 0; + for (Integer item : list) { + sum += item; + } + + Assert.assertEquals(sum, 6); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/data/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-61-collection/src/test/resources/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/data/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-61-collection/src/test/resources/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/.gitkeep similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170312-multiThreadDownload/src/test/resources/.gitkeep rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/.gitkeep diff --git a/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/algorithm/ArrayUtil.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/algorithm/ArrayUtil.java new file mode 100644 index 0000000000..c9cc7522b5 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/algorithm/ArrayUtil.java @@ -0,0 +1,279 @@ +/** + * 问题点: 没写注释,代码比较难读。尤其 merge方法。 + */ +package com.github.eulerlcs.jmr.algorithm; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + if (origin == null || origin.length < 2) { + return; + } + + for (int head = 0, tail = origin.length - 1; head < tail; head++, tail--) { + origin[head] = origin[head] ^ origin[tail]; + origin[tail] = origin[head] ^ origin[tail]; + origin[head] = origin[head] ^ origin[tail]; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + if (oldArray == null) { + return new int[0]; + } + + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) + count++; + } + + int[] newArray = new int[count]; + int newIndex = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[newIndex] = oldArray[i]; + newIndex++; + } + } + + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + if (array1 == null || array1.length == 0) { + if (array2 == null || array2.length == 0) { + return new int[0]; + } else { + return Arrays.copyOf(array2, array2.length); + } + } else if (array2 == null || array2.length == 0) { + return Arrays.copyOf(array1, array1.length); + } + + int[] result = new int[array1.length + array2.length]; + int idxResult = 0; + int idx1 = 0; + int idx2 = 0; + + for (; idx1 < array1.length; idx1++) { + if (array1[idx1] < array2[idx2]) { + result[idxResult] = array1[idx1]; + idxResult++; + } else if (array1[idx1] == array2[idx2]) { + result[idxResult] = array1[idx1]; + idxResult++; + idx2++; + } else { + for (; idx2 < array2.length; idx2++) { + if (array2[idx2] < array1[idx1]) { + result[idxResult] = array2[idx2]; + idxResult++; + } else { + if (array2[idx2] == array1[idx1]) { + idx2++; + } + + break; + } + } + + if (idx2 == array2.length) { + break; + } else { + idx1--; + } + } + } + + if (idx1 < array1.length) { + System.arraycopy(array1, idx1, result, idxResult, array1.length - idx1); + idxResult += array1.length - idx1; + } + + if (idx2 < array2.length) { + System.arraycopy(array2, idx2, result, idxResult, array2.length - idx2); + idxResult += array2.length - idx2; + } + + result = removeZero(result); + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param increaseCapacity + * @return + */ + public static int[] grow(int[] oldArray, int increaseCapacity) { + if (oldArray == null || increaseCapacity < 0) { + return new int[0]; + } + + int newCapacity = oldArray.length + increaseCapacity; + + int[] newArray = Arrays.copyOf(oldArray, newCapacity); + + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + + int[] result = new int[10]; + result[0] = 1; + result[1] = 1; + int idx = 2; + int sum = 2; + while (sum < max) { + if (idx >= result.length) { + grow(result, result.length * 2); + } + + result[idx] = sum; + sum = result[idx - 1] + result[idx]; + idx++; + } + + result = removeZero(result); + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return new int[0]; + } + + int[] all = new int[max]; + int index = 0; + int temp = 0; + + for (int i = 0; i < max; i++) { + all[i] = i; + } + + all[0] = 0; + all[1] = 0; + index = 2; + + // 筛法 + loops: for (; index < max;) { + for (int i = 2;; i++) { + temp = index * i; + if (temp >= max) { + break; + } + all[temp] = 0; + } + + for (int i = index + 1; i < max; i++) { + if (all[i] != 0) { + index = i; + continue loops; + } + } + + break; + } + + int[] result = removeZero(all); + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static long[] getPerfectNumbers(long max) { + long[] perfect = new long[49];// 到2016年1月为止,共发现了49个完全数 + int idx = 0; + long sum = 1; + long sqrt = 0; + + for (long n = 2; n < max; n++) { + sum = 1; + sqrt = (long) Math.sqrt(n); + for (long i = 2; i <= sqrt; i++) { + if (n % i == 0) + sum += i + n / i; + } + + if (sum == n) { + perfect[idx] = n; + idx++; + } + } + + // return removeZero(perfect); + return perfect; + } + + /** + * 用separator 把数组 array给连接起来 例如array= [3,8,9], separator = "-" 则返回值为"3-8-9" + * + * @param array + * @param separator + * @return + */ + public static String join(int[] array, String separator) { + if (array == null || array.length == 0) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < array.length - 1; i++) { + sb.append(array[i] + separator); + } + sb.append(String.valueOf(array[array.length - 1])); + + return sb.toString(); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LoginAction.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LoginAction.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LoginAction.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LoginAction.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LogoutAction.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LogoutAction.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LogoutAction.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/action/LogoutAction.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/Struts.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/Struts.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/Struts.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/Struts.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/View.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/View.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/View.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/core/View.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsAction.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsAction.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsAction.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsAction.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionResult.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionResult.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionResult.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionResult.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionRulerSet.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionRulerSet.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionRulerSet.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsActionRulerSet.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsConfig.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsConfig.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsConfig.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsConfig.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsDigester.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsDigester.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsDigester.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/main/java/com/github/eulerlcs/jmr/litestruts/degister/StrutsDigester.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/com/github/eulerlcs/jmr/algorithm/ArrayUtilTest.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/com/github/eulerlcs/jmr/algorithm/ArrayUtilTest.java new file mode 100644 index 0000000000..7242407f74 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/com/github/eulerlcs/jmr/algorithm/ArrayUtilTest.java @@ -0,0 +1,266 @@ +/** + * 问题点: 没有全分支覆盖。只简单的测了关键或者关心的case + */ +package com.github.eulerlcs.jmr.algorithm; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void reverseArray_null() { + int[] actuals = null; + int[] expecteds = null; + + ArrayUtil.reverseArray(actuals); + + assertArrayEquals(actuals, expecteds); + } + + @Test + public void reverseArray_0() { + int[] actuals = {}; + int[] expecteds = {}; + + ArrayUtil.reverseArray(actuals); + + assertArrayEquals(actuals, expecteds); + } + + @Test + public void reverseArray_1() { + int[] actuals = { 2 }; + int[] expecteds = { 2 }; + + ArrayUtil.reverseArray(actuals); + + assertArrayEquals(actuals, expecteds); + } + + @Test + public void reverseArray_2() { + int[] actuals = { 7, 9 }; + int[] expecteds = { 9, 7 }; + + ArrayUtil.reverseArray(actuals); + + assertArrayEquals(actuals, expecteds); + } + + @Test + public void reverseArray_4() { + int[] actuals = { 7, 9, 30, 3 }; + int[] expecteds = { 3, 30, 9, 7 }; + + ArrayUtil.reverseArray(actuals); + + assertArrayEquals(actuals, expecteds); + } + + @Test + public void reverseArray_5() { + int[] actuals = { 7, 9, 30, 3, 4 }; + int[] expecteds = { 4, 3, 30, 9, 7 }; + + ArrayUtil.reverseArray(actuals); + + assertArrayEquals(actuals, expecteds); + } + + @Test + public void removeZero_null() { + int oldArr[] = null; + int[] expecteds = {}; + + int[] newArr = ArrayUtil.removeZero(oldArr); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void removeZero_0() { + int oldArr[] = {}; + int[] expecteds = {}; + + int[] newArr = ArrayUtil.removeZero(oldArr); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void removeZero_1() { + int oldArr[] = { 0 }; + int[] expecteds = {}; + + int[] newArr = ArrayUtil.removeZero(oldArr); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void removeZero_2() { + int oldArr[] = { 3, 5 }; + int[] expecteds = { 3, 5 }; + + int[] newArr = ArrayUtil.removeZero(oldArr); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void removeZero_3() { + int oldArr[] = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; + int[] expecteds = { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }; + + int[] newArr = ArrayUtil.removeZero(oldArr); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void merge_1() { + int[] a1 = { 3, 5, 7, 8 }; + int[] a2 = { 4, 5, 6, 7 }; + int[] expecteds = { 3, 4, 5, 6, 7, 8 }; + + int[] newArr = ArrayUtil.merge(a1, a2); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void merge_2() { + int[] a1 = { 4, 5, 6, 7 }; + int[] a2 = { 3, 5, 7, 8 }; + int[] expecteds = { 3, 4, 5, 6, 7, 8 }; + + int[] newArr = ArrayUtil.merge(a1, a2); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void grow_1() { + int[] oldArray = { 2, 3, 6 }; + int increaseCapacity = 3; + int[] expecteds = { 2, 3, 6, 0, 0, 0 }; + + int[] newArr = ArrayUtil.grow(oldArray, increaseCapacity); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void fibonacci_1() { + int max = 1; + int[] expecteds = {}; + + int[] newArr = ArrayUtil.fibonacci(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void fibonacci_2() { + int max = 2; + int[] expecteds = { 1, 1 }; + + int[] newArr = ArrayUtil.fibonacci(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void fibonacci_15() { + int max = 15; + int[] expecteds = { 1, 1, 2, 3, 5, 8, 13 }; + + int[] newArr = ArrayUtil.fibonacci(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPrimes_1() { + int max = 1; + int[] expecteds = {}; + + int[] newArr = ArrayUtil.getPrimes(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPrimes_2() { + int max = 2; + int[] expecteds = {}; + + int[] newArr = ArrayUtil.getPrimes(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPrimes_3() { + int max = 3; + int[] expecteds = { 2 }; + + int[] newArr = ArrayUtil.getPrimes(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPrimes_4() { + int max = 4; + int[] expecteds = { 2, 3 }; + + int[] newArr = ArrayUtil.getPrimes(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPrimes_23() { + int max = 23; + int[] expecteds = { 2, 3, 5, 7, 11, 13, 17, 19 }; + + int[] newArr = ArrayUtil.getPrimes(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPrimes_24() { + int max = 24; + int[] expecteds = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; + + int[] newArr = ArrayUtil.getPrimes(max); + + assertArrayEquals(newArr, expecteds); + } + + @Test + public void getPerfectNumbers_max() { + long max = Long.MAX_VALUE; + max = 10000; + long[] expecteds = { 6, 28, 496, 8128, 33550336, 8589869056L, 137438691328L, 2305843008139952128L }; + + long[] newArr = ArrayUtil.getPerfectNumbers(max); + + assertEquals(newArr[3], expecteds[3]); + } + + @Test + public void join_0() { + int[] array = { 3, 8, 9 }; + String separator = "-"; + String expected = "3-8-9"; + + String actual = ArrayUtil.join(array, separator); + assertEquals(expected, actual); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/core/StrutsTest.java b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/core/StrutsTest.java similarity index 100% rename from group09/41689722.eulerlcs/2.code/jmr-61-170305-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/core/StrutsTest.java rename to group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/java/com/github/eulerlcs/jmr/litestruts/core/StrutsTest.java diff --git a/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-62-litestruts/src/test/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/data/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-63-download/data/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/Iterator.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/Iterator.java new file mode 100644 index 0000000000..78d537e842 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/Iterator.java @@ -0,0 +1,8 @@ +package com.github.eulerlcs.jmr.algorithm; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/LinkedList.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/LinkedList.java new file mode 100644 index 0000000000..7f42cc502b --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/LinkedList.java @@ -0,0 +1,126 @@ +package com.github.eulerlcs.jmr.algorithm; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o) { + + } + + public void add(int index, Object o) { + + } + + public Object get(int index) { + return null; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return -1; + } + + public void addFirst(Object o) { + + } + + public void addLast(Object o) { + + } + + public Object removeFirst() { + return null; + } + + public Object removeLast() { + return null; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public static int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/List.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/List.java new file mode 100644 index 0000000000..d693a0895d --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/algorithm/List.java @@ -0,0 +1,13 @@ +package com.github.eulerlcs.jmr.algorithm; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/Connection.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/Connection.java new file mode 100644 index 0000000000..30042c8db0 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/Connection.java @@ -0,0 +1,28 @@ +package com.github.eulerlcs.jmr.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * + * @param startPos + * 开始位置, 从0开始 + * @param endPos + * 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/ConnectionException.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/ConnectionException.java new file mode 100644 index 0000000000..2ba4d3978c --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.github.eulerlcs.jmr.download.api; + +public class ConnectionException extends Exception { + private static final long serialVersionUID = 1L; +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/ConnectionManager.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/ConnectionManager.java new file mode 100644 index 0000000000..e2faed7df6 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/ConnectionManager.java @@ -0,0 +1,11 @@ +package com.github.eulerlcs.jmr.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/DownloadListener.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/DownloadListener.java new file mode 100644 index 0000000000..80400ab21b --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.github.eulerlcs.jmr.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/core/DownloadThread.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/core/DownloadThread.java new file mode 100644 index 0000000000..179a037a92 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/core/DownloadThread.java @@ -0,0 +1,20 @@ +package com.github.eulerlcs.jmr.download.core; + +import com.github.eulerlcs.jmr.download.api.Connection; + +public class DownloadThread extends Thread { + Connection conn; + int startPos; + int endPos; + + public DownloadThread(Connection conn, int startPos, int endPos) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + @Override + public void run() { + + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/core/FileDownloader.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/core/FileDownloader.java new file mode 100644 index 0000000000..fa3c193960 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/core/FileDownloader.java @@ -0,0 +1,64 @@ +package com.github.eulerlcs.jmr.download.core; + +import com.github.eulerlcs.jmr.download.api.Connection; +import com.github.eulerlcs.jmr.download.api.ConnectionException; +import com.github.eulerlcs.jmr.download.api.ConnectionManager; +import com.github.eulerlcs.jmr.download.api.DownloadListener; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + public FileDownloader(String _url) { + this.url = _url; + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + // 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn, 0, length - 1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/impl/ConnectionImpl.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..72b679702b --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/impl/ConnectionImpl.java @@ -0,0 +1,25 @@ +package com.github.eulerlcs.jmr.download.impl; + +import java.io.IOException; + +import com.github.eulerlcs.jmr.download.api.Connection; + +public class ConnectionImpl implements Connection { + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/impl/ConnectionManagerImpl.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..b24ae09984 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/main/java/com/github/eulerlcs/jmr/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.github.eulerlcs.jmr.download.impl; + +import com.github.eulerlcs.jmr.download.api.Connection; +import com.github.eulerlcs.jmr.download.api.ConnectionException; +import com.github.eulerlcs.jmr.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/test/java/com/github/eulerlcs/jmr/download/core/FileDownloaderTest.java b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/test/java/com/github/eulerlcs/jmr/download/core/FileDownloaderTest.java new file mode 100644 index 0000000000..531601606e --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/test/java/com/github/eulerlcs/jmr/download/core/FileDownloaderTest.java @@ -0,0 +1,53 @@ +package com.github.eulerlcs.jmr.download.core; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.eulerlcs.jmr.download.api.ConnectionManager; +import com.github.eulerlcs.jmr.download.api.DownloadListener; +import com.github.eulerlcs.jmr.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + // 休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + System.out.println("下载完成!"); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-63-download/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-63-download/src/test/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/data/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/data/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/main/java/com/github/eulerlcs/jmr/algorithm/LRUPageFrame.java b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/main/java/com/github/eulerlcs/jmr/algorithm/LRUPageFrame.java new file mode 100644 index 0000000000..25268be2dc --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/main/java/com/github/eulerlcs/jmr/algorithm/LRUPageFrame.java @@ -0,0 +1,110 @@ +package com.github.eulerlcs.jmr.algorithm; + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin, eulerlcs + */ +public class LRUPageFrame { + private static class Node { + Node prev; + Node next; + int pageNum; + } + + private int capacity; + private int length = 0; + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param pageNum + */ + public void access(int pageNum) { + Node node = findNode(pageNum); + + if (node != null) { + moveToFirst(node); + } else { + node = new Node(); + node.pageNum = pageNum; + addToFirst(node); + } + } + + private Node findNode(int pageNum) { + Node node = first; + + while (node != null) { + if (node.pageNum == pageNum) { + return node; + } else { + node = node.next; + } + } + + return null; + } + + private void moveToFirst(Node node) { + if (node == first) { + return; + } else if (node == last) { + last = node.prev; + } + + if (node.prev != null) { + node.prev.next = node.next; + } + if (node.next != null) { + node.next.prev = node.prev; + } + + first.prev = node; + node.prev = null; + node.next = first; + + first = node; + } + + private void addToFirst(Node node) { + if (first == null) { + first = node; + last = first; + } else { + first.prev = node; + node.next = first; + first = node; + } + + length++; + if (length > capacity) { + last.prev.next = null; + last = last.prev; + + length = capacity; + } + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + + return buffer.toString(); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/main/java/com/github/eulerlcs/jmr/jvm/loader/ClassFileLoader.java b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/main/java/com/github/eulerlcs/jmr/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..6ef696c8b8 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/main/java/com/github/eulerlcs/jmr/jvm/loader/ClassFileLoader.java @@ -0,0 +1,66 @@ +package com.github.eulerlcs.jmr.jvm.loader; + +import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClassFileLoader { + private final static Logger log = LoggerFactory.getLogger(ClassFileLoader.class); + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + File file = findClassFile(className); + if (file == null) { + return new byte[0]; + } + + byte[] ret = null; + byte[] bytes = new byte[(int) file.length()]; + try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) { + dis.readFully(bytes); + ret = bytes; + } catch (IOException e) { + log.error("ClassFileLoader read error!", e); + } + + return ret; + } + + private File findClassFile(String className) { + String sub = className.replace(".", File.separator) + ".class"; + for (String clzPath : clzPaths) { + File file = new File(clzPath, sub); + if (file.exists()) { + return file; + } + } + + return null; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + if (clzPaths.size() == 0) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + for (String clzPath : clzPaths) { + sb.append(";"); + sb.append(clzPath); + } + + String cat = sb.toString(); + return cat.length() > 0 ? cat.substring(1) : ""; + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/algorithm/LRUPageFrameTest.java b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/algorithm/LRUPageFrameTest.java new file mode 100644 index 0000000000..debc4d7eb6 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/algorithm/LRUPageFrameTest.java @@ -0,0 +1,27 @@ +package com.github.eulerlcs.jmr.algorithm; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/jvm/loader/ClassFileloaderTest.java b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/jvm/loader/ClassFileloaderTest.java new file mode 100644 index 0000000000..b039c5f259 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/jvm/loader/ClassFileloaderTest.java @@ -0,0 +1,53 @@ +package com.github.eulerlcs.jmr.jvm.loader; + +import java.io.File; + +import javax.xml.bind.DatatypeConverter; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + private static String userDir = System.getProperty("user.dir"); + private static String path1 = "C:\temp"; + private static String path2 = userDir + File.separator + "target" + File.separator + "test-classes"; + private static String className = EmployeeV1.class.getName(); + private ClassFileLoader loader = null; + + @Before + public void setUp() throws Exception { + loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + } + + @After + public void tearDown() throws Exception { + loader = null; + } + + @Test + public void testClassPath() { + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1 + ";" + path2, clzPath); + } + + @Test + public void testClassFileLength() { + byte[] byteCodes = loader.readBinaryCode(className); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1078, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + String acctualValue = DatatypeConverter.printHexBinary(codes); + + Assert.assertEquals("CAFEBABE", acctualValue); + } +} diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/jvm/loader/EmployeeV1.java b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..070ad19083 --- /dev/null +++ b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/java/com/github/eulerlcs/jmr/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.github.eulerlcs.jmr.jvm.loader; + +public class EmployeeV1 { + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + } +} \ No newline at end of file diff --git a/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/2.code/jmr-64-minijvm/src/test/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/5.settingfile/eclipsev45.epf b/group09/41689722.eulerlcs/5.settingfile/eclipsev45.epf new file mode 100644 index 0000000000..ca88d61e06 --- /dev/null +++ b/group09/41689722.eulerlcs/5.settingfile/eclipsev45.epf @@ -0,0 +1,186 @@ +#Sat Mar 11 11:44:44 JST 2017 +\!/= +/configuration/org.eclipse.core.net/org.eclipse.core.net.hasMigrated=true +/configuration/org.eclipse.ui.ide/MAX_RECENT_WORKSPACES=10 +/configuration/org.eclipse.ui.ide/RECENT_WORKSPACES=E\:\\10.github.repo\\coding2017.eulerlcs\\group09\\41689722.eulerlcs\\2.code +/configuration/org.eclipse.ui.ide/RECENT_WORKSPACES_PROTOCOL=3 +/configuration/org.eclipse.ui.ide/SHOW_RECENT_WORKSPACES=false +/configuration/org.eclipse.ui.ide/SHOW_WORKSPACE_SELECTION_DIALOG=true +/instance/org.eclipse.core.net/org.eclipse.core.net.hasMigrated=true +/instance/org.eclipse.core.resources/encoding=UTF-8 +/instance/org.eclipse.core.resources/version=1 +/instance/org.eclipse.debug.core/prefWatchExpressions=\r\n\r\n +/instance/org.eclipse.debug.ui/org.eclipse.debug.ui.PREF_LAUNCH_PERSPECTIVES=\r\n\r\n +/instance/org.eclipse.debug.ui/pref_state_memento.org.eclipse.debug.ui.DebugVieworg.eclipse.debug.ui.DebugView=\r\n +/instance/org.eclipse.debug.ui/pref_state_memento.org.eclipse.debug.ui.ExpressionView=\r\n\r\n\r\n +/instance/org.eclipse.debug.ui/pref_state_memento.org.eclipse.debug.ui.VariableView=\r\n +/instance/org.eclipse.debug.ui/preferredDetailPanes=DefaultDetailPane\:DefaultDetailPane| +/instance/org.eclipse.e4.ui.css.swt.theme/themeid=org.eclipse.e4.ui.css.theme.e4_default6.0,6.1,6.2,6.3,10.0 +/instance/org.eclipse.e4.ui.workbench.renderers.swt/enableMRU=true +/instance/org.eclipse.e4.ui.workbench.renderers.swt/themeEnabled=true +/instance/org.eclipse.egit.core/GitRepositoriesView.GitDirectories=E\:\\10.github.repo\\coding2017.eulerlcs\\.git; +/instance/org.eclipse.egit.core/GitRepositoriesView.GitDirectories.relative=E\:\\10.github.repo\\coding2017.eulerlcs\\.git; +/instance/org.eclipse.epp.logging.aeri.ide/resetSendMode=KEEP +/instance/org.eclipse.epp.logging.aeri.ide/resetSendModeOn=0 +/instance/org.eclipse.epp.logging.aeri.ide/sendMode=NOTIFY +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.codeComplete.visibilityCheck=enabled +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.compliance=1.8 +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.source=1.8 +/instance/org.eclipse.jdt.launching/org.eclipse.jdt.launching.PREF_VM_XML=\r\n\r\n\r\n\r\n\r\n\r\n +/instance/org.eclipse.jdt.ui/content_assist_number_of_computers=24 +/instance/org.eclipse.jdt.ui/content_assist_proposals_background=255,255,255 +/instance/org.eclipse.jdt.ui/content_assist_proposals_foreground=0,0,0 +/instance/org.eclipse.jdt.ui/editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +/instance/org.eclipse.jdt.ui/fontPropagated=true +/instance/org.eclipse.jdt.ui/org.eclipse.jdt.internal.ui.navigator.layout=2 +/instance/org.eclipse.jdt.ui/org.eclipse.jdt.internal.ui.navigator.librariesnode=true +/instance/org.eclipse.jdt.ui/org.eclipse.jdt.ui.editor.tab.width= +/instance/org.eclipse.jdt.ui/org.eclipse.jdt.ui.formatterprofiles.version=12 +/instance/org.eclipse.jdt.ui/org.eclipse.jdt.ui.javadoclocations.migrated=true +/instance/org.eclipse.jdt.ui/org.eclipse.jface.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.jdt.ui/proposalOrderMigrated=true +/instance/org.eclipse.jdt.ui/sourceHoverBackgroundColor=255,255,225 +/instance/org.eclipse.jdt.ui/sp_cleanup.add_default_serial_version_id=true +/instance/org.eclipse.jdt.ui/sp_cleanup.add_generated_serial_version_id=false +/instance/org.eclipse.jdt.ui/sp_cleanup.add_missing_annotations=true +/instance/org.eclipse.jdt.ui/sp_cleanup.add_missing_deprecated_annotations=true +/instance/org.eclipse.jdt.ui/sp_cleanup.add_missing_methods=false +/instance/org.eclipse.jdt.ui/sp_cleanup.add_missing_nls_tags=false +/instance/org.eclipse.jdt.ui/sp_cleanup.add_missing_override_annotations=true +/instance/org.eclipse.jdt.ui/sp_cleanup.add_missing_override_annotations_interface_methods=true +/instance/org.eclipse.jdt.ui/sp_cleanup.add_serial_version_id=false +/instance/org.eclipse.jdt.ui/sp_cleanup.always_use_blocks=true +/instance/org.eclipse.jdt.ui/sp_cleanup.always_use_parentheses_in_expressions=false +/instance/org.eclipse.jdt.ui/sp_cleanup.always_use_this_for_non_static_field_access=false +/instance/org.eclipse.jdt.ui/sp_cleanup.always_use_this_for_non_static_method_access=false +/instance/org.eclipse.jdt.ui/sp_cleanup.convert_functional_interfaces=false +/instance/org.eclipse.jdt.ui/sp_cleanup.convert_to_enhanced_for_loop=false +/instance/org.eclipse.jdt.ui/sp_cleanup.correct_indentation=false +/instance/org.eclipse.jdt.ui/sp_cleanup.format_source_code=true +/instance/org.eclipse.jdt.ui/sp_cleanup.format_source_code_changes_only=false +/instance/org.eclipse.jdt.ui/sp_cleanup.insert_inferred_type_arguments=false +/instance/org.eclipse.jdt.ui/sp_cleanup.make_local_variable_final=true +/instance/org.eclipse.jdt.ui/sp_cleanup.make_parameters_final=false +/instance/org.eclipse.jdt.ui/sp_cleanup.make_private_fields_final=true +/instance/org.eclipse.jdt.ui/sp_cleanup.make_type_abstract_if_missing_method=false +/instance/org.eclipse.jdt.ui/sp_cleanup.make_variable_declarations_final=false +/instance/org.eclipse.jdt.ui/sp_cleanup.never_use_blocks=false +/instance/org.eclipse.jdt.ui/sp_cleanup.never_use_parentheses_in_expressions=true +/instance/org.eclipse.jdt.ui/sp_cleanup.on_save_use_additional_actions=true +/instance/org.eclipse.jdt.ui/sp_cleanup.organize_imports=true +/instance/org.eclipse.jdt.ui/sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +/instance/org.eclipse.jdt.ui/sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +/instance/org.eclipse.jdt.ui/sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +/instance/org.eclipse.jdt.ui/sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +/instance/org.eclipse.jdt.ui/sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_private_constructors=true +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_redundant_type_arguments=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_trailing_whitespaces=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_trailing_whitespaces_all=true +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unnecessary_casts=true +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unnecessary_nls_tags=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unused_imports=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unused_local_variables=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unused_private_fields=true +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unused_private_members=false +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unused_private_methods=true +/instance/org.eclipse.jdt.ui/sp_cleanup.remove_unused_private_types=true +/instance/org.eclipse.jdt.ui/sp_cleanup.sort_members=false +/instance/org.eclipse.jdt.ui/sp_cleanup.sort_members_all=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_anonymous_class_creation=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_blocks=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_blocks_only_for_return_and_throw=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_lambda=true +/instance/org.eclipse.jdt.ui/sp_cleanup.use_parentheses_in_expressions=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_this_for_non_static_field_access=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +/instance/org.eclipse.jdt.ui/sp_cleanup.use_this_for_non_static_method_access=false +/instance/org.eclipse.jdt.ui/sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true +/instance/org.eclipse.jdt.ui/spelling_locale_initialized=true +/instance/org.eclipse.jdt.ui/tabWidthPropagated=true +/instance/org.eclipse.jdt.ui/useAnnotationsPrefPage=true +/instance/org.eclipse.jdt.ui/useQuickDiffPrefPage=true +/instance/org.eclipse.jst.j2ee.webservice.ui/areThereWebServices=false +/instance/org.eclipse.m2e.discovery/org.eclipse.m2e.discovery.pref.projects= +/instance/org.eclipse.mylyn.context.core/mylyn.attention.migrated=true +/instance/org.eclipse.mylyn.monitor.ui/org.eclipse.mylyn.monitor.activity.tracking.enabled.checked=true +/instance/org.eclipse.mylyn.tasks.ui/migrated.task.repositories.secure.store=true +/instance/org.eclipse.mylyn.tasks.ui/org.eclipse.mylyn.tasks.ui.filters.nonmatching=true +/instance/org.eclipse.mylyn.tasks.ui/org.eclipse.mylyn.tasks.ui.filters.nonmatching.encouraged=true +/instance/org.eclipse.mylyn.tasks.ui/org.eclipse.mylyn.tasks.ui.welcome.message=true +/instance/org.eclipse.oomph.workingsets/working.set.group=\n\n +/instance/org.eclipse.rse.core/org.eclipse.rse.systemtype.local.systemType.defaultUserId=euler +/instance/org.eclipse.rse.ui/org.eclipse.rse.preferences.order.connections=euler-PC.Local +/instance/org.eclipse.team.ui/org.eclipse.team.ui.first_time=false +/instance/org.eclipse.ui.editors/overviewRuler_migration=migrated_3.1 +/instance/org.eclipse.ui.ide/PROBLEMS_FILTERS_MIGRATE=true +/instance/org.eclipse.ui.ide/platformState=1488095469945 +/instance/org.eclipse.ui.ide/quickStart=false +/instance/org.eclipse.ui.ide/tipsAndTricks=true +/instance/org.eclipse.ui.workbench//org.eclipse.ui.commands/state/org.eclipse.ui.navigator.resources.nested.changeProjectPresentation/org.eclipse.ui.commands.radioState=false +/instance/org.eclipse.ui.workbench//org.eclipse.ui.commands/state/org.eclipse.wst.xml.views.XPathView.processor.xpathprocessor/org.eclipse.ui.commands.radioState=xpath10 +/instance/org.eclipse.ui.workbench/ColorsAndFontsPreferencePage.expandedCategories=Torg.eclipse.ui.workbenchMisc\tTorg.eclipse.jdt.ui.presentation\tTorg.eclipse.wst.sse.ui +/instance/org.eclipse.ui.workbench/ColorsAndFontsPreferencePage.selectedElement=Forg.eclipse.jface.textfont +/instance/org.eclipse.ui.workbench/PLUGINS_NOT_ACTIVATED_ON_STARTUP=org.eclipse.m2e.discovery;org.eclipse.rse.ui; +/instance/org.eclipse.ui.workbench/REMOTE_COMMANDS_VIEW_FONT=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.compare.contentmergeviewer.TextMergeViewer=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.debug.ui.DetailPaneFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.debug.ui.MemoryViewTableFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.debug.ui.consoleFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.egit.ui.CommitMessageEditorFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.egit.ui.CommitMessageFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.egit.ui.DiffHeadlineFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.jdt.internal.ui.compare.JavaMergeViewer=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.jdt.internal.ui.compare.PropertiesFileMergeViewer=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.jdt.ui.PropertiesFileEditor.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.jdt.ui.editors.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.jface.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.mylyn.wikitext.ui.presentation.textFont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.pde.internal.ui.compare.ManifestContentMergeViewer=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.pde.internal.ui.compare.PluginContentMergeViewer=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.ui.commands=\r\n +/instance/org.eclipse.ui.workbench/org.eclipse.wst.jsdt.internal.ui.compare.JavaMergeViewer=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.wst.jsdt.ui.editors.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/org.eclipse.wst.sse.ui.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui.workbench/terminal.views.view.font.definition=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.ui/showIntro=false +/instance/org.eclipse.wst.jsdt.ui/fontPropagated=true +/instance/org.eclipse.wst.jsdt.ui/org.eclipse.jface.textfont=1|Consolas|12.0|0|WINDOWS|1|-16|0|0|0|400|0|0|0|0|3|2|1|49|Consolas; +/instance/org.eclipse.wst.jsdt.ui/org.eclipse.wst.jsdt.ui.editor.tab.width= +/instance/org.eclipse.wst.jsdt.ui/org.eclipse.wst.jsdt.ui.formatterprofiles.version=11 +/instance/org.eclipse.wst.jsdt.ui/org.eclipse.wst.jsdt.ui.javadoclocations.migrated=true +/instance/org.eclipse.wst.jsdt.ui/proposalOrderMigrated=true +/instance/org.eclipse.wst.jsdt.ui/tabWidthPropagated=true +/instance/org.eclipse.wst.jsdt.ui/useAnnotationsPrefPage=true +/instance/org.eclipse.wst.jsdt.ui/useQuickDiffPrefPage=true +@org.eclipse.core.net=1.3.0.v20160418-1534 +@org.eclipse.core.resources=3.11.1.v20161107-2032 +@org.eclipse.debug.core=3.10.100.v20160419-1720 +@org.eclipse.debug.ui=3.11.202.v20161114-0338 +@org.eclipse.e4.ui.css.swt.theme=0.10.100.v20160523-0836 +@org.eclipse.e4.ui.workbench.renderers.swt=0.14.0.v20160525-0940 +@org.eclipse.egit.core=4.4.1.201607150455-r +@org.eclipse.epp.logging.aeri.ide=2.0.3.v20161205-0933 +@org.eclipse.jdt.core=3.12.2.v20161117-1814 +@org.eclipse.jdt.launching=3.8.101.v20161111-2014 +@org.eclipse.jdt.ui=3.12.2.v20160929-0804 +@org.eclipse.jst.j2ee.webservice.ui=1.1.500.v201302011850 +@org.eclipse.m2e.discovery=1.7.0.20160603-1933 +@org.eclipse.mylyn.context.core=3.21.0.v20160701-1337 +@org.eclipse.mylyn.monitor.ui=3.21.0.v20160630-1702 +@org.eclipse.mylyn.tasks.ui=3.21.0.v20160913-2131 +@org.eclipse.oomph.workingsets=1.6.0.v20161019-0656 +@org.eclipse.rse.core=3.3.100.201603151753 +@org.eclipse.rse.ui=3.3.300.201610252046 +@org.eclipse.team.ui=3.8.0.v20160518-1906 +@org.eclipse.ui=3.108.1.v20160929-1045 +@org.eclipse.ui.editors=3.10.1.v20161106-1856 +@org.eclipse.ui.ide=3.12.2.v20161115-1450 +@org.eclipse.ui.workbench=3.108.2.v20161025-2029 +@org.eclipse.wst.jsdt.ui=2.0.0.v201608301904 +file_export_version=3.0 diff --git a/group09/610673813/src/coding/week02/array/ArrayUtil.java b/group09/610673813/src/coding/week02/array/ArrayUtil.java new file mode 100644 index 0000000000..ed443a2c22 --- /dev/null +++ b/group09/610673813/src/coding/week02/array/ArrayUtil.java @@ -0,0 +1,345 @@ +package coding.week02.array; + +public class ArrayUtil +{ + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin) + { + for(int i=0, j = origin.length-1; i array2[j]) + { + newArray[count++] = array2[j++]; + } + else if(array1[i] == array2[j]) + { + newArray[count++] = array2[j++]; + i++; + } + } + while(i==array1.length && j 1) + { + s = s + seperator; + for(int i=1; i= length - 1){ - endPos = length - 1; - } - new DownloadThread(barrier , conn, startPos, endPos , filePath).start(); - } + } - } catch (ConnectionException e) { - System.out.println(e.getMessage()); - } finally{ - if(conn != null){ - conn.close(); - } - } + public void oneThreadDownload() { + final CyclicBarrier barrier = new CyclicBarrier(1 ,new Runnable() { + @Override + public void run() { + getListener().notifyFinished(); + } + }); + try { + Thread thread = new DownloadThread("oneThread", conn,0,length, fileName, barrier); + thread.start(); + } finally { + if (conn != null) { + conn.close(); + } + } + } - } - - private String getFileType(String url) { - int index = url.lastIndexOf("."); - return url.substring(index + 1 , url.length()); - } + public void threadPoolDownload() throws ConnectionException { + final CyclicBarrier barrier = new CyclicBarrier(threadNum ,new Runnable() { + @Override + public void run() { + getListener().notifyFinished(); // 栅栏 + } + }); + ExecutorService threadPool = Executors.newCachedThreadPool(); + int len = conn.getContentLength(); + for(int i = 0; i< threadNum; i++) + { + int start=i*len/ threadNum; + int end = (i+1)*len/ threadNum -1; + conn = cm.open(this.url); + if(i== threadNum -1) + { + end =len; + } + Thread thread = new DownloadThread("thread"+i, conn, start, end, fileName, barrier); + threadPool.execute(thread); + } + if (conn != null) { + conn.close(); + } + } public void setListener(DownloadListener listener) { this.listener = listener; } - - public void setConnectionManager(ConnectionManager ucm){ this.cm = ucm; @@ -108,4 +115,4 @@ public DownloadListener getListener(){ return this.listener; } -} \ No newline at end of file +} diff --git a/group09/790466157/src/com/coderising/download/FileDownloaderTest.java b/group09/790466157/src/com/coderising/download/FileDownloaderTest.java index a79d23d9c5..66d6455036 100644 --- a/group09/790466157/src/com/coderising/download/FileDownloaderTest.java +++ b/group09/790466157/src/com/coderising/download/FileDownloaderTest.java @@ -10,6 +10,7 @@ public class FileDownloaderTest { boolean downloadFinished = false; + private double time = 0; @Before public void setUp() throws Exception { } @@ -21,39 +22,34 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488796402240&di=8ca9322617d5338cad61232a06f6ed7a&imgtype=0&src=http%3A%2F%2Fjiangsu.china.com.cn%2Fuploadfile%2F2017%2F0212%2F1486868426284307.jpg"; - - FileDownloader downloader = new FileDownloader(url); + String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; +// String url = "https://www.baidu.com/img/bd_logo.png"; + FileDownloader downloader = new FileDownloader(url, "test.png"); ConnectionManager cm = new ConnectionManagerImpl(); downloader.setConnectionManager(cm); - downloader.setListener(new DownloadListener() { @Override public void notifyFinished() { downloadFinished = true; } - }); - downloader.execute(); - // �ȴ����߳����س���ִ����� + // 等待多线程下载程序执行完毕 while (!downloadFinished) { try { - System.out.println("��û��������ɣ���������"); - //����5�� - Thread.sleep(5000); + System.out.println("还没有下载完成,休眠0.01秒"); + time += 0.01; + //休眠0.01秒 + Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } - System.out.println("������ɣ�"); - - + System.out.println("下载完成!耗时"+time+"秒"); } - -} \ No newline at end of file +} diff --git a/group09/790466157/src/com/coderising/download/api/Connection.java b/group09/790466157/src/com/coderising/download/api/Connection.java index fe772d969c..d370d27c68 100644 --- a/group09/790466157/src/com/coderising/download/api/Connection.java +++ b/group09/790466157/src/com/coderising/download/api/Connection.java @@ -4,23 +4,21 @@ public interface Connection { /** - * ʼͽλã ȡݣ ֵֽ - * @param startPos ʼλã 0ʼ - * @param endPos λ + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 * @return */ - public byte[] read(int startPos,int endPos) throws IOException; + byte[] read(int startPos, int endPos) throws IOException; + /** - * õݵij + * 得到数据内容的长度 * @return */ - public int getContentLength(); + int getContentLength(); /** - * ر + * 关闭连接 */ - public void close(); - public Connection open(Object url); + void close(); } - - diff --git a/group09/790466157/src/com/coderising/download/api/ConnectionException.java b/group09/790466157/src/com/coderising/download/api/ConnectionException.java index 132bf8fbdd..1551a80b3d 100644 --- a/group09/790466157/src/com/coderising/download/api/ConnectionException.java +++ b/group09/790466157/src/com/coderising/download/api/ConnectionException.java @@ -1,5 +1,5 @@ package com.coderising.download.api; public class ConnectionException extends Exception { - -} \ No newline at end of file + +} diff --git a/group09/790466157/src/com/coderising/download/api/ConnectionManager.java b/group09/790466157/src/com/coderising/download/api/ConnectionManager.java index e6a9811662..e3759c46ce 100644 --- a/group09/790466157/src/com/coderising/download/api/ConnectionManager.java +++ b/group09/790466157/src/com/coderising/download/api/ConnectionManager.java @@ -1,10 +1,13 @@ package com.coderising.download.api; +import java.io.IOException; +import java.net.ProtocolException; + public interface ConnectionManager { /** - * һurl , һ + * 给定一个url , 打开一个连接 * @param url * @return */ - public Connection open(String url) throws ConnectionException; -} \ No newline at end of file + Connection open(String url) throws ConnectionException; +} diff --git a/group09/790466157/src/com/coderising/download/api/DownloadListener.java b/group09/790466157/src/com/coderising/download/api/DownloadListener.java index 64ac13231b..de81b7607d 100644 --- a/group09/790466157/src/com/coderising/download/api/DownloadListener.java +++ b/group09/790466157/src/com/coderising/download/api/DownloadListener.java @@ -1,5 +1,5 @@ package com.coderising.download.api; public interface DownloadListener { - public void notifyFinished(); -} \ No newline at end of file + void notifyFinished(); +} diff --git a/group09/790466157/src/com/coderising/download/impl/ConnectionImpl.java b/group09/790466157/src/com/coderising/download/impl/ConnectionImpl.java index a0bea92f06..ba27ce6c9a 100644 --- a/group09/790466157/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group09/790466157/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,51 +1,64 @@ package com.coderising.download.impl; - +import java.io.BufferedInputStream; import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Arrays; - - -import org.omg.CORBA.portable.InputStream; - +import com.basic.ArrayList; import com.coderising.download.api.Connection; +public class ConnectionImpl implements Connection{ + private HttpURLConnection downLoadConn; + private HttpURLConnection getLengthConn; -import java.net.URLConnection; + public ConnectionImpl(URL urlObject) { + HttpURLConnection conn = null; + try { + downLoadConn = (HttpURLConnection) urlObject.openConnection(); + downLoadConn.setRequestMethod("GET"); -import com.coderising.download.api.Connection; + getLengthConn = (HttpURLConnection) urlObject.openConnection(); + getLengthConn.setRequestMethod("GET"); + } catch (IOException e) { + e.printStackTrace(); + } + + } -public class ConnectionImpl implements Connection{ - -private URLConnection connection; - @Override public byte[] read(int startPos, int endPos) throws IOException { -// connection.setAllowUserInteraction(true); -// connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - InputStream inputstream = (InputStream) connection.getInputStream(); - byte[] buffer = new byte[endPos - startPos + 1]; - inputstream.skip(startPos); - inputstream.read(buffer); - inputstream.close(); - return buffer; - } + downLoadConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream in = downLoadConn.getInputStream(); + byte[] buf = new byte[endPos-startPos+1]; + byte[] tempBuf = new byte[1024]; + BufferedInputStream bis = new BufferedInputStream(in); + int len = 0; + int totalLen = 0; + while((len=bis.read(tempBuf,0,tempBuf.length))!=-1){ + System.arraycopy(tempBuf, 0, buf, totalLen, len); + totalLen += len; + } + String desc = " bytes=" + startPos + "-" + endPos + " "; + System.out.println(Thread.currentThread().getName()+desc+totalLen); + in.close(); + bis.close(); + return Arrays.copyOf(buf, totalLen); + } @Override - public int getContentLength(){ - return connection.getContentLength(); + public int getContentLength() { + int len = getLengthConn.getContentLength(); + return len; } @Override public void close() { + downLoadConn.disconnect(); + getLengthConn.disconnect(); } - public void setConnection(URLConnection connection) { - this.connection = connection; - } - - @Override - public Connection open(Object url) { - // TODO Auto-generated method stub - return null; - } -} \ No newline at end of file +} diff --git a/group09/790466157/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group09/790466157/src/com/coderising/download/impl/ConnectionManagerImpl.java index 99bdec847f..b66bb996be 100644 --- a/group09/790466157/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/group09/790466157/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -1,35 +1,23 @@ package com.coderising.download.impl; - - - -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; - - - - import com.coderising.download.api.Connection; -import com.coderising.download.impl.ConnectionImpl; import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; +import java.io.IOException; +import java.net.URL; + public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - URL urlObj ; - ConnectionImpl connection = null; - try { - urlObj = new URL(url); - connection = new ConnectionImpl(); - } catch (MalformedURLException e) { - throw new ConnectionException(); - } - return connection; + URL urlObject; + try { + urlObject = new URL(url); + return new ConnectionImpl(urlObject); + } catch (IOException e) { + e.printStackTrace(); + throw new ConnectionException(); + } } -} \ No newline at end of file +} diff --git a/group09/790466157/src/com/coderising/linkedlist/Iterator.java b/group09/790466157/src/com/coderising/linkedlist/Iterator.java new file mode 100644 index 0000000000..b09016ee94 --- /dev/null +++ b/group09/790466157/src/com/coderising/linkedlist/Iterator.java @@ -0,0 +1,7 @@ +package com.coderising.linkedlist; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group09/790466157/src/com/coderising/linkedlist/LinkedList.java b/group09/790466157/src/com/coderising/linkedlist/LinkedList.java new file mode 100644 index 0000000000..7530d105e9 --- /dev/null +++ b/group09/790466157/src/com/coderising/linkedlist/LinkedList.java @@ -0,0 +1,263 @@ +package com.coderising.linkedlist; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(head == null){ + head = new Node(o); + }else{ + Node pos = head; + while(pos.next != null){ + pos = pos.next; + } + pos.next = new Node(o); + } + size++; + } + + public void add(int index , Object o){ + checkIndex(index); + if(index == 0) { + Node node = new Node(o); + node.next = head; + head = node; + } + else{ + Node pos = head; + for(int i = 0;i < index-1;i++){ + pos = pos.next; + } + Node node = new Node(o); + node.next = pos.next; + pos.next = node; + } + size++; + } + + private void checkIndex(int index) { + if(index < 0 || index >size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); + } + + public Object get(int index){ + checkIndexPosition(index); + Node pos = head; + for(int i = 0;i < index;i++){ + pos = pos.next; + } + return pos.data; + } + + public Object remove(int index){ + checkIndexPosition(index); + Node element = head; + if(index == 0){ + head = head.next; + }else{ + Node pos = head; + for(int i = 0;i < index - 1;i++){ + pos = pos.next; + } + element = pos.next; + pos.next = pos.next.next; + } + size--; + return element.data; + } + + private void checkIndexPosition(int index) { + if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); + } + + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + + public void addLast(Object o){ + add(size,o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + class LinkedListIterator implements Iterator{ + + private Node node = head; + private int pos = 0; + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public Object next() { + pos++; + if(pos != 1){ + node = node.next; + } + return node.data; + } + } + + private static class Node{ + Object data; + Node next; + public Node(Object data){ + this.data = data; + next = null; + } + } + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(size == 0) return; + + for(int i=1;i5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(size == 0) return; + + int removeNum = size/2; + for(int i=0;i size || i<0 || i>=size) return; + + for(int k=i;k<(length+i);k++){ + remove(i); + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(list == null) return new int[0]; + + int[] targetList = new int[list.size]; + for(int i=0;i min && (int)get(i) < max){ + remove(i--); + } + } + */ + + //遍历到最小值和最大值处并记录位置,最后调用remove(int i,int length)进行范围内的删除。 + int minPos = 0; + int maxPos = 0; + boolean exec = true; + for(int i=0;i min) { + minPos = i; + exec = false; + } else if((int)get(i) >max){ + maxPos = i; + break; + } + } + remove(minPos, maxPos - minPos); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList newList = new LinkedList(); + for(int i=0;i actions = new HashMap<>(); + + public Configuration(String fileName) { + + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is){ + + SAXBuilder builder = new SAXBuilder(); + + try { + + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for(Element actionElement : root.getChildren("action")){ + + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for(Element resultElement : actionElement.getChildren("result")){ + + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + + this.actions.put(actionName, ac); + } + + + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + + } + + + } + + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getViewName(resultName); + } + + private static class ActionConfig{ + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } + +} diff --git a/group09/790466157/src/com/coderising/litestruts/ConfigurationException.java b/group09/790466157/src/com/coderising/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..97e286827f --- /dev/null +++ b/group09/790466157/src/com/coderising/litestruts/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.coderising.litestruts; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } + +} diff --git a/group09/790466157/src/com/coderising/litestruts/ConfigurationTest.java b/group09/790466157/src/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..734649f37a --- /dev/null +++ b/group09/790466157/src/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,50 @@ +package com.coderising.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ConfigurationTest { + + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView(){ + String jsp = cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + + } + +} diff --git a/group09/790466157/src/com/coderising/litestruts/ReflectionUtil.java b/group09/790466157/src/com/coderising/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..0bd53fea93 --- /dev/null +++ b/group09/790466157/src/com/coderising/litestruts/ReflectionUtil.java @@ -0,0 +1,123 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for(String name : params.keySet() ){ + + String methodName = "set" + name; + + for(Method m: methods){ + + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + private static List getMethods(Class clz, String startWithName){ + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith(startWithName)){ + + methods.add(m); + + } + + } + + return methods; + } + + public static Map getParamterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for(Method m : methods){ + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + + e.printStackTrace(); + } + } + + return params; + } + + ////////////////////////Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("get")){ + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("set")){ + + methods.add(m); + + } + + } + + return methods; + + } + + + + +} diff --git a/group09/790466157/src/com/coderising/litestruts/ReflectionUtilTest.java b/group09/790466157/src/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..cbe732d83f --- /dev/null +++ b/group09/790466157/src/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception{ + + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + ReflectionUtil.setParameters(o,params); + + + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + @Test + public void testGetGetterMethod() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetParamters() throws Exception{ + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + + + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } +} diff --git a/group09/790466157/src/com/coderising/litestruts/SAX.java b/group09/790466157/src/com/coderising/litestruts/SAX.java deleted file mode 100644 index ab3f0c1044..0000000000 --- a/group09/790466157/src/com/coderising/litestruts/SAX.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.coderising.litestruts; -import org.xml.sax.helpers.DefaultHandler; -import org.xml.sax.Attributes; -public class SAX extends DefaultHandler { - //ĵʼ¼ - public void startDocument() { - System.out.println("ĵʼ "); - } - //ĵ¼ - public void endDocument() { - System.out.println("ĵ"); - } - //Ԫؿʼ¼ - public void startElement(String uri, String localName, String qname, Attributes attr) - { System.out.println("Ԫؿʼ: : " + localName + " ޶: " + qname + " ռURI: "+uri); - int attrCount = attr.getLength(); - if(attrCount>0) { - System.out.println(":"); - for(int i = 0 ; i parameters) { - /* - - 0. ȡļstruts.xml + /* + + 0. 读取配置文件struts.xml - 1. actionNameҵӦclass LoginAction, ͨʵ - parametersеݣösetter parametersе + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") , - ǾӦõ setNamesetPassword + 那就应该调用 setName和setPassword方法 - 2. ͨöexectue ÷ֵ"success" + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - 3. ͨҵgetter getMessage, - ͨã ֵγһHashMap , {"message": "¼ɹ"} , - ŵViewparameters + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters - 4. struts.xmlе ,Լexecuteķֵ ȷһjsp - ŵViewjspֶС + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 */ + + + String clzName = cfg.getClassName(actionName); + + if(clzName == null){ + return null; + } + try { + + Class clz = Class.forName(clzName); + Object action = clz.newInstance(); + + ReflectionUtil.setParameters(action, parameters); + + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String)m.invoke(action); + + Map params = ReflectionUtil.getParamterMap(action); + String resultView = cfg.getResultView(actionName, resultName); + View view = new View(); + view.setParameters(params); + view.setJsp(resultView); + return view; + + + + } catch (Exception e) { + + e.printStackTrace(); + } return null; } diff --git a/group10/205301442/src/api/Connection.java b/group10/205301442/src/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group10/205301442/src/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group10/205301442/src/api/ConnectionException.java b/group10/205301442/src/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group10/205301442/src/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group10/205301442/src/api/ConnectionManager.java b/group10/205301442/src/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group10/205301442/src/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group10/205301442/src/api/DownloadListener.java b/group10/205301442/src/api/DownloadListener.java new file mode 100644 index 0000000000..41c4907246 --- /dev/null +++ b/group10/205301442/src/api/DownloadListener.java @@ -0,0 +1,6 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(boolean isFinish); + public boolean getIsFinished(); +} diff --git a/group10/205301442/src/com/coding/week1/LinkedList1.java b/group10/205301442/src/com/coding/week1/LinkedList1.java new file mode 100644 index 0000000000..9583a6505c --- /dev/null +++ b/group10/205301442/src/com/coding/week1/LinkedList1.java @@ -0,0 +1,280 @@ +package com.coding.week1; +import java.util.List; +import java.util.ArrayList; + +public class LinkedList1 { + private Node head; + private int size; + public void add(Object o){ + Node node = new Node(o,null); + node(size-1).next = node; + size++; + } + public void add(int index , Object o){ + if(index<0){ + return; + } + Node node = new Node(o,null); + if(index==0){ + head=node; + node.next = node(0); + }else{ + node(index-1).next = node; + node.next = node(index+1); + } + size++; + } + public Object get(int index){ + if(index<0){ + return null; + } + return node(index); + } + public Object remove(int index){ + if(index<0){ + return null; + } + Object o =node(index).data; + node(index-1).next = node(index+1); + size--; + return o; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + Node n = new Node(o,null); + head = n; + if(size>0){ + n.next = node(0); + } + size++; + + } + public void addLast(Object o){ + Node n = new Node(o,null); + if(size>0){ + node(size-1).next = n; + }else{ + head = n; + } + size++; + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return new Ito(); + } + public class Ito implements Iterator{ + int cursor; + @Override + public boolean hasNext() { + if(cursor!=size){ + return true; + } + return false; + } + + @Override + public Object next() { + if(cursor>=size-1){ + return null; + } + Object o=node(cursor).data; + cursor++; + return o; + + } + + } + + + private static class Node{ + Object data; + Node next; + public Node(Object data,Node next){ + this.data = data; + this.next = next; + } + + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node x = node(size-1); + head = x; + for(int i=size-2;i>=0;i--){ + x.next = node(i); + x = node(i); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int newSize = size/2+size%2; + head = node(newSize-1); + size = newSize; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + if(i==0){ + head = node(length); + } + node(i-1).next = node(i+length); + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] temp = new int[size]; + for(int i=0;i-1){ + temp[0]=Integer.parseInt(o); + j++; + } + } + + } + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + int[] temp = new int[size]; + int newSize = 0; + for(int i=0;i lists = new ArrayList(); + while(true){ + if((int)n.data>min&&(int)n.data0){ + if(nowLen+m>len){ + System.arraycopy(temp, 0, bt, nowLen, len-nowLen); + break; + } + System.arraycopy(temp, 0, bt, nowLen, m); + nowLen += m; + + } + return bt; + } + + @Override + public int getContentLength() { + if(is==null){ + return 0; + } + try { + int length=conn.getContentLength(); + return length; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return 0; + } + + @Override + public void close() { + if(is!=null){ + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + +} diff --git a/group10/205301442/src/download/impl/ConnectionManagerImpl.java b/group10/205301442/src/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..34e02aad97 --- /dev/null +++ b/group10/205301442/src/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,34 @@ +package com.coderising.download.impl; + +import java.io.DataInputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + //在connection里实现连接,不再这儿,这里只是返回 + @Override + public Connection open(String url) throws ConnectionException { + try { + + + URL uri = new URL(url); + HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(5*1000); + Connection connImpl = new ConnectionImpl(conn); + return connImpl; + } catch (IOException e) { + // TODO Auto-generated catch block + + } + return null; + } + +} diff --git a/group10/205301442/src/download/impl/DoloadListenerImpl.java b/group10/205301442/src/download/impl/DoloadListenerImpl.java new file mode 100644 index 0000000000..d448b817c3 --- /dev/null +++ b/group10/205301442/src/download/impl/DoloadListenerImpl.java @@ -0,0 +1,16 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.DownloadListener; + +public class DoloadListenerImpl implements DownloadListener{ + boolean isFinish = false; + @Override + public void notifyFinished(boolean isfinish) { + // TODO Auto-generated method stub + this.isFinish = isfinish; + } + public boolean getIsFinished(){ + return isFinish; + } + +} diff --git a/group10/205301442/src/impl/ConnectionImpl.java b/group10/205301442/src/impl/ConnectionImpl.java new file mode 100644 index 0000000000..612ef940c9 --- /dev/null +++ b/group10/205301442/src/impl/ConnectionImpl.java @@ -0,0 +1,76 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private HttpURLConnection conn; + private InputStream is; + public ConnectionImpl(HttpURLConnection conn) { + try { + this.conn = conn; + is = conn.getInputStream(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + if(is==null){ + return null; + } + + is.skip(startPos); + int len = endPos-startPos; + byte[] bt = new byte[len]; + byte[] temp = new byte[1024]; + int m=0; + int nowLen=0; + while((m=is.read(temp))>0){ + if(nowLen+m>len){ + System.arraycopy(temp, 0, bt, nowLen, len-nowLen); + break; + } + System.arraycopy(temp, 0, bt, nowLen, m); + nowLen += m; + + } + return bt; + } + + @Override + public int getContentLength() { + if(is==null){ + return 0; + } + try { + int length=conn.getContentLength(); + return length; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return 0; + } + + @Override + public void close() { + if(is!=null){ + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + +} diff --git a/group10/205301442/src/impl/ConnectionManagerImpl.java b/group10/205301442/src/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..34e02aad97 --- /dev/null +++ b/group10/205301442/src/impl/ConnectionManagerImpl.java @@ -0,0 +1,34 @@ +package com.coderising.download.impl; + +import java.io.DataInputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + //在connection里实现连接,不再这儿,这里只是返回 + @Override + public Connection open(String url) throws ConnectionException { + try { + + + URL uri = new URL(url); + HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(5*1000); + Connection connImpl = new ConnectionImpl(conn); + return connImpl; + } catch (IOException e) { + // TODO Auto-generated catch block + + } + return null; + } + +} diff --git a/group10/205301442/src/impl/DoloadListenerImpl.java b/group10/205301442/src/impl/DoloadListenerImpl.java new file mode 100644 index 0000000000..d448b817c3 --- /dev/null +++ b/group10/205301442/src/impl/DoloadListenerImpl.java @@ -0,0 +1,16 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.DownloadListener; + +public class DoloadListenerImpl implements DownloadListener{ + boolean isFinish = false; + @Override + public void notifyFinished(boolean isfinish) { + // TODO Auto-generated method stub + this.isFinish = isfinish; + } + public boolean getIsFinished(){ + return isFinish; + } + +} diff --git a/group10/904627477/src/com/coding/basic/LRUPageFrame.java b/group10/904627477/src/com/coding/basic/LRUPageFrame.java new file mode 100644 index 0000000000..95dc7ca88f --- /dev/null +++ b/group10/904627477/src/com/coding/basic/LRUPageFrame.java @@ -0,0 +1,135 @@ +package com.coding.basic; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + Node node = findPageNumNode(pageNum); + if(node==null){ + if(size()>=capacity){ + removeLast(); + } + push(pageNum); + }else{ + moveToFirst(node); + } + } + + public Node findPageNumNode(int pageNum) { + Node node = first; + while(node!=null){ + if(node.pageNum==pageNum){ + break; + }else{ + node = node.next; + } + } + return node; + } + + public void moveToFirst(Node node) { + if(first==null||node==null||first.pageNum==node.pageNum){ + return; + } + if(node.pageNum == last.pageNum){ + node.prev.next = null; + last = node.prev; + }else{ + Node tPrev = node.prev; + Node tNext = node.next; + tPrev.next = tNext; + tNext.prev = tPrev; + } + node.prev = null; + node.next = first; + first = node; + } + + public void push(int pageNum) { + if(size()==0){ + first = new Node(); + first.pageNum = pageNum; + last = first; + return ; + } + Node node; + node = new Node(); + node.pageNum = pageNum; + node.next = first; + first.prev = node; + first = node; + } + + public void removeLast() { + if(last==null){ + return ; + } + if(size()==1){ + first = null; + last = null; + return ; + } + Node temp = last.prev; + last.prev = null; + last = temp; + last.next = null; + } + + public int size() { + int size = 0; + Node temp = first; + while (temp!=null) { + size++; + temp = temp.next; + } + return size; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group10/904627477/src/com/coding/download/DownloadThread.java b/group10/904627477/src/com/coding/download/DownloadThread.java index 7e98539050..98a811e9ac 100644 --- a/group10/904627477/src/com/coding/download/DownloadThread.java +++ b/group10/904627477/src/com/coding/download/DownloadThread.java @@ -2,7 +2,8 @@ import java.io.File; import java.io.IOException; -import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; import com.coding.download.api.Connection; import com.coding.util.IOUtils; @@ -13,9 +14,17 @@ public class DownloadThread extends Thread { int startPos; int endPos; private File file; + private CyclicBarrier barrier; + public DownloadThread(Connection conn, int startPos, int endPos,File file,CyclicBarrier barrier) { + this.barrier = barrier; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.file = file; + } + public DownloadThread(Connection conn, int startPos, int endPos,File file) { - this.conn = conn; this.startPos = startPos; this.endPos = endPos; @@ -29,8 +38,15 @@ public void run() { if(buff!=null&&buff.length!=0){ IOUtils.writeFile(file, startPos, buff); } + if(barrier!=null){ //修改后代码 + barrier.await(); + } } catch (IOException e) { e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); } } } diff --git a/group10/904627477/src/com/coding/download/FileDownloader.java b/group10/904627477/src/com/coding/download/FileDownloader.java index 146491f2db..19a0f081b2 100644 --- a/group10/904627477/src/com/coding/download/FileDownloader.java +++ b/group10/904627477/src/com/coding/download/FileDownloader.java @@ -1,17 +1,12 @@ package com.coding.download; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.CyclicBarrier; import com.coding.download.api.Connection; import com.coding.download.api.ConnectionException; import com.coding.download.api.ConnectionManager; import com.coding.download.api.DownloadListener; -import com.coding.download.api.Resource; import com.coding.util.IOUtils; @@ -26,6 +21,7 @@ public class FileDownloader { ConnectionManager cm; private static String localFile = "c:/test/test.jpg"; + private final static int MAX_THREAD_NUM = 3; public FileDownloader(String _url) { @@ -47,14 +43,12 @@ public void execute(){ // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - /**/ + /* my try { Connection conn = cm.open(url); int length = conn.getContentLength(); File file = new File(localFile); - if(!file.exists()){ - IOUtils.createFile(length, localFile); - } + IOUtils.createFile(length, localFile); Resource res = new Resource(url,file); Thread c = new CreateThread(res,length); Thread r = new RemoveThread(res,listener); @@ -63,26 +57,30 @@ public void execute(){ } catch (ConnectionException e) { e.printStackTrace(); } - /*Connection conn = null; + */ try { - conn = cm.open(this.url); - int length = conn.getContentLength(); + CyclicBarrier barrier = new CyclicBarrier(MAX_THREAD_NUM, new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = cm.open(url); + int length = conn.getContentLength(); + IOUtils.createFile(length, localFile); File file = new File(localFile); - if(!file.exists()){ - IOUtils.createFile(length, localFile); + int size = length/MAX_THREAD_NUM; + int last = length%MAX_THREAD_NUM; + for(int i=0;i results = new HashMap(); + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClazz() { + return clazz; + } + public void setClazz(String clazz) { + this.clazz = clazz; + } + public String getMethod() { + return method; + } + public void setMethod(String method) { + this.method = method; + } + public Map getResults() { + return results; + } + public void setResults(Map results) { + this.results = results; + } + public Action() { + super(); + // TODO Auto-generated constructor stub + } + public Action(String name, String clazz, String method) { + super(); + this.name = name; + this.clazz = clazz; + this.method = method; + this.results = new HashMap(); + } + + +} diff --git a/group10/904627477/src/com/coding/litestruts/Result.java b/group10/904627477/src/com/coding/litestruts/Result.java new file mode 100644 index 0000000000..5910b4a15b --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/Result.java @@ -0,0 +1,42 @@ +package com.coding.litestruts; + +public class Result { + + public final static String DEFAULT_NAME="success"; + + private String name; + private String type; + private String jspPath; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getJspPath() { + return jspPath; + } + public void setJspPath(String jspPath) { + this.jspPath = jspPath; + } + public Result(String name, String type, String jspPath) { + super(); + this.name = name; + this.type = type; + this.jspPath = jspPath; + } + public Result() { + super(); + // TODO Auto-generated constructor stub + } + + + + +} diff --git a/group10/904627477/src/com/coding/litestruts/Struts.java b/group10/904627477/src/com/coding/litestruts/Struts.java index eb68fa9441..8c359cf094 100644 --- a/group10/904627477/src/com/coding/litestruts/Struts.java +++ b/group10/904627477/src/com/coding/litestruts/Struts.java @@ -1,11 +1,6 @@ package com.coding.litestruts; -import java.util.List; import java.util.Map; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; @@ -37,21 +32,21 @@ public static View runAction(String actionName, Map parameters) { } View view = new View(); String path = getStrutsXMLPath(); - Element actionEle = getActionElement(path, actionName); + Action actionEle = StrutsXMLParser.getStrutsXML(path).get(actionName); if(actionEle==null){ return null; } - String className = actionEle.attributeValue("class"); - Object action = ReflectUtil.getObject(className, parameters); - if(action==null){ - return null; - } - String methodName = actionEle.attributeValue("method"); - methodName = methodName==null?"execute":methodName; - Object reslut = ReflectUtil.exectue(action, methodName); - String jsp = getElementJsp(actionEle, reslut!=null?reslut.toString():null); - view.setJsp(jsp); - view.setParameters(ReflectUtil.getAttributes(action)); + Object action = ReflectUtil.getObject(actionEle.getClazz(), parameters); + if(action==null){ + return null; + } + Object reslut = ReflectUtil.exectue(action, actionEle.getMethod()); + if(reslut==null){ + return null; + } + Result resultEle = actionEle.getResults().get(reslut.toString()); + view.setJsp(resultEle.getJspPath()); + view.setParameters(ReflectUtil.getAttributes(action)); return view; } @@ -60,47 +55,6 @@ private static String getStrutsXMLPath(){ path = path.substring(1); return path; } - - @SuppressWarnings("unchecked") - public static Element getActionElement(String path,String actionName){ - if(path==null||actionName==null){ - return null; - } - Element actionEle = null; - try { - SAXReader read = new SAXReader(); - Document doc = read.read(path); - Element root = doc.getRootElement(); - List actions = root.elements("action"); - for (Element element : actions) { - String name = element.attributeValue("name"); - if(actionName.equals(name)){ - actionEle = element; - break; - } - } - } catch (SecurityException e) { - e.printStackTrace(); - } catch (DocumentException e) { - e.printStackTrace(); - } - return actionEle; - } - - @SuppressWarnings("unchecked") - public static String getElementJsp(Element actionEle, String reslut) { - String jsp = null; - if(reslut!=null){ - List results = actionEle.elements("result"); - for (Element reslutEle : results) { - String resName = reslutEle.attributeValue("name"); - resName = resName==null?"success":resName; - if(reslut.equals(resName)){ - jsp = reslutEle.getText().trim(); - } - } - } - return jsp; - } + } diff --git a/group10/904627477/src/com/coding/litestruts/StrutsXMLParser.java b/group10/904627477/src/com/coding/litestruts/StrutsXMLParser.java new file mode 100644 index 0000000000..5efe4cbc2a --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/StrutsXMLParser.java @@ -0,0 +1,68 @@ +package com.coding.litestruts; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class StrutsXMLParser { + + public static Map getStrutsXML(){ + String path = System.getProperty("user.dir"); + path = path + "/src/struts.xml"; + return getStrutsXML(path); + } + + public static Map getStrutsXML(String xmlPath){ + if(xmlPath==null){ + throw new IllegalArgumentException(); + } + Map actions = new HashMap(); + try { + SAXReader read = new SAXReader(); + Document doc = read.read(xmlPath); + Element root = doc.getRootElement(); + @SuppressWarnings("unchecked") + List eles = root.elements("action"); + for (Element element : eles) { + String name = element.attributeValue("name"); + actions.put(name, getAction(element)); + } + } catch (SecurityException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } + return actions; + } + + private static Action getAction(Element element) { + String name = element.attributeValue("name"); + String clazz = element.attributeValue("class"); + String method = element.attributeValue("method"); + method = method==null?Action.DEFAULT_METHOD:method; + Action action = new Action(name, clazz, method); + @SuppressWarnings("unchecked") + List eles = element.elements("result"); + for (Element ele : eles) { + String resName = ele.attributeValue("name"); + resName = resName==null?Result.DEFAULT_NAME:resName; + action.getResults().put(resName, getResult(ele)); + } + return action; + } + + private static Result getResult(Element ele) { + String name = ele.attributeValue("name"); + name = name==null?Result.DEFAULT_NAME:name; + String type = ele.attributeValue("type"); + String jspPath = ele.getText().trim(); + Result result = new Result(name, type, jspPath); + return result; + } + +} diff --git a/group10/904627477/src/com/coding/test/ClassFileloaderTest.java b/group10/904627477/src/com/coding/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..f43bfbdbae --- /dev/null +++ b/group10/904627477/src/com/coding/test/ClassFileloaderTest.java @@ -0,0 +1,88 @@ +package com.coding.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.conding.jvm.loader.ClassFileLoader; + +public class ClassFileloaderTest { + + + static String path1 = "D:\\workspace\\MyGithub\\coding2017\\group10\\904627477\\target\\classes"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coding.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1040, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coding.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className){ + String filePath = getFilePath(className); + if(filePath==null){ + return null; + } + byte[] result = IOUtils.readFile(filePath); + return result; + } + + public String getFilePath(String className) { + String filePath = null; + String relativePath = className.replace('.', '/')+".class"; + for (String str : clzPaths) { + String tempPath = str + "/" + relativePath; + File file = new File(tempPath); + if(file.exists()){ + filePath = tempPath; + break; + } + } + return filePath; + } + + public void addClassPath(String path) { + if(path==null||"".equals(path)){ + return; + } + if(clzPaths.indexOf(path)!=-1){ + return ; + } + clzPaths.add(path); + } + + public String getClassPath(){ + StringBuffer sb = new StringBuffer(); + for (String clzPath : clzPaths) { + sb.append(clzPath+";"); + } + return sb.length()==0?"":sb.substring(0, sb.length()-1); + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java b/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java deleted file mode 100644 index 3f41a350e8..0000000000 --- a/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,248 +0,0 @@ -package com.coderising.array; - -import com.coding.basic.ArrayList; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - int length = origin.length; - int[] temp = new int[length]; - for (int i = 0; i < length; i++) { - temp[i] = origin[length-1-i]; - } - System.arraycopy(temp, 0, origin, 0, length); - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - - int length = oldArray.length; - int[] tempArr = new int[length]; - int j = 0; - int zeroNum = 0;//储存0的个数 - for (int i = 0; i < length; i++) { - if(oldArray[i]!=0){ - tempArr[j] = oldArray[i]; - j ++; - }else{ - zeroNum ++; - } - } - //删除数组尾端的0 - int[] newArray = new int[length-zeroNum]; - System.arraycopy(tempArr, 0, newArray, 0, length-zeroNum); - return newArray; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - - int length1 = array1.length; - int length2 = array2.length; - int[] array3 = new int[length1 + length2]; - //将array1、array2的值加入array3中 - System.arraycopy(array1, 0, array3, 0, length1); - System.arraycopy(array2, 0, array3, length1, length2); - int length = array3.length; - int temp; - //将array3冒泡排序 - for (int i = 0; i < length; i++) { - for (int j = 0; j < length - i; j++) { - if(array3[i]>array3[j+i]){ - temp = array3[i]; - array3[i] = array3[j+i]; - array3[j+i] = temp; - } - } - } - return duplicate(array3); - } - - /** - *去重 - */ - private int[] duplicate(int[] array){ - - for (int i = 1; i < array.length; i++) { - if(array[i-1]==array[i]){ - array[i] = 0; - } - } - return removeZero(array); - } - - /** - * 位图法合并 - * @param array1 - * @param array2 - * @return - */ - public int[] merge2(int[] array1, int[] array2){ - - int bitSize = 0; - int a = array1[array1.length-1] ; - int b = array2[array2.length-1]; - bitSize =(a>b)?a:b; - boolean[] bitmap = new boolean[bitSize+1]; - for (int i = 0; i < array1.length; i++) { - bitmap[array1[i]]=true; - } - for (int i = 0; i < array2.length; i++) { - bitmap[array2[i]]=true; - } - - ArrayList ls = new ArrayList(); - for (int i = 0; i < bitmap.length; i++) { - if(bitmap[i]==true){ - ls.add(i); - } - } - return objList2int(ls); - } - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - - int[] newArray = new int[oldArray.length+size]; - System.arraycopy(oldArray, 0,newArray , 0, oldArray.length); - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - - int[] array = {}; - if(max <= 1)return array; - //生成 斐波那契数列的ArrayList集合 - ArrayList ls = new ArrayList(); - ls.add(1);ls.add(1); - int next;int i = 1; - while(true){ - next = (int)ls.get(i) +(int)ls.get(i-1); - if(next >= max){ - break; - } - ls.add(next); - i ++; - } - return objList2int(ls); - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - - ArrayList primesList = new ArrayList(); - boolean flag; - for (int i = 2; i < max; i++) { - flag = false; - for (int j = 2; j <= Math.sqrt(i); j++) { - if (i % j == 0) { - flag =true; - break; - } - } - if(!flag){ - primesList.add(i); - } - } - return objList2int(primesList); - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - - int temp; - ArrayList perfectList = new ArrayList(); - for (int i = 6; i <= max; i++) { - temp = 0; - for (int j = 1; j <= (i/2); j++) { - if(i%j == 0){ - temp += j; - } - } - if(temp == i){ - perfectList.add(i); - } - } - return objList2int(perfectList); - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - - StringBuilder str = new StringBuilder(); - for (int i = 0; i < array.length; i++) { - str.append(array[i]+seperator); - } - return str.substring(0, str.lastIndexOf(seperator)); - } - - /** - * 将存储int数据的ArrayList转换为int数组 - * @param ls - * @return - */ - public int[] objList2int(ArrayList ls){ - - Object[] objArr = ls.toArray(); - int[] array = new int[ls.size()]; - for (int i = 0; i < ls.size(); i++) { - array[i] = (int) objArr[i]; - } - return array; - } - -} diff --git a/group12/2258659044/zj-2017/src/com/coderising/download/DownloadThread.java b/group12/2258659044/zj-2017/src/com/coderising/download/DownloadThread.java index b4d218399f..3c13facd32 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/download/DownloadThread.java +++ b/group12/2258659044/zj-2017/src/com/coderising/download/DownloadThread.java @@ -1,8 +1,6 @@ package com.coderising.download; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; @@ -31,35 +29,6 @@ public DownloadThread(String downloadPath, Connection conn, int startPos, this.endPos = endPos; } - /** - * 这种操作存在弊端, - * 若文件过大,调用conn.read读取过程中程序中断 - * 将无法缓存任何数据 - */ - /*public void run() { - - try { - - //请求服务器下载部分文件 指定文件的位置 读取指定位子的字节 - byte[] buffer = conn.read(startPos, endPos); - //随机访问文件流 - RandomAccessFile raf = new RandomAccessFile(tempFile, "rwd"); - //随机写文件的时候从哪个位置开始写 - raf.seek(startPos);//定位文件 - //写文件 - raf.write(buffer); - raf.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (conn != null) { - conn.close(); - } - } - }*/ - public void run() { try { @@ -75,8 +44,8 @@ public void run() { tempFile = file; //获取指定文件段的下载流 InputStream in = conn.getDownloadStream(startPos, endPos); - if(in == null){ - return; + if(in == null){//重新请求连接 + run(); } //随机访问文件流 RandomAccessFile raf = new RandomAccessFile(tempFile, "rwd"); @@ -90,10 +59,9 @@ public void run() { downloadSize += length; } raf.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + } catch (Exception e) { + run(); + e.printStackTrace(); } finally { if (conn != null) { conn.close(); diff --git a/group12/2258659044/zj-2017/src/com/coderising/download/FileDownloader.java b/group12/2258659044/zj-2017/src/com/coderising/download/FileDownloader.java index cb84d148c7..721f2c77e0 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/download/FileDownloader.java +++ b/group12/2258659044/zj-2017/src/com/coderising/download/FileDownloader.java @@ -31,18 +31,7 @@ public void execute(){ conn = cm.open(this.url); int length = conn.getContentLength(); //分配下载块 - int blockSize = length / threadNum; - DownloadThread[] threads = new DownloadThread[threadNum]; - for (int thread = 1; thread <= threadNum; thread++) { - int startIndex = (thread - 1) * blockSize; - int endIndex = thread * blockSize-1; - if (thread == threadNum) {//最后一个线程下载的长度 - endIndex = length; - } - DownloadThread thr = new DownloadThread(downloadPath,cm.open(this.url),startIndex,endIndex); - threads[thread-1] = thr; - thr.start(); - } + DownloadThread[] threads = assignDownloadPart(length); //判断所有线程是否下载完成 new NotifyCaller(listener,threads,length).start(); @@ -54,6 +43,29 @@ public void execute(){ } } } + + /** + * 分配下载块并启动下载 + * @param length + * @return + * @throws ConnectionException + */ + private DownloadThread[] assignDownloadPart(int length) + throws ConnectionException { + int blockSize = length / threadNum; + DownloadThread[] threads = new DownloadThread[threadNum]; + for (int thread = 1; thread <= threadNum; thread++) { + int startIndex = (thread - 1) * blockSize; + int endIndex = thread * blockSize-1; + if (thread == threadNum) {//最后一个线程下载的长度 + endIndex = length; + } + DownloadThread thr = new DownloadThread(downloadPath,cm.open(this.url),startIndex,endIndex); + threads[thread-1] = thr; + thr.start(); + } + return threads; + } public void setListener(DownloadListener listener) { this.listener = listener; diff --git a/group12/2258659044/zj-2017/src/com/coderising/download/NotifyCaller.java b/group12/2258659044/zj-2017/src/com/coderising/download/NotifyCaller.java index 03f4149688..df49a92a07 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/download/NotifyCaller.java +++ b/group12/2258659044/zj-2017/src/com/coderising/download/NotifyCaller.java @@ -65,7 +65,7 @@ private String getDownloadSpeed(int timeDiff){ if(num==null||num.isEmpty()){ num = "0"; } - return num+"M/s"; + return num+"Mb/s"; } /** diff --git a/group12/2258659044/zj-2017/src/com/coderising/download/impl/ConnectionImpl.java b/group12/2258659044/zj-2017/src/com/coderising/download/impl/ConnectionImpl.java index ec8e503fe9..8f5a0a8757 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group12/2258659044/zj-2017/src/com/coderising/download/impl/ConnectionImpl.java @@ -7,7 +7,7 @@ import com.coderising.download.api.Connection; -public class ConnectionImpl implements Connection{ +class ConnectionImpl implements Connection{ /*http连接*/ private HttpURLConnection httpConnection; @@ -18,19 +18,8 @@ public class ConnectionImpl implements Connection{ @Override public byte[] read(int startPos, int endPos) throws IOException { - byte[] data = null; InputStream is = getDownloadStream(startPos,endPos); - if(is !=null){ - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length = -1; - while ((length = is.read(buffer)) != -1) { - baos.write(buffer, 0, length); - } - baos.flush(); - data = baos.toByteArray(); - } - return data; + return inputStremCovertToArray(is); } @Override @@ -53,6 +42,27 @@ public void close() { httpConnection.disconnect(); } + /** + * 将输入流转换为byte数组 + * @param is + * @return + * @throws IOException + */ + private byte[] inputStremCovertToArray(InputStream is) throws IOException{ + + byte[] data = null; + if(is !=null){ + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = -1; + while ((length = is.read(buffer)) != -1) { + baos.write(buffer, 0, length); + } + baos.flush(); + data = baos.toByteArray(); + } + return data; + } public void setHttpConnection(HttpURLConnection httpConnection) { this.httpConnection = httpConnection; } diff --git a/group12/2258659044/zj-2017/src/com/coderising/jvm/loader/ClassFileLoader.java b/group12/2258659044/zj-2017/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..e005c00c5c --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,33 @@ +package com.coderising.jvm.loader; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + File clzFile = ClassFileLoaderUtil.getClzFile(clzPaths,className); + + return ClassFileLoaderUtil.readClz(clzFile); + + } + + public void addClassPath(String path) { + + this.clzPaths.add(path); + } + + public String getClassPath(){ + + StringBuffer buff = new StringBuffer(); + for (String str : clzPaths) { + buff.append(str+";"); + } + return buff.substring(0, buff.length()-1); + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coderising/jvm/loader/ClassFileLoaderUtil.java b/group12/2258659044/zj-2017/src/com/coderising/jvm/loader/ClassFileLoaderUtil.java new file mode 100644 index 0000000000..254490ed42 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coderising/jvm/loader/ClassFileLoaderUtil.java @@ -0,0 +1,71 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +public class ClassFileLoaderUtil { + + /** + * 根据类完整包名与classPath获取类class文件 + * @return + */ + public static File getClzFile(List clzPaths ,String className){ + + File clazFile = null; + //将com.zj.className 转化为 com\\zj\\className.class; + if(className!=null){ + className = className.replace(".", "\\")+".class"; + } + //寻找文件所在目录 + for (String clzPath : clzPaths) { + clazFile = new File(clzPath+"\\"+className); + if(clazFile.exists()){ + break; + } + } + return clazFile; + } + + /** + * 读取文件并返回该文件的字节数组 + * @param clzFile + * @return + */ + public static byte[] readClz(File file){ + + byte[] data = null; + InputStream is = null; + ByteArrayOutputStream baos = null; + try { + is = new FileInputStream(file); + if(is !=null){ + baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = -1; + while ((length = is.read(buffer)) != -1) { + baos.write(buffer, 0, length); + } + baos.flush(); + data = baos.toByteArray(); + } + } catch (Exception e) { + e.printStackTrace(); + }finally{ + try { + if(baos!=null){ + baos.close(); + } + if(is!=null){ + is.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return data; + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java deleted file mode 100644 index f1fbf7e8b1..0000000000 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - /*扩容因子*/ - private static final int GENE = 10; - - private Object[] elementData = new Object[10]; - /*扩容引用*/ - private Object[] newElementData; - - public void add(Object o){ - grow(); - elementData[size] = o; - size ++; - } - public void add(int index, Object o){ - - if(index<0||index>size){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - grow(); - if(indexsize){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - return elementData[index]; - } - - public Object remove(int index){ - - Object o = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); - size --; - return o; - } - - public int size(){ - return size; - } - - public Object[] toArray(){ - Object[] objArr = new Object[size]; - System.arraycopy(elementData, 0, objArr, 0, size); - return objArr; - } - - /** - * 扩容,扩容因子为10 - */ - private void grow(){ - - if(size>=elementData.length){//长度不够需要扩容 - newElementData = new Object[size+GENE]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - elementData = newElementData; - } - } - - - public Iterator iterator(){ - - return new Itr(); - } - - private class Itr implements Iterator{ - - int cursor; - @Override - public boolean hasNext() { - return cursor != ArrayList.this.size; - } - - @Override - public Object next() { - - int i = this.cursor; - if (i >= ArrayList.this.size){ - throw new NoSuchElementException(); - } - this.cursor = (i + 1); - return ArrayList.this.elementData[i]; - } - - } -} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java index 3449517197..202aa4f54a 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java @@ -1,5 +1,7 @@ package com.coding.basic; +import com.coding.basic.array.ArrayList; + public class BinaryTree { //根节点 diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java b/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java deleted file mode 100644 index 3bf26a1a1c..0000000000 --- a/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,271 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private Node head; - - private int size = 0; - - public void add(Object o){ - - Node addNode = new Node(); - addNode.data = o; - if(size==0){ - head = addNode; - }else{ - //获取最后一个节点 - Node lastNode = getPointNode(size-1); - lastNode.next = addNode; - } - size++; - } - public void add(int index , Object o){ - - Node addNode = new Node(); - addNode.data = o; - if(index == 0){ //添加头结点 - addFirst(o); - }else if(index == size){//添加尾节点 - addLast(o); - }else{//在投节点与尾部添加节点 - Node prePointNode = getPointNode(index-1); - Node pointNode = prePointNode.next; - prePointNode.next = addNode; - addNode.next = pointNode; - size ++; - } - } - public Object get(int index){ - - Node node = getPointNode(index); - return node.data; - } - - public Object remove(int index){ - - Node pointNode = getPointNode(index); - Node nextPointNode = pointNode.next; - if(index ==0){ - head = nextPointNode; - }else{ - Node prePointNode = getPointNode(index-1); - prePointNode.next = nextPointNode; - } - size --; - return pointNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - - Node secondNode = head; - head = new Node(); - head.data = o; - if(size>0){ - head.next = secondNode; - } - size ++; - } - - public void addLast(Object o){ - add(o); - } - - public Object removeFirst(){ - - return remove(0); - } - - public Object removeLast(){ - - return remove(size-1); - } - public Iterator iterator(){ - return new Itr(); - } - - private class Itr implements Iterator{ - - int cursor; - @Override - public boolean hasNext() { - return cursor != LinkedList.this.size; - } - - @Override - public Object next() { - - int i = this.cursor; - if (i >= LinkedList.this.size){ - throw new NoSuchElementException(); - } - this.cursor = (i + 1); - return LinkedList.this.get(i); - } - - } - - /** - * 获取指定的节点 - * @return - */ - private Node getPointNode(int index){ - - if(index<0||index>size){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size+""); - } - Node node = head; - for (int i = 0; i < index; i++) { - node = node.next; - } - return node; - } - - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - Stack stack = new Stack(); - Node node; - //缓存原链表数据 - for (node = head; node!=null;node = node.next) { - stack.push(node.data); - } - //重新赋值 - for (node = head; node!=null;node = node.next) { - node.data = stack.pop(); - } - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf(){ - int newSize = size/2; - head = getPointNode(newSize); - size = size%2>0?newSize+1:newSize; - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - if(i==0){ - if(length101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - int[] array = new int[list.size()]; - for (int i = 0; i < array.length; i++) { - array[i] = (int) get((int)list.get(i)); - } - return array; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - * @param list - */ - - public void subtract(LinkedList list){ - for (int i = 0; i < size; i++) { - for (int j = 0; j < list.size(); j++) { - if(get(i).equals(list.get(j))){ - remove(i); - i--; - } - } - } - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - for (int i = 0; i < size-1; i++) { - if(get(i).equals(get(i+1))){ - remove(i); - i --; - } - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - for (int i = 0; i < size; i++) { - if((int)get(i)>min&&(int)get(i)size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + grow(); + if(indexsize){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + return elementData[index]; + } + + public Object remove(int index){ + + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + size --; + return o; + } + + public int size(){ + return size; + } + + public Object[] toArray(){ + Object[] objArr = new Object[size]; + System.arraycopy(elementData, 0, objArr, 0, size); + return objArr; + } + + /** + * 扩容,扩容因子为10 + */ + private void grow(){ + + if(size>=elementData.length){//长度不够需要扩容 + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } + } + + + public Iterator iterator(){ + + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= ArrayList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return ArrayList.this.elementData[i]; + } + + } +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/array/ArrayUtil.java b/group12/2258659044/zj-2017/src/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..9cb55f0f53 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,247 @@ +package com.coding.basic.array; + + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) { + temp[i] = origin[length-1-i]; + } + System.arraycopy(temp, 0, origin, 0, length); + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + + int length = oldArray.length; + int[] tempArr = new int[length]; + int j = 0; + int zeroNum = 0;//储存0的个数 + for (int i = 0; i < length; i++) { + if(oldArray[i]!=0){ + tempArr[j] = oldArray[i]; + j ++; + }else{ + zeroNum ++; + } + } + //删除数组尾端的0 + int[] newArray = new int[length-zeroNum]; + System.arraycopy(tempArr, 0, newArray, 0, length-zeroNum); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + int length1 = array1.length; + int length2 = array2.length; + int[] array3 = new int[length1 + length2]; + //将array1、array2的值加入array3中 + System.arraycopy(array1, 0, array3, 0, length1); + System.arraycopy(array2, 0, array3, length1, length2); + int length = array3.length; + int temp; + //将array3冒泡排序 + for (int i = 0; i < length; i++) { + for (int j = 0; j < length - i; j++) { + if(array3[i]>array3[j+i]){ + temp = array3[i]; + array3[i] = array3[j+i]; + array3[j+i] = temp; + } + } + } + return duplicate(array3); + } + + /** + *去重 + */ + private int[] duplicate(int[] array){ + + for (int i = 1; i < array.length; i++) { + if(array[i-1]==array[i]){ + array[i] = 0; + } + } + return removeZero(array); + } + + /** + * 位图法合并 + * @param array1 + * @param array2 + * @return + */ + public int[] merge2(int[] array1, int[] array2){ + + int bitSize = 0; + int a = array1[array1.length-1] ; + int b = array2[array2.length-1]; + bitSize =(a>b)?a:b; + boolean[] bitmap = new boolean[bitSize+1]; + for (int i = 0; i < array1.length; i++) { + bitmap[array1[i]]=true; + } + for (int i = 0; i < array2.length; i++) { + bitmap[array2[i]]=true; + } + + ArrayList ls = new ArrayList(); + for (int i = 0; i < bitmap.length; i++) { + if(bitmap[i]==true){ + ls.add(i); + } + } + return objList2int(ls); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + + int[] newArray = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0,newArray , 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + + int[] array = {}; + if(max <= 1)return array; + //生成 斐波那契数列的ArrayList集合 + ArrayList ls = new ArrayList(); + ls.add(1);ls.add(1); + int next;int i = 1; + while(true){ + next = (int)ls.get(i) +(int)ls.get(i-1); + if(next >= max){ + break; + } + ls.add(next); + i ++; + } + return objList2int(ls); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + ArrayList primesList = new ArrayList(); + boolean flag; + for (int i = 2; i < max; i++) { + flag = false; + for (int j = 2; j <= Math.sqrt(i); j++) { + if (i % j == 0) { + flag =true; + break; + } + } + if(!flag){ + primesList.add(i); + } + } + return objList2int(primesList); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + int temp; + ArrayList perfectList = new ArrayList(); + for (int i = 6; i <= max; i++) { + temp = 0; + for (int j = 1; j <= (i/2); j++) { + if(i%j == 0){ + temp += j; + } + } + if(temp == i){ + perfectList.add(i); + } + } + return objList2int(perfectList); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + str.append(array[i]+seperator); + } + return str.substring(0, str.lastIndexOf(seperator)); + } + + /** + * 将存储int数据的ArrayList转换为int数组 + * @param ls + * @return + */ + public int[] objList2int(ArrayList ls){ + + Object[] objArr = ls.toArray(); + int[] array = new int[ls.size()]; + for (int i = 0; i < ls.size(); i++) { + array[i] = (int) objArr[i]; + } + return array; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/linklist/LRUPageFrame.java b/group12/2258659044/zj-2017/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..b8a24f2f34 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,163 @@ +package com.coding.basic.linklist; + + +/** + * 用双向链表实现LRU算法 + * @author ZJ + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + Object obj; + + Node() { + } + } + + private int capacity;//容量 + + private int size;//已经存放的数量 + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(Object obj) { + + if(obj == null){ + return; + } + Node node = getNode(obj); + if(node!=null){ + move2head(node); + }else{ + refresh(obj); + } + } + + /** + * 刷新LRU队列 + * @param obj + */ + private void refresh(Object obj) { + //添加元素 + if(size0){ + head.next = secondNode; + } + size ++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + + return remove(0); + } + + public Object removeLast(){ + + return remove(size-1); + } + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != LinkedList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= LinkedList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return LinkedList.this.get(i); + } + + } + + /** + * 获取指定的节点 + * @return + */ + private Node getPointNode(int index){ + + if(index<0||index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size+""); + } + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + Stack stack = new Stack(); + Node node; + //缓存原链表数据 + for (node = head; node!=null;node = node.next) { + stack.push(node.data); + } + //重新赋值 + for (node = head; node!=null;node = node.next) { + node.data = stack.pop(); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + int newSize = size/2; + head = getPointNode(newSize); + size = size%2>0?newSize+1:newSize; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + if(i==0){ + if(length101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] array = new int[list.size()]; + for (int i = 0; i < array.length; i++) { + array[i] = (int) get((int)list.get(i)); + } + return array; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * @param list + */ + + public void subtract(LinkedList list){ + for (int i = 0; i < size; i++) { + for (int j = 0; j < list.size(); j++) { + if(get(i).equals(list.get(j))){ + remove(i); + i--; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + for (int i = 0; i < size-1; i++) { + if(get(i).equals(get(i+1))){ + remove(i); + i --; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + for (int i = 0; i < size; i++) { + if((int)get(i)>min&&(int)get(i)parameters){ if(actionName == null || parameters == null) return null; List actions = null; try { - File xmlfile = new File("D:\\5Java\\coding2017\\group12\\247565311\\week2\\struts.xml"); + File xmlfile = new File(System.getProperty("user.dir")+"\\bin\\week2\\struts.xml"); Document doc = new SAXReader().read(xmlfile); Element root = doc.getRootElement(); actions = root.elements(); diff --git a/group12/247565311/week3/DownloadThread.java b/group12/247565311/week3/DownloadThread.java index 3271ba2e99..2670bef00b 100644 --- a/group12/247565311/week3/DownloadThread.java +++ b/group12/247565311/week3/DownloadThread.java @@ -1,24 +1,31 @@ package week3; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; import week3.api.Connection; public class DownloadThread extends Thread{ Connection conn; + CyclicBarrier barrier; int startPos; int endPos; String path = ""; - public DownloadThread( Connection conn, int startPos, int endPos,String filepath){ + int step = 1024*200; // ÿ200kдһļ + public DownloadThread(CyclicBarrier _barrier, Connection conn, int startPos, int endPos,String filepath){ this.conn = conn; this.startPos = startPos; this.endPos = endPos; this.path = filepath; + this.barrier = _barrier; } public void run(){ // ȡصֽ飬дļעһ̳߳ @@ -27,27 +34,28 @@ public void run(){ // connectȡֽ飬ûֽˣͱʾⲿ // filepathдļ if(conn == null) return; - ByteBuffer buffer = ByteBuffer.allocate(endPos-startPos); - Path filepath = Paths.get(path); - - if(filepath == null) return; int curEndPos = startPos; - // while(curEndPos endPos) + curEndPos += step; + if (curEndPos > endPos) curEndPos = endPos; try { byte[] data = conn.read(startPos, curEndPos); - FileChannel channel = FileChannel.open(filepath,StandardOpenOption.WRITE); - // System.out.println("startPos"+startPos + ", length:"+data.length); - buffer.put(data); - channel.write(buffer); + RandomAccessFile files = new RandomAccessFile(path,"rw"); + files.seek(startPos); + files.write(data); + files.close(); + System.out.println("startPos"+startPos + ", length:"+data.length); } catch (IOException e) { - //e.printStackTrace(); - System.out.println("дļ"); + e.printStackTrace(); } - // } + } conn.close(); + try { + barrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } } } diff --git a/group12/247565311/week3/FileDownloader.java b/group12/247565311/week3/FileDownloader.java index c5ab5fb5d0..1a426c3682 100644 --- a/group12/247565311/week3/FileDownloader.java +++ b/group12/247565311/week3/FileDownloader.java @@ -1,6 +1,5 @@ package week3; - -import java.io.FileNotFoundException; +import java.util.concurrent.CyclicBarrier; import java.io.IOException; import java.io.RandomAccessFile; @@ -11,6 +10,7 @@ import week3.impl.ConnectionManagerImpl; public class FileDownloader { + private int MaxThreadNum = 4; private String url = null,path=null; DownloadListener listener = null; private ConnectionManager cm = new ConnectionManagerImpl(); @@ -20,7 +20,7 @@ public FileDownloader(String weburl,String localpath) { this.path = localpath; } - public void execute(){ + public void execute() throws InterruptedException{ // 在这里实现你的代码, 注意: 需要用多线程实现下载 // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) @@ -35,6 +35,11 @@ public void execute(){ // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 Connection conn = null; + CyclicBarrier barr= new CyclicBarrier(MaxThreadNum,new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); try { conn = cm.open(this.url); int length = conn.getContentLength(); @@ -43,18 +48,13 @@ public void execute(){ tarfile.setLength(length); tarfile.close(); Thread[] threads = new Thread[4]; - threads[0] = new DownloadThread(cm.open(this.url),0,length/4,path); - threads[1] = new DownloadThread(cm.open(this.url),length/4,length/2,path); - threads[2] = new DownloadThread(cm.open(this.url),length/2,3*length/4,path); - threads[3] = new DownloadThread(cm.open(this.url),3*length/4,length,path); + threads[0] = new DownloadThread(barr,cm.open(this.url),0,length/4,path); + threads[1] = new DownloadThread(barr,cm.open(this.url),length/4,length/2,path); + threads[2] = new DownloadThread(barr,cm.open(this.url),length/2,3*length/4,path); + threads[3] = new DownloadThread(barr,cm.open(this.url),3*length/4,length,path); for(int i=0;i<4;i++) threads[i].start(); - threads[0].join(); - threads[1].join(); - threads[2].join(); - threads[3].join(); - this.getListener().notifyFinished(); - } catch (ConnectionException | IOException | InterruptedException e) { + } catch (ConnectionException | IOException e) { e.printStackTrace(); }finally{ if(conn != null){ @@ -71,4 +71,7 @@ public void setConnectionManager(ConnectionManager ucm){ public DownloadListener getListener(){ return this.listener; } + public double getDownPercent(){ + return 0.0; + } } diff --git a/group12/247565311/week3/FileDownloaderTest.java b/group12/247565311/week3/FileDownloaderTest.java index 96893b71e9..3c729218d3 100644 --- a/group12/247565311/week3/FileDownloaderTest.java +++ b/group12/247565311/week3/FileDownloaderTest.java @@ -19,8 +19,8 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "https://edmullen.net/test/rc.jpg"; - String path = "D:\\hellp.jpg"; + String url = "http://music.163.com/api/pc/download/latest"; + String path = "D:\\hellp.exe"; FileDownloader downloader = new FileDownloader(url,path); ConnectionManager cm = new ConnectionManagerImpl(); downloader.setConnectionManager(cm); @@ -30,16 +30,21 @@ public void notifyFinished() { downloadFinished = true; } }); - downloader.execute(); - // 等待多线程下载程序执行完毕 + double time = 0; + try { + downloader.execute(); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } while (!downloadFinished) { try { - System.out.println("还没有下载完成,休眠五秒"); - Thread.sleep(5000);//休眠5秒 + Thread.sleep(100);//休眠0.1秒 + + time += 1; } catch (InterruptedException e) { e.printStackTrace(); } } - System.out.println("下载完成!"); + System.out.println("下载完成!耗时:"+time/10.0+" 秒。"); } } diff --git a/group12/247565311/week3/impl/ConnectionImpl.java b/group12/247565311/week3/impl/ConnectionImpl.java index bdd28aa4ec..7173caf56d 100644 --- a/group12/247565311/week3/impl/ConnectionImpl.java +++ b/group12/247565311/week3/impl/ConnectionImpl.java @@ -1,43 +1,65 @@ package week3.impl; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; import week3.api.Connection; public class ConnectionImpl implements Connection{ - HttpURLConnection conn = null; + URL url = null; + public ConnectionImpl(String str){ + try { + url = new URL(str); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } // Ҫִأȡֽ // ฺ򿪡ر @Override - public byte[] read(int startPos, int endPos) throws IOException { - if(conn == null || startPos>=endPos) return null; + public byte[] read(int startPos, int endPos) { + HttpURLConnection conn = null; byte[]res = null; - conn.setRequestProperty("Range","bytes="+startPos+"-"+endPos); - int responcode = conn.getResponseCode(); - if(200 < responcode && responcode < 300){ - InputStream input = conn.getInputStream(); - res = new byte[endPos-startPos]; - input.read(res); - input.close(); + try { + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestProperty("Range","bytes="+startPos+"-"+endPos); + int responcode = conn.getResponseCode(); + if(200 < responcode && responcode < 300){ + InputStream input = conn.getInputStream(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + res = new byte[2048]; + while(output.size()0)output.write(res,0,len); + else break; + } + return Arrays.copyOf(output.toByteArray(), endPos-startPos); + } + } catch (IOException e) { + e.printStackTrace(); } - conn.disconnect(); + if(conn!=null) conn.disconnect(); return res; } @Override public int getContentLength() { - if(conn == null) return 0; - return conn.getContentLength(); + try{ + URLConnection con = url.openConnection(); + return con.getContentLength(); + }catch(Exception e){ + e.printStackTrace(); + } + return -1; } @Override public void close() { - if(conn == null) return; - conn.disconnect(); - } - public void setConn(HttpURLConnection urlconn) { - conn = urlconn; } } diff --git a/group12/247565311/week3/impl/ConnectionManagerImpl.java b/group12/247565311/week3/impl/ConnectionManagerImpl.java index 68e458b1ca..b4a7cf381f 100644 --- a/group12/247565311/week3/impl/ConnectionManagerImpl.java +++ b/group12/247565311/week3/impl/ConnectionManagerImpl.java @@ -13,16 +13,6 @@ public class ConnectionManagerImpl implements ConnectionManager { ConnectionImpl conImpl = null; @Override public Connection open(String url) throws ConnectionException { - try { - URL urllink = new URL(url); - conImpl = new ConnectionImpl(); - HttpURLConnection httpconn = (HttpURLConnection)urllink.openConnection(); - httpconn.setConnectTimeout(5*1000); - httpconn.setRequestProperty("User-Agent","Mozilla/4.0 (compatiable; MSIE 5.0; Windows NT; DigExt)"); // ģ - conImpl.setConn(httpconn); - } catch (Exception e) { - throw (ConnectionException)e; - } - return conImpl; + return new ConnectionImpl(url); } } diff --git a/group12/247565311/week5/ClassFileLoader.java b/group12/247565311/week5/ClassFileLoader.java new file mode 100644 index 0000000000..fc5e920f19 --- /dev/null +++ b/group12/247565311/week5/ClassFileLoader.java @@ -0,0 +1,63 @@ +package week5; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws Exception { + for(String s:clzPaths){ + String filename = s+className+".class"; + File file = new File(filename); + if(file.exists())return loadClassFile(filename); + } + return null; + } + + private byte[] loadClassFile(String clzFileName) throws Exception { + File file = new File(clzFileName); + long filelength = file.length(); + byte[]res = null; + if(filelength>Integer.MAX_VALUE)throw new IOException("ļ"); + try { + FileInputStream fileinput = new FileInputStream(file); + res = new byte[(int) filelength]; + int offset=0,length=0; + while(offset-1)) + offset += length; + fileinput.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return res; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath_V1(){ + String res = ""; + int size = clzPaths.size(); + for(int i=0;i lib = new HashSet(); + class Node{ + int val; + Node next; + public Node(int _val){ + val = _val; + next = null; + } + } + public LRU(int _size){ + if(_size>0) { + this.size = _size; + } + } + public int[] getAll(){ + int length = lib.size(),index = 0; + int []res = new int[length]; + Node p = head.next; + while(p!=null){ + res[index] = p.val; + index += 1; + p = p.next; + } + return res; + } + public void add(int e){ + int index = 0; + if(lib.contains(e)){ + Node p = head; + while(p.next!= null){ + if(p.next.val == e){ + Node newn = p.next; + p.next = newn.next; + newn.next = head.next; + head.next = newn; + break; + } + p = p.next; + } + }else{ + if(lib.size() == size){ + lib.add(e); + Node newn = new Node(e); + newn.next = head.next; + head.next = newn; + Node p = head; + while(p.next.next != null) + p = p.next; + Node deln = p.next; + lib.remove(deln.val); + p.next = null; + }else{ + Node newn = new Node(e); + newn.next = head.next; + head.next = newn; + lib.add(e); + } + } + } +} diff --git a/group12/247565311/week5/LRUTest.java b/group12/247565311/week5/LRUTest.java new file mode 100644 index 0000000000..0ee0d95309 --- /dev/null +++ b/group12/247565311/week5/LRUTest.java @@ -0,0 +1,36 @@ +package week5; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LRUTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAdd() { + LRU lru = new LRU(5); + lru.add(3); + lru.add(7); + lru.add(5); + lru.add(8); + lru.add(10); + Assert.assertArrayEquals(new int[]{10,8,5,7,3}, lru.getAll()); + lru.add(5); + lru.add(3); + Assert.assertArrayEquals(new int[]{3,5,10,8,7}, lru.getAll()); + lru.add(8); + lru.add(11); + Assert.assertArrayEquals(new int[]{11,8,3,5,10}, lru.getAll()); + } +} diff --git a/group12/349166103/LRUPageFrame.java b/group12/349166103/LRUPageFrame.java new file mode 100644 index 0000000000..4dda68632d --- /dev/null +++ b/group12/349166103/LRUPageFrame.java @@ -0,0 +1,108 @@ + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node(int Num) { + pageNum=Num; + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + private int nodeNum = 0; + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + int isPageNum = isContained(pageNum); + if(isPageNum!=-1){ + int index = 0; + Node node = first; + while(index!=isPageNum){ // move to the Node + node=node.next; + index++; + } + if(index != 0){ + if(node.next == null){ + node.prev.next = null; + }else{ + node.prev.next = node.next; + } + node.next = first; + first = node; + } + }else{ + if(first != null){ + Node tmp = new Node(pageNum); + tmp.next = first; + first.prev = tmp; + first = tmp; + nodeNum++; + if(nodeNum > capacity){ + Node node = first; + for(int i=0;i1.6.1 + + org.apache.commons + commons-lang3 + 3.0 + + + + commons-io + commons-io + 2.4 + diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayList.java b/group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayList.java new file mode 100644 index 0000000000..d5c5fe04cc --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayList.java @@ -0,0 +1,173 @@ +package com.zhaogd.array; + +import java.util.Arrays; + +import com.zhaogd.collection.Iterator; +import com.zhaogd.collection.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * 按下标顺序新增元素 + * + * @Method add + * @param o + * @see com.zhaogd.collection.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size] = o; + size++; + } + + /** + * 在指定下标位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.zhaogd.collection.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkRangeForAdd(index); + + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + /** + * 根据下标获取列表数据 + * + * @Method get + * @param index + * @return + * @see com.zhaogd.collection.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkRangeForGetOrRemove(index); + + return elementData[index]; + } + + /** + * 根据下标移除元素,并返回移除的元素值 + * + * @Method remove + * @param index + * @return + * @see com.zhaogd.collection.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkRangeForGetOrRemove(index); + + Object oldValue = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + + size--; + elementData[size] = null; + + return oldValue; + } + + /** + * 获得列表长度 + * + * @Method size + * @return + * @see com.zhaogd.collection.guodong.datastructure.List#size() + */ + public int size() { + return size; + } + + /** + * 获取ArrayList的迭代器 + * + * @MethodName iterator + * @author zhaogd + * @date 2017年2月21日 下午8:19:28 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + /** + * 确保数组容量足够,如果不够则扩充数组 + * + * @MethodName ensureCapacityInternal + * @author zhaogd + * @date 2017年2月21日 下午5:06:46 + * @param minCapacity + */ + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity > elementData.length) { + grow(minCapacity); + } + } + + /** + * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 + * + * @MethodName grow + * @author zhaogd + * @date 2017年2月21日 下午5:20:55 + * @param minCapacity + */ + private void grow(int minCapacity) { + minCapacity = elementData.length + elementData.length / 2; + elementData = Arrays.copyOf(elementData, minCapacity); + } + + /** + * 检查Add方法的下标范围是否合法 + * + * @MethodName checkRangeForAdd + * @author zhaogd + * @date 2017年2月21日 下午7:32:55 + * @param index + */ + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查Get或者Remove方法的下标范围是否合法 + * + * @MethodName checkRangeForGetOrRemove + * @author zhaogd + * @date 2017年2月21日 下午7:33:21 + * @param index + */ + private void checkRangeForGetOrRemove(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private class ArrayListIterator implements Iterator { + + private int lastIndex = 0; + + @Override + public boolean hasNext() { + return lastIndex < size; + } + + @Override + public Object next() { + return elementData[lastIndex++]; + } + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java deleted file mode 100644 index d3afc5e01a..0000000000 --- a/group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java +++ /dev/null @@ -1,170 +0,0 @@ -package com.zhaogd.collection; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - /** - * 按下标顺序新增元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - ensureCapacityInternal(size + 1); - elementData[size] = o; - size++; - } - - /** - * 在指定下标位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkRangeForAdd(index); - - ensureCapacityInternal(size + 1); - - System.arraycopy(elementData, index, elementData, index + 1, size - index); - - elementData[index] = o; - size++; - } - - /** - * 根据下标获取列表数据 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkRangeForGetOrRemove(index); - - return elementData[index]; - } - - /** - * 根据下标移除元素,并返回移除的元素值 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkRangeForGetOrRemove(index); - - Object oldValue = elementData[index]; - - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - - size--; - elementData[size] = null; - - return oldValue; - } - - /** - * 获得列表长度 - * - * @Method size - * @return - * @see com.guodong.datastructure.List#size() - */ - public int size() { - return size; - } - - /** - * 获取ArrayList的迭代器 - * - * @MethodName iterator - * @author zhaogd - * @date 2017年2月21日 下午8:19:28 - * @return - */ - public Iterator iterator() { - return new ArrayListIterator(); - } - - /** - * 确保数组容量足够,如果不够则扩充数组 - * - * @MethodName ensureCapacityInternal - * @author zhaogd - * @date 2017年2月21日 下午5:06:46 - * @param minCapacity - */ - private void ensureCapacityInternal(int minCapacity) { - if (minCapacity > elementData.length) { - grow(minCapacity); - } - } - - /** - * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 - * - * @MethodName grow - * @author zhaogd - * @date 2017年2月21日 下午5:20:55 - * @param minCapacity - */ - private void grow(int minCapacity) { - minCapacity = elementData.length + elementData.length / 2; - elementData = Arrays.copyOf(elementData, minCapacity); - } - - /** - * 检查Add方法的下标范围是否合法 - * - * @MethodName checkRangeForAdd - * @author zhaogd - * @date 2017年2月21日 下午7:32:55 - * @param index - */ - private void checkRangeForAdd(int index) { - if (index > size || index < 0) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查Get或者Remove方法的下标范围是否合法 - * - * @MethodName checkRangeForGetOrRemove - * @author zhaogd - * @date 2017年2月21日 下午7:33:21 - * @param index - */ - private void checkRangeForGetOrRemove(int index) { - if (index >= size || index < 0) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - private class ArrayListIterator implements Iterator { - - private int lastIndex = 0; - - @Override - public boolean hasNext() { - return lastIndex < size; - } - - @Override - public Object next() { - return elementData[lastIndex++]; - } - } -} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java deleted file mode 100644 index 136f53284e..0000000000 --- a/group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java +++ /dev/null @@ -1,362 +0,0 @@ -package com.zhaogd.collection; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private int size; - - private Node head; - - private Node last; - - /** - * 向 链表尾端插入元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - linkLast(o); - } - - /** - * 向链表指定位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkIndexForAdd(index); - - if (index == size) { - linkLast(o); - } else { - Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 - Node currentNode = getNodeByIndex(index); // 取到当前下标节点 - Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 - - if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 - head = newNode; - } else { - prevNode.next = newNode; - } - size++; - } - } - - /** - * 根据下标获取链表中元素 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkIndexForGet(index); - return getNodeByIndex(index).data; - } - - public Object getLast() { - return last.data; - } - - /** - * 根据下标移除链表元素 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkIndexForGet(index); - - Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 - Node currentNode = null; - if (prevNode == null) { - currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 - head = currentNode.next; - } else { - currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 - prevNode.next = currentNode.next; - } - Node nextNode = currentNode.next; - - if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 - last = prevNode; - } else { - currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 - } - Object data = currentNode.data; - currentNode.data = null; // 清空当前节点的值,等待垃圾回收 - - size--; - - return data; - } - - /** - * 返回List长度 - */ - public int size() { - return size; - } - - /** - * 向列表头部添加元素 - * - * @param o - */ - public void addFirst(Object o) { - Node n = head; - Node newNode = new Node(o, n); - - head = newNode; - if (n == null) { - last = newNode; - } - size++; - } - - /** - * 向列表尾部添加元素 - * - * @param o - */ - public void addLast(Object o) { - linkLast(o); - } - - /** - * 移除链表第一个元素 - * - * @return - */ - public Object removeFirst() { - Node n = head; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node nextNode = n.next; - - n.data = null; - n.next = null; - - head = nextNode; - if (nextNode == null) { - last = null; - } - - size--; - return data; - } - - public Object removeLast() { - Node n = last; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node prevNode = getNodeByIndex(size - 2); - n.data = null; - if (prevNode == null) { - head = null; - } else { - prevNode.next = null; - } - last = prevNode; - - size--; - return data; - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } - - - /** - * 根据下标获取对应的节点 - * - * @MethodName getNodeByIndex - * @author zhaogd - * @date 2017年2月23日 下午3:32:48 - * @param index - * @return - */ - private Node getNodeByIndex(int index) { - if (index < 0) { - return null; - } - Node n = head; - for (int i = 0; i < index; i++) { - n = n.next; - } - return n; - } - - /** - * 在链表尾端插入节点 - * - * @MethodName linkLast - * @author zhaogd - * @date 2017年2月23日 下午3:14:28 - * @param o - */ - private void linkLast(Object o) { - Node n = last; // 取出原尾端数据 - Node newNode = new Node(o, null); // 创建新节点 - last = newNode; // 把新节点放入链表尾端 - // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 - // 如果不为空,把原尾端节点指向新节点 - if (n == null) { - head = newNode; - } else { - n.next = newNode; - } - - size++; - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForAdd - * @author zhaogd - * @date 2017年2月23日 下午3:05:07 - * @param index - */ - private void checkIndexForAdd(int index) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForGet - * @author zhaogd - * @date 2017年2月23日 下午4:21:35 - * @param index - */ - private void checkIndexForGet(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - public Iterator iterator() { - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { - private Node current; - - private int index; - - @Override - public boolean hasNext() { - return index < size; - } - - @Override - public Object next() { - if (current == null) { - current = getNodeByIndex(index); - } - Object data = current.data; - current = current.next; - index++; - return data; - } - } -} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java index d4a0647ab6..b81d015351 100644 --- a/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java @@ -1,5 +1,7 @@ package com.zhaogd.collection; +import com.zhaogd.collection.linkedlist.LinkedList; + public class Queue { private LinkedList element = new LinkedList(); diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java deleted file mode 100644 index afb01f5f92..0000000000 --- a/group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.zhaogd.collection; - -public class Stack { - private LinkedList elementData = new LinkedList(); - - public void push(Object o) { - elementData.addLast(o); - } - - public Object pop() { - return elementData.removeLast(); - } - - public Object peek() { - return elementData.getLast(); - } - - public boolean isEmpty() { - return elementData.size() == 0; - } - - public int size() { - return elementData.size(); - } -} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrame.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrame.java new file mode 100644 index 0000000000..39a4e4063d --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrame.java @@ -0,0 +1,164 @@ +package com.zhaogd.collection.linkedlist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + + moveExistingNodeToHead(node); + + } else{ + + node = new Node(); + node.pageNum = pageNum; + + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + + + + } + } + + private void addNewNodetoHead(Node node) { + + if(isEmpty()){ + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + + } + + + + + + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrameTest.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrameTest.java new file mode 100644 index 0000000000..b2fc8c8323 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.zhaogd.collection.linkedlist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LinkedList.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LinkedList.java new file mode 100644 index 0000000000..78721b060a --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/linkedlist/LinkedList.java @@ -0,0 +1,365 @@ +package com.zhaogd.collection.linkedlist; + +import java.util.NoSuchElementException; + +import com.zhaogd.collection.Iterator; +import com.zhaogd.collection.List; + +public class LinkedList implements List { + + private int size; + + private Node head; + + private Node last; + + /** + * 向 链表尾端插入元素 + * + * @Method add + * @param o + * @see com.zhaogd.collection.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + linkLast(o); + } + + /** + * 向链表指定位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.zhaogd.collection.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkIndexForAdd(index); + + if (index == size) { + linkLast(o); + } else { + Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 + + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 + head = newNode; + } else { + prevNode.next = newNode; + } + size++; + } + } + + /** + * 根据下标获取链表中元素 + * + * @Method get + * @param index + * @return + * @see com.zhaogd.collection.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkIndexForGet(index); + return getNodeByIndex(index).data; + } + + public Object getLast() { + return last.data; + } + + /** + * 根据下标移除链表元素 + * + * @Method remove + * @param index + * @return + * @see com.zhaogd.collection.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkIndexForGet(index); + + Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 + Node currentNode = null; + if (prevNode == null) { + currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 + head = currentNode.next; + } else { + currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 + prevNode.next = currentNode.next; + } + Node nextNode = currentNode.next; + + if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 + last = prevNode; + } else { + currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 + } + Object data = currentNode.data; + currentNode.data = null; // 清空当前节点的值,等待垃圾回收 + + size--; + + return data; + } + + /** + * 返回List长度 + */ + public int size() { + return size; + } + + /** + * 向列表头部添加元素 + * + * @param o + */ + public void addFirst(Object o) { + Node n = head; + Node newNode = new Node(o, n); + + head = newNode; + if (n == null) { + last = newNode; + } + size++; + } + + /** + * 向列表尾部添加元素 + * + * @param o + */ + public void addLast(Object o) { + linkLast(o); + } + + /** + * 移除链表第一个元素 + * + * @return + */ + public Object removeFirst() { + Node n = head; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node nextNode = n.next; + + n.data = null; + n.next = null; + + head = nextNode; + if (nextNode == null) { + last = null; + } + + size--; + return data; + } + + public Object removeLast() { + Node n = last; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node prevNode = getNodeByIndex(size - 2); + n.data = null; + if (prevNode == null) { + head = null; + } else { + prevNode.next = null; + } + last = prevNode; + + size--; + return data; + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + + /** + * 根据下标获取对应的节点 + * + * @MethodName getNodeByIndex + * @author zhaogd + * @date 2017年2月23日 下午3:32:48 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + if (index < 0) { + return null; + } + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + /** + * 在链表尾端插入节点 + * + * @MethodName linkLast + * @author zhaogd + * @date 2017年2月23日 下午3:14:28 + * @param o + */ + private void linkLast(Object o) { + Node n = last; // 取出原尾端数据 + Node newNode = new Node(o, null); // 创建新节点 + last = newNode; // 把新节点放入链表尾端 + // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 + // 如果不为空,把原尾端节点指向新节点 + if (n == null) { + head = newNode; + } else { + n.next = newNode; + } + + size++; + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForAdd + * @author zhaogd + * @date 2017年2月23日 下午3:05:07 + * @param index + */ + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForGet + * @author zhaogd + * @date 2017年2月23日 下午4:21:35 + * @param index + */ + private void checkIndexForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current; + + private int index; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (current == null) { + current = getNodeByIndex(index); + } + Object data = current.data; + current = current.next; + index++; + return data; + } + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/Stack.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/Stack.java new file mode 100644 index 0000000000..85a8800f39 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/Stack.java @@ -0,0 +1,27 @@ +package com.zhaogd.collection.stack; + +import com.zhaogd.collection.linkedlist.LinkedList; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public void push(Object o) { + elementData.addLast(o); + } + + public Object pop() { + return elementData.removeLast(); + } + + public Object peek() { + return elementData.getLast(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/StackUtil.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/StackUtil.java new file mode 100644 index 0000000000..ecf128b27e --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/stack/StackUtil.java @@ -0,0 +1,45 @@ +package com.zhaogd.collection.stack; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + return null; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + return false; + } + + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/AccessFlag.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..1557b0ead2 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package com.zhaogd.jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + +} \ No newline at end of file diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/ClassFile.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..bf6abbe3fd --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/ClassFile.java @@ -0,0 +1,75 @@ +package com.zhaogd.jvm.clz; + +import com.zhaogd.jvm.constant.ClassInfo; +import com.zhaogd.jvm.constant.ConstantPool; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + + System.out.println("Super Class Name:"+ getSuperClassName()); + + + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/ClassIndex.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..0bdf47d002 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package com.zhaogd.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ClassInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..9851e063c0 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ClassInfo.java @@ -0,0 +1,24 @@ +package com.zhaogd.jvm.constant; + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ConstantInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..0afd79256b --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.zhaogd.jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ConstantPool.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..4663d4d195 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/ConstantPool.java @@ -0,0 +1,29 @@ +package com.zhaogd.jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/FieldRefInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..bba61aea27 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.zhaogd.jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/MethodRefInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..61c965bc0a --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.zhaogd.jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/NameAndTypeInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..aef7f16d1e --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.zhaogd.jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/NullConstantInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..1cb411bee2 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.zhaogd.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/StringInfo.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..c58bcb59d7 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.zhaogd.jvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/UTF8Info.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..c8b2e6a4c0 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.zhaogd.jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/loader/ByteCodeIterator.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..7147060b84 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,5 @@ +package com.zhaogd.jvm.loader; + +public class ByteCodeIterator { + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/loader/ClassFileLoader.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..6d04787a99 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/loader/ClassFileLoader.java @@ -0,0 +1,140 @@ +package com.zhaogd.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.zhaogd.jvm.clz.ClassFile; + + + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + className = className.replace('.', File.separatorChar) +".class"; + + for(String path : this.clzPaths){ + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } + } + + return null; + + + + } + + private byte[] loadClassFile(String clzFileName) { + + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); + + } + + + + public String getClassPath(){ + return StringUtils.join(this.clzPaths,";"); + } + + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + + } + + + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/test/EmployeeV1.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..e9f656131b --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/test/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.zhaogd.jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/jvm/util/Util.java b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/util/Util.java new file mode 100644 index 0000000000..9c2fcc183e --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/jvm/util/Util.java @@ -0,0 +1,24 @@ +package com.zhaogd.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i MAX_CONNECTIONS) { diff --git a/group12/382266293/src/com/coderising/download/FileDownloader.java b/group12/382266293/src/com/coderising/download/FileDownloader.java index cc77b380a9..c756248bb1 100644 --- a/group12/382266293/src/com/coderising/download/FileDownloader.java +++ b/group12/382266293/src/com/coderising/download/FileDownloader.java @@ -44,7 +44,7 @@ public void execute() { try { Connection conn = cm.open(this.url); int length = conn.getContentLength(); - checkLength(length, conn); + System.out.println("file length:" + length); setLocation("C:\\"); @@ -52,7 +52,8 @@ public void execute() { String name = conn.getFileName(); setFileName(name); setTempName(name); - + checkLength(length, conn); + DownloadUtil.createTempFile(tempName, length); int connNumbers = DownloadUtil.calculateConnects(length); @@ -143,14 +144,17 @@ private void setAndStartThreadPool(Connection conn, DownloadThread[] threadPool, threadPool[0] = new DownloadThread(conn, beginPos, endPos); setAndStartThread(threadPool[0], tempName); for (int i = 1; i < connectionNumbers; i++) { - Connection con = cm.open(this.url); beginPos = endPos + 1; endPos = beginPos + batch_size; + Connection con = cm.open(this.url); + if (i == connectionNumbers - 1) { endPos = length - 1; } threadPool[i] = new DownloadThread(con, beginPos, endPos); setAndStartThread(threadPool[i], tempName); + + } } diff --git a/group12/382266293/src/com/coderising/download/impl/ConnectionImpl.java b/group12/382266293/src/com/coderising/download/impl/ConnectionImpl.java index 05f31103de..61ed430106 100644 --- a/group12/382266293/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group12/382266293/src/com/coderising/download/impl/ConnectionImpl.java @@ -33,17 +33,27 @@ public byte[] read(int startPos, int endPos) throws IOException { InputStream in = null; ByteArrayOutputStream out = null; try { + httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); in = httpConn.getInputStream(); out = new ByteArrayOutputStream(); in = httpConn.getInputStream(); - in.skip(startPos); - byte[] buffer = new byte[endPos-startPos + 1]; + //in.skip(startPos); + int len = 0; byte[] b = new byte[1024]; while((len = in.read(b)) != -1) { out.write(b, 0, len); } + int totalLen = endPos - startPos + 1; + + if (out.size() > totalLen) { + byte[] data = out.toByteArray(); + return data; + } + return out.toByteArray(); + } catch (IOException e) { e.printStackTrace(); } diff --git a/group12/382266293/src/com/coderising/jvm/loader/ClassFileLoader.java b/group12/382266293/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..81a79a1e04 --- /dev/null +++ b/group12/382266293/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,93 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + String clzFileName = "//" + className.replaceAll("\\.", "//") + ".class"; + return loadClassFile(clzFileName); + } + + @SuppressWarnings("resource") + private byte[] loadClassFile(String clzFileName) { + File classFile = getClassFile(clzFileName); + if (null == classFile) { + try { + throw new ClassNotFoundException(clzFileName + " does not exist."); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + RandomAccessFile raf = null; + ByteArrayOutputStream out = null; + try { + out = new ByteArrayOutputStream(); + raf = new RandomAccessFile(classFile, "r"); + int len = 0; + byte[] b = new byte[1024]; + while ((len = raf.read(b)) != -1) { + out.write(b, 0, len); + } + int totalLen = (int) classFile.length(); + + if (out.size() > totalLen) { + byte[] data = out.toByteArray(); + return data; + } + + return out.toByteArray(); + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + + } + + private File getClassFile(String clzFileName) { + + for (String path : clzPaths) { + File file = new File(path + "//" + clzFileName); + if (file.exists()) { + return file; + } + } + return null; + + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath_V1() { + + return null; + } + + public String getClassPath() { + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < clzPaths.size(); i++) { + + sb.append(clzPaths.get(i)); + if (i < clzPaths.size() - 1) { + sb.append(";"); + } + + } + + return sb.toString(); + } + +} diff --git a/group12/382266293/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group12/382266293/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..ab9f6f6d73 --- /dev/null +++ b/group12/382266293/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,80 @@ +package com.coderising.jvm.test; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + +public class ClassFileloaderTest { + + static String path1 = "C:\\Users\\Administrator\\git\\coding2017n\\group12\\382266293\\bin"; + static String path2 = "C:\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} diff --git a/group12/382266293/src/com/coderising/jvm/test/EmployeeV1.java b/group12/382266293/src/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..67735a92b0 --- /dev/null +++ b/group12/382266293/src/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group12/382266293/src/com/coderising/litestruts/LoginAction.java b/group12/382266293/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..76547ac3b3 --- /dev/null +++ b/group12/382266293/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/Configuration.java b/group12/382266293/src/litestruts/Configuration.java index 9b7d4e8466..4efedbce67 100644 --- a/group12/382266293/src/litestruts/Configuration.java +++ b/group12/382266293/src/litestruts/Configuration.java @@ -1,9 +1,9 @@ package litestruts; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; - import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; @@ -11,15 +11,13 @@ import static util.Print.*; public class Configuration { - - ActionCfg actionCfg = new ActionCfg(); - + Map actions = new HashMap<>(); + private static Configuration cfg = new Configuration(); private Configuration() { - + } - public static Configuration getNewInstance() { if (cfg == null) { @@ -28,75 +26,108 @@ public static Configuration getNewInstance() { return cfg; } - private String getFile(String fileName) { + public void parse(String fileName) { String src = this.getClass().getPackage().getName(); - return "file://" + src + "\\" + fileName + ".xml"; + String filepath = src.replace(".", "/") + "/" +fileName; + + InputStream is = this.getClass().getResourceAsStream("/" + filepath); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + + } + } - public void parseAction(String src) { - - String fileName = getFile(src); + public void parseXML(InputStream is) { + SAXBuilder reader = new SAXBuilder(); try { - Document doc = reader.build("C:\\struts.xml"); + Document doc = reader.build(is); Element root = doc.getRootElement(); for(Element element : root.getChildren("action")) { - String name = element.getAttributeValue("name"); + String actionName = element.getAttributeValue("name"); String clz = element.getAttributeValue("class"); - actionCfg.actionInfo.put(name, clz); + ActionCfg ac = new ActionCfg(actionName,clz); for(Element e : element.getChildren("result")) { String result = e.getAttributeValue("name"); String jsp = e.getText().trim(); - println("result:" + result + "jsp:" + jsp); - Map res = new HashMap<>(); - res.put(result, jsp); - actionCfg.resultInfo.put(name, res); + ac.addViewResult(result, jsp); } + + actions.put(actionName, ac); } } catch (JDOMException | IOException e) { e.printStackTrace(); } - + } + + + public String getClassName(String action) { + ActionCfg cfg = this.actions.get(action); + if (cfg == null) { + return null; + } + return cfg.getClassName(); + } + + + public String getResultView(String action, String resultName) { + ActionCfg cfg = this.actions.get(action); + if (cfg == null) { + return null; + } + return cfg.getViewResult().get(resultName); } public static void main(String[] args) { Configuration cfg = new Configuration(); - cfg.parseAction("struts"); - Map info = cfg.getActionInfo(); - Map result = cfg.getResultInfo().get("login"); - println(info); - println(result); + cfg.parse("struts.xml"); + String clz = cfg.getClassName("login"); + println(clz); + + } + private static class ActionCfg { - private Map actionInfo; - private Map> resultInfo; - public ActionCfg() { - this.actionInfo = new HashMap(); - this.resultInfo = new HashMap>(); + String name; + String clz; + Map viewResult = new HashMap<>(); + + public Map getViewResult() { + return viewResult; + } + + public ActionCfg(String name, String clz) { + this.name = name; + this.clz = clz; } - - } + public void addViewResult(String result, String jsp) { + viewResult.put(result, jsp); + + } - public Map> getResultInfo() { - - return actionCfg.resultInfo; - } + public String getClassName() { + return clz; + } - public Map getActionInfo() { - - return actionCfg.actionInfo; } - + + + diff --git a/group12/382266293/src/litestruts/ConfigurationTest.java b/group12/382266293/src/litestruts/ConfigurationTest.java index 1b5d2f43e1..e9d41a07c9 100644 --- a/group12/382266293/src/litestruts/ConfigurationTest.java +++ b/group12/382266293/src/litestruts/ConfigurationTest.java @@ -1,32 +1,54 @@ package litestruts; -import java.util.HashMap; -import java.util.Map; - import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; + + public class ConfigurationTest { + + Configuration cfg = Configuration.getNewInstance(); + + + @Before public void setUp() throws Exception { + + cfg.parse("struts.xml"); } @After public void tearDown() throws Exception { + } @Test - public void testGetGetterMethods() { - Configuration cfg = Configuration.getNewInstance(); - Map actionName = new HashMap<>(); - actionName.put("login","com.coderising.action.LoginAction"); - actionName.put("logout","com.coderising.action.LogoutAction"); + public void testGetClassName() { + + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView(){ + String jsp = cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); - //Assert.assertTrue(cfg.getActionName().containsKey(actionName)); + jsp = cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + jsp = cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + jsp = cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); } diff --git a/group12/382266293/src/litestruts/StrutsTest.java b/group12/382266293/src/litestruts/StrutsTest.java index 35686c8e30..4b59f846f5 100644 --- a/group12/382266293/src/litestruts/StrutsTest.java +++ b/group12/382266293/src/litestruts/StrutsTest.java @@ -1,17 +1,19 @@ package litestruts; -import java.beans.IntrospectionException; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.junit.Assert; import org.junit.Test; + + + + public class StrutsTest { @Test - public void testLoginActionSuccess() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public void testLoginActionSuccess() { String actionName = "login"; @@ -27,7 +29,7 @@ public void testLoginActionSuccess() throws InstantiationException, IllegalAcces } @Test - public void testLoginActionFailed() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public void testLoginActionFailed() { String actionName = "login"; Map params = new HashMap(); params.put("name","test"); @@ -38,4 +40,4 @@ public void testLoginActionFailed() throws InstantiationException, IllegalAccess Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); } -} \ No newline at end of file +} diff --git a/group12/382266293/src/litestruts/struts.xml b/group12/382266293/src/litestruts/struts.xml index fb0c2be3de..4c6eeabbd4 100644 --- a/group12/382266293/src/litestruts/struts.xml +++ b/group12/382266293/src/litestruts/struts.xml @@ -1,10 +1,10 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - + /jsp/welcome.jsp /jsp/error.jsp diff --git a/group12/446031103/src/com/coderising/array/ArrayUtil.java b/group12/446031103/src/com/coderising/array/ArrayUtil.java deleted file mode 100644 index a771999a22..0000000000 --- a/group12/446031103/src/com/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,202 +0,0 @@ -package com.coderising.array; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - int end = origin.length-1; - int temp ; - for (int i = 0; i < end; i++,end--) { - temp=origin[i]; - origin[i]=origin[end]; - origin[end] = temp; - } - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - int zeroCnt = 0; - for (int i : oldArray) { - if(0==i){ - zeroCnt++; - } - - } - int size = 0; - int [] result = new int[oldArray.length-zeroCnt]; - for (int i : oldArray) { - if(0!=i){ - result[size]=i; - size++; - } - - } - return result; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - //合拼数组,缺排序,缺去重 - int [] temp = new int[array1.length+array2.length]; - System.arraycopy(array1, 0, temp, 0, array1.length); - System.arraycopy(array2, 0, temp, array1.length, array2.length); - List resultList= new ArrayList(); - for (int i : temp) { - if(!resultList.contains(i)) - resultList.add(i); - }//已去重数组,缺排序 - int [] result = new int[resultList.size()]; - for (int i = 0; i < resultList.size(); i++) { - result[i] = resultList.get(i); - } - //冒泡排序 - for (int i = 0; i < result.length-1; i++) { - for (int j = 0; j < result.length-i-1; j++) { - if(result[j]>result[j+1]){ - int tempInt = result[j]; - result[j] =result[j+1]; - result[j+1] = tempInt; - } - } - - } - return result; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return Arrays.copyOf(oldArray, oldArray.length+size); - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - int first =0; - int second = 1; - List resultList= new ArrayList(); - if(max!=second) - add(first,second,max,resultList); - int [] result = new int[resultList.size()]; - for (int i = 0; i < resultList.size(); i++) { - result[i] = resultList.get(i); - } - return result; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - List resultList= new ArrayList(); - for (int i = 2; i < max; i++) { - boolean isAdd = true; - for (int j = 2; j < i; j++) { - if(0==i%j){ - isAdd = false; - break; - } - } - if(isAdd) - resultList.add(i); - } - int [] result = new int[resultList.size()]; - for (int i = 0; i < resultList.size(); i++) { - result[i] = resultList.get(i); - } - return result; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - List resultList= new ArrayList(); - for (int i = 1; i < max; i++) { - int temp = 0; - for (int j = 1; j < i; j++) { - if(0==i%j){ - temp+=j; - } - } - if(i==temp) - resultList.add(i); - } - int [] result = new int[resultList.size()]; - for (int i = 0; i < resultList.size(); i++) { - result[i] = resultList.get(i); - } - return result; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return Arrays.toString(array).replace("[", "").replace("]", "").replace(", ", seperator); - } - - /** - * - * @param number1 - * @param number2 - * @param max - * @param resultList - * @return - */ - public List add(int number1,int number2,int max,List resultList){ - if(number2 totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); } @Override public int getContentLength() { - + URLConnection openConnection; + try { + openConnection = url.openConnection(); + return openConnection.getContentLength(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return 0; } diff --git a/group12/446031103/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group12/446031103/src/com/coderising/download/impl/ConnectionManagerImpl.java index 172371dd55..18836b4a28 100644 --- a/group12/446031103/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/group12/446031103/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -9,7 +9,7 @@ public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - return null; + return new ConnectionImpl(url); } } diff --git a/group12/446031103/src/com/coderising/download/test/ConnectionTest.java b/group12/446031103/src/com/coderising/download/test/ConnectionTest.java new file mode 100644 index 0000000000..cda74451ca --- /dev/null +++ b/group12/446031103/src/com/coderising/download/test/ConnectionTest.java @@ -0,0 +1,53 @@ +package com.coderising.download.test; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class ConnectionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testContentLength() throws Exception{ + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception{ + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + + // 测试不充分,没有断言内容是否正确 + } + +} diff --git a/group12/446031103/src/com/coderising/download/test/FileDownloaderTest.java b/group12/446031103/src/com/coderising/download/test/FileDownloaderTest.java new file mode 100644 index 0000000000..a3e7d24429 --- /dev/null +++ b/group12/446031103/src/com/coderising/download/test/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download.test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.FileDownloader; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + + FileDownloader downloader = new FileDownloader(url,"E:\\TEST\\test.jpg"); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + } + +} diff --git a/group12/446031103/src/com/coderising/jvm/loader/ClassFileLoader.java b/group12/446031103/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..e021b94d38 --- /dev/null +++ b/group12/446031103/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,43 @@ +package com.coderising.jvm.loader; + +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + return null; + + + } + + private byte[] loadClassFile(String clzFileName) { + + return null; + } + + + + public void addClassPath(String path) { + + } + + public String getClassPath_V1(){ + + return null; + } + + public String getClassPath(){ + return null; + } + + + + + +} diff --git a/group12/446031103/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group12/446031103/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..50eb1be6fa --- /dev/null +++ b/group12/446031103/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i actions = new HashMap<>(); + + public Configuration(String fileName){ + String packageName = this.getClass().getPackage().getName(); + packageName = packageName.replace('.', '/'); + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + parseXML(is); + } + + + + private void parseXML(InputStream is) { + SAXReader reader = new SAXReader(); + try { + Document document = reader.read(is); + Element struts = document.getRootElement(); + Iterator actions = struts.elementIterator(); + while (actions.hasNext()) { + Element action = (Element) actions.next(); + String actionName=action.attributeValue("name"); + String actionClass=action.attributeValue("class"); + ActionConfig ac = new ActionConfig(actionName,actionClass); + Iterator results = action.elementIterator(); + while (results.hasNext()) { + Element result = (Element) results.next(); + String name = result.attributeValue("name"); + String viewName = result.getStringValue(); + ac.addViewResult(name, viewName); + } + this.actions.put(actionName, ac); + } + } catch (DocumentException e) { + + e.printStackTrace(); + } + + } + + + + public String getClassName(String actionName) { + ActionConfig actionConfig = actions.get(actionName); + if(null==actionConfig) + return null; + return actionConfig.getClassName(); + } + + public String getResultView(String actionName, String resultName) { + ActionConfig actionConfig =actions.get(actionName); + if(null==actionConfig) + return null; + return actionConfig.getViewName(resultName); + } + + private static class ActionConfig{ + + String name; + String clzName; + Map viewResult = new HashMap<>(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } + } + + +} diff --git a/group12/446031103/src/com/coderising/litestruts/ConfigurationTest.java b/group12/446031103/src/com/coderising/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..f4af430eef --- /dev/null +++ b/group12/446031103/src/com/coderising/litestruts/ConfigurationTest.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ConfigurationTest { + Configuration cfg = new Configuration("struts.xml"); + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.coderising.litestruts.LoginAction", clzName); + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.coderising.litestruts.LogoutAction", clzName); + } + @Test + public void testGetResultView() { + String jsp=cfg.getResultView("login","success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + jsp=cfg.getResultView("login","fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + jsp=cfg.getResultView("logout","success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + jsp=cfg.getResultView("logout","error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + } +} diff --git a/group12/446031103/src/com/coderising/litestruts/LoginAction.java b/group12/446031103/src/com/coderising/litestruts/LoginAction.java index 39af2d5c26..dcdbe226ed 100644 --- a/group12/446031103/src/com/coderising/litestruts/LoginAction.java +++ b/group12/446031103/src/com/coderising/litestruts/LoginAction.java @@ -36,7 +36,4 @@ public void setPassword(String password){ public String getMessage(){ return this.message; } - public void setMessage(String message){ - this.message = message; - } } diff --git a/group12/446031103/src/com/coderising/litestruts/ReflectionUtil.java b/group12/446031103/src/com/coderising/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..c9bbd312ef --- /dev/null +++ b/group12/446031103/src/com/coderising/litestruts/ReflectionUtil.java @@ -0,0 +1,75 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + public static List getMethods(Class clz,String startsWithName) { + List methods = new ArrayList<>(); + for (Method method : clz.getDeclaredMethods()) { + if(method.getName().startsWith(startsWithName)){ + methods.add(method); + } + } + return methods; + } + + public static void setParams(Object o, Map params) { + List methods = getSetterMethods(o.getClass()); + for (String name : params.keySet()) { + String methodName = "set"+name; + for (Method method : methods) { + if(methodName.equalsIgnoreCase(method.getName())){ + try { + method.invoke(o, params.get(name)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + }; + } + + } + } + + public static Map getParams(Object o) { + Map params = new HashMap<>(); + List methods = getGetterMethods(o.getClass()); + for (Method method : methods) { + try { + String name=method.getName(); + name = name.replaceFirst("get", "").toLowerCase(); + Object value = method.invoke(o); + params.put(name, value); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return params; + } + +} diff --git a/group12/446031103/src/com/coderising/litestruts/ReflectionUtilTest.java b/group12/446031103/src/com/coderising/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..f1b233be37 --- /dev/null +++ b/group12/446031103/src/com/coderising/litestruts/ReflectionUtilTest.java @@ -0,0 +1,96 @@ +package com.coderising.litestruts; + +import static org.junit.Assert.*; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSetterMethod() throws ClassNotFoundException { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods=ReflectionUtil.getSetterMethods(clz); + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + @Test + public void testGetGetterMethod() throws ClassNotFoundException { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods=ReflectionUtil.getGetterMethods(clz); + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + expectedNames.add("getMessage"); + Set acctualNames = new HashSet<>(); + for(Method m : methods){ + acctualNames.add(m.getName()); + } + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + @Test + public void testSetterParams() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + ReflectionUtil.setParams(o,params); + + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + @Test + public void testGetterParams() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + String name = "com.coderising.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction la = (LoginAction) clz.newInstance(); + la.setName("test"); + la.setPassword("123456"); + Map params =ReflectionUtil.getParams(la); + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } +} diff --git a/group12/446031103/src/com/coderising/litestruts/Struts.java b/group12/446031103/src/com/coderising/litestruts/Struts.java index 8f5913d836..c4466c1d02 100644 --- a/group12/446031103/src/com/coderising/litestruts/Struts.java +++ b/group12/446031103/src/com/coderising/litestruts/Struts.java @@ -9,6 +9,7 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import org.dom4j.Document; @@ -24,92 +25,42 @@ * @version: V1.0 */ public class Struts { + private final static Configuration cfg = new Configuration("struts.xml"); public static View runAction(String actionName, Map parameters) { - Map xmlDoc = getXMLDOC(actionName); - Map viewMap = new HashMap(); - View view = new View(); - view.setParameters(viewMap); - try { - Class classz = Class.forName(xmlDoc.get(actionName)); - LoginAction la = (LoginAction) classz.newInstance(); - la.setName(parameters.get("name")); - la.setPassword(parameters.get("password")); - Method exectue = classz.getMethod("execute", null); - Object result = exectue.invoke(la, null); - Field[] fields = classz.getDeclaredFields(); - for (Field field : fields) { - PropertyDescriptor pd = new PropertyDescriptor(field.getName(), classz); - Method readMethod = pd.getReadMethod(); - viewMap.put(field.getName(), (String) readMethod.invoke(la, null)); - } - view.setJsp(xmlDoc.get(result)); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IntrospectionException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + /* - * 0. 读取配置文件struts.xml 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象), + * 0. 读取配置文件struts.xml + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象), * 据parameters中的数据,调用对象的setter方法 例如parameters中的数据是 ("name"="test" , "password"="1234") , - * 那就应该调用 setName和setPassword方法 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" 3. - * 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - * 放到View对象的parameters 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 那就应该调用 setName和setPassword方法 + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * 3.通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + * 放到View对象的parameters + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, * 放到View对象的jsp字段中。 */ - return view; - } - /** - * @MethodName: getXMLDOC - * @Description: 解析xml文件 - * @param actionName - * @return - * @return: Map - */ - private static Map getXMLDOC(String actionName) { - Map xmldoc = new HashMap(); - // 解析struts.xml文件 - // 创建SAXReader的对象reader - SAXReader reader = new SAXReader(); + String className = cfg.getClassName(actionName); + try { - // 通过reader对象的read方法读取struts.xml,得到document对象 - Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); - // 获取根节点 - Element struts = document.getRootElement(); - // 迭代节点 - Iterator actions = struts.elementIterator(); - while (actions.hasNext()) { - Element action = (Element) actions.next(); - if (actionName.equals(action.attributeValue("name"))) { - xmldoc.put(action.attributeValue("name"), action.attributeValue("class")); - Iterator results = action.elementIterator(); - while (results.hasNext()) { - Element result = (Element) results.next(); - xmldoc.put(result.attributeValue("name"), result.getStringValue()); - } - break; - } - } - } catch (DocumentException e) { + Class clz = Class.forName(className); + Object o = clz.newInstance(); + ReflectionUtil.setParams(o, parameters); + Method exectue = clz.getDeclaredMethod("execute"); + String resultName=(String) exectue.invoke(o); + Map params = ReflectionUtil.getParams(o); + String resultView = cfg.getResultView(actionName, resultName); + View v = new View(); + v.setJsp(resultView); + v.setParameters(params); + return v; + } catch (Exception e) { + // TODO Auto-generated catch block e.printStackTrace(); - } - return xmldoc; + } + + + return null; } + } diff --git a/group12/446031103/src/com/coding/basic/ArrayList.java b/group12/446031103/src/com/coding/basic/ArrayList.java deleted file mode 100644 index bc3d75c3f6..0000000000 --- a/group12/446031103/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.coding.basic; - -import java.util.Arrays; - - - -/** - * - * arrayList集合-数组 - * - * @ClassName ArrayList - * @author msh - * @date 2017年2月21日 下午3:49:24 - */ -public class ArrayList implements List { - private int size = 0; - private Object[] elementData = new Object[0]; - /** - * - * 向最后插入元素 - * - * @Method add 添加 - * @param o 元素 - * @see com.coding.basic.List#add(java.lang.Object) - */ - public void add(Object o){ - ensureCapacity(size + 1); - elementData[size] = o; - size++; - } - /** - * - * 向指定位置插入元素 - * - * @Method add 添加 - * @param index 下标 - * @param o 元素 - * @see com.coding.basic.List#add(int, java.lang.Object) - */ - public void add(int index, Object o){ - validate(index); - ensureCapacity(size + 1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - /** - * - * 取得元素 - * - * @Method get 取得 - * @param index 下标 - * @return - * @see com.coding.basic.List#get(int) - */ - public Object get(int index){ - validate(index); - return elementData[index]; - } - /** - * - * 删除元素 - * - * @Method remove 删除 - * @param index 下标 - * @return 删除的元素 - * @see com.coding.basic.List#remove(int) - */ - public Object remove(int index){ - validate(index); - Object oldValue = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[size] = null; - size--; - return oldValue; - } - /** - * - * 取得集合大小 - * - * @Method size 集合大小 - * @return 集合大小 - * @see com.coding.basic.List#size() - */ - public int size(){ - return this.size; - } - /** - * 迭代 - * @return - */ - public Iterator iterator(){ - return new ArrayListIterator(); - } - /** - * 判断是否需要数组增长 - * @param minCapacity - */ - private void ensureCapacity(int minCapacity) { - if(minCapacity>elementData.length){ - int newCapacity = Math.max(minCapacity, elementData.length*2); - Object[] newElementData = new Object[newCapacity]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - } - } - /** - * - * 验证 - * - * @MethodName validate 下标 - * @author msh - * @date 2017年2月21日 下午3:54:21 - * @param index - */ - private void validate(int index) { - if (index < 0 || index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - /** - * - * @author Administrator - * - */ - private class ArrayListIterator implements Iterator{ - private int position; - private ArrayList list; - @Override - public boolean hasNext() { - return position < list.size(); - } - - @Override - public Object next() { - if (hasNext()) { - return list.get(position++); - } - return null; - } - - } -} diff --git a/group12/446031103/src/com/coding/basic/BinaryTreeNode.java b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index ed26d946bb..0000000000 --- a/group12/446031103/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.coding.basic; - - -public class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - private BinaryTreeNode( Object data,BinaryTreeNode left,BinaryTreeNode right){ - this.data = data; - this.left = left; - this.right = right; - } - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(BinaryTreeNode tree,Object o){ - if(null== tree){ - tree = new BinaryTreeNode(o, null, null); - } - if(Integer.valueOf(o.toString())>Integer.valueOf(tree.data.toString())){ - tree.right =insert(tree.right,o); - }else{ - tree.left = insert(tree.left,o); - } - return tree; - } - -} diff --git a/group12/446031103/src/com/coding/basic/LinkedList.java b/group12/446031103/src/com/coding/basic/LinkedList.java deleted file mode 100644 index 33f6d79e65..0000000000 --- a/group12/446031103/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,370 +0,0 @@ -package com.coding.basic; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import org.junit.experimental.theories.Theories; - -import sun.reflect.Reflection; - -/** - * - * LinkedList集合-链 - * - * @ClassName LinkedList - * @author msh - * @date 2017年2月21日 下午4:08:01 - */ -public class LinkedList implements List { - //链头 - private Node head; - //集合大小 - private int size=0; - /** - * - * 向链中添加元素 - * - * @Method add 添加 - * @param o 元素 - * @see com.coding.basic.List#add(java.lang.Object) - */ - public void add(Object o){ - Node newNode = new Node(o, null); - if (null == head) { - head = newNode; - } else { - Node lastNode = null; - for (int i = 0; i < size; i++) { - lastNode = (Node) get(i); - } - lastNode.next = newNode; - } - size++; - } - /** - * - * 向链中添加元素 - * - * @Method add 增加 - * @param index 下标 - * @param o 元素 - * @see com.coding.basic.List#add(int, java.lang.Object) - */ - public void add(int index , Object o){ - validate(index); - Node newNode = null; - Node perNode = null; - Node nextNode = null; - // 当为最后插入时 - if (index == size - 1) { - newNode = new Node(o, null); - for (int i = 0; i < index; i++) { - Node tempNode = (Node) get(i); - perNode = tempNode.next; - } - perNode.next = newNode; - } else if (0 == index) { - nextNode = head.next; - newNode = new Node(o, nextNode); - head = newNode; - } else { - for (int i = 0; i < index; i++) { - Node tempNode = (Node) get(i); - perNode = tempNode.next; - } - nextNode = perNode.next.next; - newNode = new Node(o, nextNode); - perNode.next = newNode; - } - size++; - } - /** - * - * 取得元素 - * - * @Method get 取得 - * @param index 下标 - * @return - * @see com.coding.basic.List#get(int) - */ - public Object get(int index){ - validate(index); - Node tempNode = head; - for (int i = 0; i <= index; i++) { - tempNode = tempNode.next; - } - return tempNode; - } - /** - * - * 删除元素 - * - * @Method remove 删除 - * @param index 下标 - * @return - * @see com.coding.basic.List#remove(int) - */ - public Object remove(int index){ - Node removeNode = (Node) get(index); - validate(index); - if (index == size - 1) { - Node tempNode = head; - for (int i = 0; i < index; i++) { - tempNode = tempNode.next; - } - tempNode.next = null; - } else if (index == 0) { - Node tempNode = head.next; - head.next = null; - head = tempNode; - } else { - } - size--; - return removeNode; - } - /** - * - * 取得集合大小 - * - * @Method size 集合大小 - * @return 集合大小 - * @see com.coding.basic.List#size() - */ - public int size(){ - return size; - } - /** - * - * 想链头中插入元素 - * - * @MethodName addFirst - * @author msh - * @date 2017年2月21日 下午4:10:56 - * @param o - */ - public void addFirst(Object o){ - Node newNode = new Node(o, head); - head = newNode; - } - /** - * - * 向链后加入元素 - * - * @MethodName addLast - * @author msh - * @date 2017年2月21日 下午4:11:43 - * @param o - */ - public void addLast(Object o){ - add(o); - } - /** - * - * 删除链头 - * - * @MethodName removeFirst - * @author msh - * @date 2017年2月21日 下午4:12:14 - * @return - */ - public Object removeFirst(){ - if(null==head) - throw new IndexOutOfBoundsException("Size: " + size); - Node orgHead = head; - Node tempNode = head.next; - head.next = null; - head = tempNode; - return orgHead; - } - /** - * - * 删除链尾 - * - * @MethodName removeLast - * @author zhaogd - * @date 2017年2月21日 下午4:12:44 - * @return - */ - public Object removeLast(){ - if(null==head) - throw new IndexOutOfBoundsException("Size: " + size); - Node lastNode = (Node) get(size); - Node tempNode = head; - for (int i = 0; i < (size - 1); i++) { - tempNode = tempNode.next; - } - tempNode.next = null; - return lastNode; - } - public Iterator iterator(){ - return null; - } - - /** - * - * 验证 - * - * @MethodName validate 下标 - * @author msh - * @date 2017年2月21日 下午3:54:21 - * @param index - */ - private void validate(int index) { - if (index < 0 || index >= size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - /** - * - * 链中元素 - * - * @ClassName Node - * @author zhaogd - * @date 2017年2月21日 下午4:13:10 - */ - private static class Node{ - Object data; - Node next; - public Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - for (int i = size; i <0; i--) { - Node last=(Node) get(i); - if(0==i) - last.next = null; - else - last.next = (Node) get(i-1); - } - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf(){ - - for (int i = 0; i < size%2; i++) { - remove(i); - } - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - for (int a = i; a < i+length; a++) { - remove(a); - } - - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - int [] result =new int[list.size]; - Class callClass=Reflection.getCallerClass(); - for (int i = 0; i < list.size; i++) { - Node node=(Node) list.get(i); - try { - Method method=callClass.getDeclaredMethod("get", new Class[]{int.class}); - result[i]=(int) method.invoke(callClass, new Object[]{node.data}); - } catch (NoSuchMethodException | SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - for (int i = 0; i < list.size; i++) { - Node node=(Node) list.get(i); - for (int j = 0; j < size; j++) { - Node orgNode=(Node) get(i); - if (node.data.equals(orgNode.data)) { - remove(j); - } - } - } - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - for (int i = 0; i < size; i++) { - Node node=(Node) get(i); - for (int j = i+1; j < size; j++) { - Node newNode=(Node) get(j); - if(newNode.data.equals(node.data)) - remove(j); - } - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - for (int i = 0; i < size; i++) { - Node node=(Node) get(i); - if((int)node.data>min && (int)node.dataelementData.length){ + int newCapacity = Math.max(minCapacity, elementData.length*2); + Object[] newElementData = new Object[newCapacity]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + } + } + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + /** + * + * @author Administrator + * + */ + private class ArrayListIterator implements Iterator{ + private int position; + private ArrayList list; + @Override + public boolean hasNext() { + return position < list.size(); + } + + @Override + public Object next() { + if (hasNext()) { + return list.get(position++); + } + return null; + } + + } +} diff --git a/group12/446031103/src/com/datastructure/array/ArrayUtil.java b/group12/446031103/src/com/datastructure/array/ArrayUtil.java new file mode 100644 index 0000000000..a5b62040bc --- /dev/null +++ b/group12/446031103/src/com/datastructure/array/ArrayUtil.java @@ -0,0 +1,216 @@ +package com.datastructure.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(null ==origin ||0==origin.length){ + return; + } + for (int i = 0,j=origin.length-1; i < j; i++,j--) { + int temp=origin[i] ; + origin[i]= origin[j]; + origin[i]=temp; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + if(null==oldArray||oldArray.length ==0){ + return null; + } + int notZeroCnt = 0; + int [] temp = new int[oldArray.length]; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + temp[notZeroCnt++] = oldArray[i]; + } + } + System.arraycopy(temp, 0, temp, 0, notZeroCnt); + return temp; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + if(null==array1&&null==array2){ + return null; + } + int [] temp = new int[array1.length+array2.length]; + int i = 0; + int j = 0; + int count = 0; + while (iarray2[j]){ + temp[count++] = array2[j++]; + } + if(array1[i]==array2[j]){ + temp[count++] = array2[j]; + i++; + j++; + } + } + while(i==array1.length&&j=max){ + break; + }else{ + cnt++; + } + } + return Arrays.copyOf(temp, cnt); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max<2){ + return new int[0]; + } + int [] temp = new int[max]; + int cnt = 0; + for (int i = 2; i < max; i++) { + if(isPrime(i)){ + temp[cnt++] = i; + } + } + return Arrays.copyOf(temp, cnt); + } + + private boolean isPrime(int n){ + int i = 2; + while(iInteger.valueOf(tree.data.toString())){ + tree.right =insert(tree.right,o); + }else{ + tree.left = insert(tree.left,o); + } + return tree; + } + +} diff --git a/group12/446031103/src/com/datastructure/basic/Iterator.java b/group12/446031103/src/com/datastructure/basic/Iterator.java new file mode 100644 index 0000000000..bee5d797c9 --- /dev/null +++ b/group12/446031103/src/com/datastructure/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.datastructure.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/446031103/src/com/datastructure/basic/List.java b/group12/446031103/src/com/datastructure/basic/List.java new file mode 100644 index 0000000000..633f1f73e2 --- /dev/null +++ b/group12/446031103/src/com/datastructure/basic/List.java @@ -0,0 +1,9 @@ +package com.datastructure.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group12/446031103/src/com/datastructure/basic/Queue.java b/group12/446031103/src/com/datastructure/basic/Queue.java new file mode 100644 index 0000000000..b82627d59f --- /dev/null +++ b/group12/446031103/src/com/datastructure/basic/Queue.java @@ -0,0 +1,70 @@ +package com.datastructure.basic; + +import com.datastructure.linklist.LinkedList; + +/** + * + * 队列-先进先出 + * + * @ClassName Queue + * @author msh + * @date 2017年2月21日 下午9:29:03 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + /** + * + * 入队列 + * + * @MethodName enQueue + * @author msh + * @date 2017年2月21日 下午9:45:15 + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + } + /** + * + * 离开队列 + * + * @MethodName deQueue + * @author msh + * @date 2017年2月21日 下午9:56:06 + * @return + */ + public Object deQueue(){ + if(isEmpty()) + throw new IndexOutOfBoundsException("size:"+size()); + Object o=elementData.get(0); + elementData.removeFirst(); + return o; + } + /** + * + * 是否为空 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:57:14 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==elementData.size()) + temp= true; + return temp; + } + /** + * + * 队列中元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:57:28 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group12/446031103/src/com/datastructure/basic/Stack.java b/group12/446031103/src/com/datastructure/basic/Stack.java new file mode 100644 index 0000000000..3088b0a000 --- /dev/null +++ b/group12/446031103/src/com/datastructure/basic/Stack.java @@ -0,0 +1,83 @@ +package com.datastructure.basic; + +import com.datastructure.array.ArrayList; + +/** + * + * 栈-先进后出 + * + * @ClassName Stack + * @author msh + * @date 2017年2月21日 下午9:05:39 + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + /** + * + * 向栈中加入元素 + * + * @MethodName push + * @author msh + * @date 2017年2月21日 下午9:12:03 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + /** + * + * 从栈中取出元素 + * + * @MethodName pop + * @author msh + * @date 2017年2月21日 下午9:12:51 + * @return + */ + public Object pop(){ + Object o= peek(); + elementData.remove(size()-1); + return o; + } + /** + * + * 取出栈顶元素 + * + * @MethodName peek + * @author msh + * @date 2017年2月21日 下午9:13:08 + * @return + */ + public Object peek(){ + Object o=elementData.get(size()-1); + return o; + } + /** + * + * 判断栈中是否有元素 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:14:26 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==size()) + temp = true; + return temp; + } + /** + * + * 栈中有多少元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:16:42 + * @return + */ + public int size(){ + return elementData.size(); + } + +} diff --git a/group12/446031103/src/com/datastructure/linklist/LRUPageFrame.java b/group12/446031103/src/com/datastructure/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..5e20d9a49e --- /dev/null +++ b/group12/446031103/src/com/datastructure/linklist/LRUPageFrame.java @@ -0,0 +1,158 @@ +package com.datastructure.linklist; + +import java.util.Objects; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node(Node prev,Node next,int pageNum) { + this.prev = prev; + this.next = next; + this.pageNum = pageNum; + } + Node(){ + + } + } + + private int capacity; + private int addCnt ; + private int size; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + first = new Node(); + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + if(null==this.last){ + this.last = new Node(this.first,null,pageNum); + this.first.next = this.last; + addCnt++; + size++; + return; + } + if(contain(pageNum)){ + if(Objects.equals(this.first.next.pageNum, pageNum)){ + return; + } + boolean temp = Objects.equals(getLastNode(size).pageNum, pageNum); + Node currentNode = getNode(pageNum); + Node nextNode = currentNode.next; + Node preNode = currentNode.prev; + + currentNode.prev = this.first; + currentNode.next = this.first.next; + this.first.next = currentNode; + + if(temp){ + preNode.next = null; + if(null==preNode.prev) + preNode.prev = currentNode; + }else{ + preNode.prev = currentNode; + preNode.next = nextNode; + nextNode.next = null; + nextNode.prev = preNode; + } + this.last = getLastNode(size); + return; + } + Node addNode = new Node(this.first,this.first.next,pageNum); + Node oldFirstnode=this.first.next ; + oldFirstnode.prev = addNode; + this.first.next = addNode; + if(isOut()){ + + addCnt++; + if(this.size !=this.capacity ) + size++; + if(addCnt>size) + this.last.prev.next = null; + this.last = getLastNode(size); + }else{ + this.last.prev.next = null; + } + + + } + + private boolean isOut(){ + return this.addCnt<=this.capacity; + } + + private Node getLastNode(int sizes) { + Node node=this.first; + for (int i = 0; i < sizes; i++) { + node = node.next; + } + return node; + } + + private boolean contain(Object o){ + Node node = this.first; + + while(null!=node.next){ + + node = node.next; + + if(Objects.equals(node.pageNum,o)){ + return true; + } + + } + return false; + } + + private Node getNode(Object o){ + Node node = this.first; + + while(null!=node.next){ + + node = node.next; + + if(Objects.equals(node.pageNum,o)){ + return node; + } + + } + return null; + } + + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node.next != null){ + node = node.next; + buffer.append(node.pageNum); + if(node.next != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group12/446031103/src/com/datastructure/linklist/LRUPageFrameTest.java b/group12/446031103/src/com/datastructure/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..956aa6c697 --- /dev/null +++ b/group12/446031103/src/com/datastructure/linklist/LRUPageFrameTest.java @@ -0,0 +1,46 @@ +package com.datastructure.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + +// frame.access(7); +// Assert.assertEquals("7", frame.toString()); +// frame.access(7); +// Assert.assertEquals("7", frame.toString()); +// frame.access(2); +// Assert.assertEquals("2,7", frame.toString()); +// frame.access(7); +// Assert.assertEquals("7,2", frame.toString()); +// frame.access(3); +// Assert.assertEquals("3,7,2", frame.toString()); +// frame.access(2); +// Assert.assertEquals("2,3,7", frame.toString()); +// frame.access(2); +// Assert.assertEquals("2,3,7", frame.toString()); + } + +} diff --git a/group12/446031103/src/com/datastructure/linklist/LinkedList.java b/group12/446031103/src/com/datastructure/linklist/LinkedList.java new file mode 100644 index 0000000000..1236734423 --- /dev/null +++ b/group12/446031103/src/com/datastructure/linklist/LinkedList.java @@ -0,0 +1,397 @@ +package com.datastructure.linklist; +import java.util.Stack; + +import com.datastructure.basic.Iterator; +import com.datastructure.basic.List; + +/** + * + * LinkedList集合-链 + * + * @ClassName LinkedList + * @author msh + * @date 2017年2月21日 下午4:08:01 + */ +public class LinkedList implements List { + //链头 + private Node head; + //集合大小 + private int size=0; + /** + * + * 向链中添加元素 + * + * @Method add 添加 + * @param o 元素 + * @see com.datastructure.basic.List#add(java.lang.Object) + */ + public void add(Object o){ + Node newNode = new Node(o, null); + if (null == head) { + head = newNode; + } else { + Node lastNode = null; + for (int i = 0; i < size; i++) { + lastNode = (Node) get(i); + } + lastNode.next = newNode; + } + size++; + } + /** + * + * 向链中添加元素 + * + * @Method add 增加 + * @param index 下标 + * @param o 元素 + * @see com.datastructure.basic.List#add(int, java.lang.Object) + */ + public void add(int index , Object o){ + validate(index); + Node newNode = null; + Node perNode = null; + Node nextNode = null; + // 当为最后插入时 + if (index == size - 1) { + newNode = new Node(o, null); + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + perNode.next = newNode; + } else if (0 == index) { + nextNode = head.next; + newNode = new Node(o, nextNode); + head = newNode; + } else { + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + nextNode = perNode.next.next; + newNode = new Node(o, nextNode); + perNode.next = newNode; + } + size++; + } + /** + * + * 取得元素 + * + * @Method get 取得 + * @param index 下标 + * @return + * @see com.datastructure.basic.List#get(int) + */ + public Object get(int index){ + validate(index); + Node tempNode = head; + for (int i = 0; i <= index; i++) { + tempNode = tempNode.next; + } + return tempNode; + } + /** + * + * 删除元素 + * + * @Method remove 删除 + * @param index 下标 + * @return + * @see com.datastructure.basic.List#remove(int) + */ + public Object remove(int index){ + Node removeNode = (Node) get(index); + validate(index); + if (index == size - 1) { + Node tempNode = head; + for (int i = 0; i < index; i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + } else if (index == 0) { + Node tempNode = head.next; + head.next = null; + head = tempNode; + } else { + } + size--; + return removeNode; + } + /** + * + * 取得集合大小 + * + * @Method size 集合大小 + * @return 集合大小 + * @see com.datastructure.basic.List#size() + */ + public int size(){ + return size; + } + /** + * + * 想链头中插入元素 + * + * @MethodName addFirst + * @author msh + * @date 2017年2月21日 下午4:10:56 + * @param o + */ + public void addFirst(Object o){ + Node newNode = new Node(o, head); + head = newNode; + } + /** + * + * 向链后加入元素 + * + * @MethodName addLast + * @author msh + * @date 2017年2月21日 下午4:11:43 + * @param o + */ + public void addLast(Object o){ + add(o); + } + /** + * + * 删除链头 + * + * @MethodName removeFirst + * @author msh + * @date 2017年2月21日 下午4:12:14 + * @return + */ + public Object removeFirst(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node orgHead = head; + Node tempNode = head.next; + head.next = null; + head = tempNode; + return orgHead; + } + /** + * + * 删除链尾 + * + * @MethodName removeLast + * @author zhaogd + * @date 2017年2月21日 下午4:12:44 + * @return + */ + public Object removeLast(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node lastNode = (Node) get(size); + Node tempNode = head; + for (int i = 0; i < (size - 1); i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + return lastNode; + } + public Iterator iterator(){ + return null; + } + + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + /** + * + * 链中元素 + * + * @ClassName Node + * @author zhaogd + * @date 2017年2月21日 下午4:13:10 + */ + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Stack s=new Stack(); + Node currentNode = head; + while(null!=currentNode){ + s.push(currentNode); + Node tempNode=currentNode.next; + currentNode.next = null; + currentNode = tempNode; + } + head = (Node) s.pop(); + currentNode= head; + while(!s.isEmpty()){ + Node tempNode=(Node) s.pop(); + currentNode.next = tempNode; + currentNode = tempNode; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + + for (int i = 0; i < size/2; i++) { + remove(i); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(i<0||i>=size){ + throw new IndexOutOfBoundsException(); + } + int len = size-1>=length?length:size-1; + int k = 0; + while(k101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int [] arr = new int [list.size()]; + for (int i = 0; i < list.size; i++) { + arr[i] = (int) get((int)list.get(i)); + } + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + for (int i = 0; i < list.size; i++) { + Node node=(Node) list.get(i); + for (int j = 0; j < size; j++) { + Node orgNode=(Node) get(i); + if (node.data.equals(orgNode.data)) { + remove(j); + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + * @throws Exception + */ + public void removeDuplicateValues() throws Exception{ + if(null==head){ + throw new Exception(); + } + Node pre = head; + Node cur =head; + while(null!=cur.next){ + cur = cur.next; + Object o=pre.data; + while(cur.data == o){ + if(null==cur.next){ + pre.next = null; + } + pre.next = cur.next; + size--; + cur = cur.next; + if(null==cur){ + break; + } + } + pre = pre.next; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(null==head){ + return ; + } + Node node = head; + int star = 0; + int end = 0; + int i = 0; + while(null!=node){ + if((int)node.data<=min){ + star = i; + } + if((int)node.data>=max){ + end = i; + break; + } + i++; + } + remove(star,end-star); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + if(list==null){ + return null; + } + LinkedList newList = new LinkedList(); + int i1=0; + int i2=0; + if(i1value2){ + i2++; + } + } + return newList; + } +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/array/ArrayUtil.java b/group12/563253496/week3_file_download/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..e5ddb476a6 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/DownloadThread.java b/group12/563253496/week3_file_download/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..a3dc676a90 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,50 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.sun.org.apache.xpath.internal.SourceTree; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread { + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + + public DownloadThread(Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + } + + public void run() { + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + + try { + byte[] data= conn.read(startPos,endPos); + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + file.seek(startPos); + file.write(data); + file.close(); + conn.close(); + barrier.await(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } + + } +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/FileDownloader.java b/group12/563253496/week3_file_download/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..8b35a9a11d --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,126 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + + +public class FileDownloader { + + String url; + String localFile; + DownloadListener listener; + + ConnectionManager cm; + + private static final int DOWNLOAD_TRHEAD_NUM = 3; + + public FileDownloader(String _url, String _localFile) { + this.url = _url; + this.localFile = _localFile; + + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM, new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + + + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + this.createPlaceHolderFile(localFile, length); + + int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); + + for (int i = 0; i < DOWNLOAD_TRHEAD_NUM; i++) { + DownloadThread thread = new DownloadThread(cm.open(url), ranges[i][0], ranges[i][1], localFile, barrier); + thread.start(); + } + + //new DownloadThread(conn, 0, length - 1, localFile, barrier).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + private void createPlaceHolderFile(String fileName, int contentLen) { + try { + RandomAccessFile file = new RandomAccessFile(fileName, "rw"); + for (int i = 0; i < contentLen; i++) { + file.write(0); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + int[][] ranges = new int[threadNum][2]; + int eachThreadSize = contentLen / threadNum; + int left = contentLen % threadNum; + + for (int i = 0; i < threadNum; i++) { + int startPos = i * eachThreadSize; + int endPos = (i + 1) * eachThreadSize - 1; + if (i == (threadNum - 1)) { + endPos += left; + } + ranges[i][0] = startPos; + ranges[i][1] = endPos; + } + return ranges; + } + + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/FileDownloaderTest.java b/group12/563253496/week3_file_download/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..7ec4c1f2e3 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + //String url = "http://localhost:8080/test.jpg"; + String url ="http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g3/M04/0B/06/Cg-4V1Q_K_2IS20UAAvKmTmHyYIAAQLGwOZI-YAC8qx308.jpg"; + FileDownloader downloader = new FileDownloader(url,"d:/test.jpg"); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/api/Connection.java b/group12/563253496/week3_file_download/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/api/ConnectionException.java b/group12/563253496/week3_file_download/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/api/ConnectionManager.java b/group12/563253496/week3_file_download/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/api/DownloadListener.java b/group12/563253496/week3_file_download/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/impl/ConnectionImpl.java b/group12/563253496/week3_file_download/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..37b413558d --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,73 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection { + + static final int BUFFER_SIZE = 1024; + URL url; + + ConnectionImpl(String _url) { + try { + url = new URL(_url); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream is = httpConn.getInputStream(); + byte[] buffer = new byte[BUFFER_SIZE]; + int totalLen = endPos - startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while (baos.size() < totalLen) { + int len = is.read(buffer); + if (len < 0) { + break; + } + baos.write(buffer); + } + if (baos.size() > totalLen) { + byte[] temp = baos.toByteArray(); + return Arrays.copyOf(temp, totalLen); + } + return baos.toByteArray(); + + + } + + @Override + public int getContentLength() { + try { + URLConnection conn = url.openConnection(); + return conn.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + + + } + + @Override + public void close() { + + + } + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group12/563253496/week3_file_download/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..f4a83cdbf6 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + //return null; + } + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/litestruts/LoginAction.java b/group12/563253496/week3_file_download/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/litestruts/Struts.java b/group12/563253496/week3_file_download/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/litestruts/StrutsTest.java b/group12/563253496/week3_file_download/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group12/563253496/week3_file_download/src/com/coderising/litestruts/View.java b/group12/563253496/week3_file_download/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group12/563253496/week3_file_download/src/com/coding/basic/ArrayList.java b/group12/563253496/week3_file_download/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group12/563253496/week3_file_download/src/com/coding/basic/BinaryTreeNode.java b/group12/563253496/week3_file_download/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group15/1502_1617273078/src/com/coding/basic/Iterator.java b/group12/563253496/week3_file_download/src/com/coding/basic/Iterator.java similarity index 100% rename from group15/1502_1617273078/src/com/coding/basic/Iterator.java rename to group12/563253496/week3_file_download/src/com/coding/basic/Iterator.java diff --git a/group12/563253496/week3_file_download/src/com/coding/basic/LinkedList.java b/group12/563253496/week3_file_download/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..4fdb03db8a --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coding/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + + + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/List.java b/group12/563253496/week3_file_download/src/com/coding/basic/List.java similarity index 100% rename from group15/1513_121469918/HomeWork20170305/src/com/coding/basic/List.java rename to group12/563253496/week3_file_download/src/com/coding/basic/List.java diff --git a/group12/563253496/week3_file_download/src/com/coding/basic/Queue.java b/group12/563253496/week3_file_download/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group12/563253496/week3_file_download/src/com/coding/basic/Stack.java b/group12/563253496/week3_file_download/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group12/563253496/week3_file_download/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group12/563253496/week4_jvm1/src/com/coderising/jvm/loader/ClassFileLoader.java b/group12/563253496/week4_jvm1/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..5f56b80bff --- /dev/null +++ b/group12/563253496/week4_jvm1/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,81 @@ +package com.coderising.jvm.loader; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + String path = getClassFilePath(className); + if (path != null) { + try { + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path)); + int count = bis.available(); + byte[] content = new byte[count]; + int len = bis.read(content,0,count); + return content; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + + + } + + private byte[] loadClassFile(String clzFileName) { + + return null; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath_V1() { + + return null; + } + + public String getClassPath() { + StringBuilder sb = new StringBuilder(); + for (String s : clzPaths) { + sb.append(s); + sb.append(";"); + } + sb.deleteCharAt(sb.length() - 1); + return sb.toString(); + } + + private String getClassFilePath(String className) { + StringBuilder sb = new StringBuilder(); + for (String path : clzPaths + ) { + sb.append(path); + sb.append("\\"); + char[] classname = className.toCharArray(); + for (int i = 0; i < classname.length; i++) { + if (classname[i] == '.') { + sb.append("\\"); + + } else { + sb.append(classname[i]); + } + } + sb.append(".class"); + String classpath = sb.toString(); + File file = new File(classpath); + if (file.exists()) { + return classpath; + } + } + return null; + } +} diff --git a/group12/563253496/week4_jvm1/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group12/563253496/week4_jvm1/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..4127673b7e --- /dev/null +++ b/group12/563253496/week4_jvm1/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "D:\\mygit\\coding2017\\group12\\563253496\\week4_jvm1\\out\\production\\week4_jvm1"; + static String path2 = "C:\\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i +* @since
 29, 2017
+* @version 1.0 +*/ public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group13/2729382520/L4/src/io/github/vxzh/jvm/loader/ClassFileLoader.java b/group13/2729382520/L4/src/io/github/vxzh/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..db6f3b81ef --- /dev/null +++ b/group13/2729382520/L4/src/io/github/vxzh/jvm/loader/ClassFileLoader.java @@ -0,0 +1,79 @@ +package io.github.vxzh.jvm.loader; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + +public class ClassFileLoader { + + private static final int BUFFER_MAX_SIZE = 1024; + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + className = className.replaceAll("\\.", "/"); + File file = findFile(className); + if (file == null) { + return new byte[0]; + } + + FileInputStream fis = null; + ByteArrayOutputStream bos = null; + try { + fis = new FileInputStream(file); + bos = new ByteArrayOutputStream(); + byte buffer[] = new byte[BUFFER_MAX_SIZE]; + int len = -1; + while ((len = fis.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + return bos.toByteArray(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fis != null) + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + if (bos != null) + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return null; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + + StringBuilder builder = new StringBuilder(); + for (String path : clzPaths) { + builder.append(path).append(";"); + } + + return builder.toString().substring(0, builder.toString().length() - 1); + } + + private File findFile(String className) { + for (String path : clzPaths) { + String filePath = path + "/" + className + ".class"; + File file = new File(filePath); + if (file.exists()) { + return file; + } + } + return null; + } +} \ No newline at end of file diff --git a/group13/2729382520/L4/src/io/github/vxzh/jvm/test/ClassFileloaderTest.java b/group13/2729382520/L4/src/io/github/vxzh/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..8f8607162f --- /dev/null +++ b/group13/2729382520/L4/src/io/github/vxzh/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,82 @@ +package io.github.vxzh.jvm.test; + +import io.github.vxzh.jvm.loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + + + static String path1 = "/Users/xuxiaoqing/Workspace/test"; + static String path2 = "/Users/xuxiaoqing/Documents/demo"; + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "io.github.vxzh.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "io.github.vxzh.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group13/2729382520/L4/src/io/github/vxzh/jvm/test/EmployeeV1.java b/group13/2729382520/L4/src/io/github/vxzh/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..d0507a84ac --- /dev/null +++ b/group13/2729382520/L4/src/io/github/vxzh/jvm/test/EmployeeV1.java @@ -0,0 +1,29 @@ +package io.github.vxzh.jvm.test; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + } +} \ No newline at end of file diff --git a/group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrame.java b/group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrame.java new file mode 100644 index 0000000000..f2858f5d69 --- /dev/null +++ b/group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrame.java @@ -0,0 +1,109 @@ +package io.github.vxzh.lru; + +/** + * 用双向链表实现LRU算法 + */ +public class LRUPageFrame { + + private static class Node { + Node prev; + Node next; + int pageNum; + + Node(Node prev, int pageNum, Node next) { + this.prev = prev; + this.pageNum = pageNum; + this.next = next; + } + } + + private int capacity; + private int size; + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if (first != null && first.pageNum == pageNum) { + return; + } + + removeNode(pageNum); + if (size() + 1 > capacity) { + removeLast(); + } + addFirst(pageNum); + + } + + private int size() { + return size; + } + + private void addFirst(int pageNum) { + Node f = first; + Node newNode = new Node(null, pageNum, f); + if (f == null) + last = newNode; + else + f.prev = newNode; + first = newNode; + size++; + } + + private void removeLast() { + + Node l = last; + Node prev = l.prev; + prev.next = null; + l.prev = null; + last = prev; + size--; + } + + private void removeNode(int pageNum) { + Node node = first; + while (node != null) { + if (node.pageNum == pageNum) { + if (node == last) { + removeLast(); + } else { + final Node prev = node.prev; + final Node next = node.next; + prev.next = next; + next.prev = prev; + node.prev = null; + node.next = null; + size--; + } + break; + } else { + node = node.next; + } + } + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrameTest.java b/group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..584632554c --- /dev/null +++ b/group13/2729382520/L4/src/io/github/vxzh/lru/LRUPageFrameTest.java @@ -0,0 +1,29 @@ +package io.github.vxzh.lru; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group13/2931408816/lesson4/build.gradle b/group13/2931408816/lesson4/build.gradle new file mode 100644 index 0000000000..f4062b8d33 --- /dev/null +++ b/group13/2931408816/lesson4/build.gradle @@ -0,0 +1,27 @@ +group 'cn.net.pikachu' +version '1.0-SNAPSHOT' + +buildscript { + ext.kotlin_version = '1.1.1' + + repositories { + mavenCentral() + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java b/group13/2931408816/lesson4/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..6e2869d8ec --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,56 @@ +package com.coderising.jvm.loader; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + try { + String path = className.replaceAll("\\.","/"); + System.out.println(path); +// String base = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); + String base = "D:\\src\\java\\study\\coding2017\\group13\\2931408816\\lesson4\\build\\classes\\main"; + System.out.println(base); + InputStream inputStream = new FileInputStream(base+"/"+path+".class"); + byte[] bytes = new byte[inputStream.available()]; + inputStream.read(bytes); + return bytes; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuilder builder = new StringBuilder(); + for (String s : + clzPaths) { + builder.append(s).append(";"); + } + builder.deleteCharAt(builder.length()-1); + return builder.toString(); +// return null; + } + + + + + +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coderising/jvm/test/EmployeeV1.java b/group13/2931408816/lesson4/src/main/java/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..12e3d7efdd --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/BinaryTreeNode.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group15/1521_653895972/src/com/coding/basic/Iterator.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/Iterator.java similarity index 100% rename from group15/1521_653895972/src/com/coding/basic/Iterator.java rename to group13/2931408816/lesson4/src/main/java/com/coding/basic/Iterator.java diff --git a/group15/1521_653895972/src/com/coding/basic/List.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/List.java similarity index 100% rename from group15/1521_653895972/src/com/coding/basic/List.java rename to group13/2931408816/lesson4/src/main/java/com/coding/basic/List.java diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/Queue.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/Stack.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..459ec560b4 --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/array/ArrayList.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/array/ArrayUtil.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..22a39e5c97 --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,118 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + + public Node(int pageNum) { + this.pageNum = pageNum; + } + } + + private int capacity; + private int curSize = 0; + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if (first == null){ + first = new Node(pageNum); + if (last == null){ + last =first; + } + curSize++; + return; + } + if (curSize < capacity){ + Node node = new Node(pageNum); + node.next=first; + first.prev=node; + first=node; + curSize++; + return; + } + Node node = first; + // 是否已存在 + while (node.next!=null){ + if (node.pageNum == pageNum){ + // 存在即交换 + if (first.pageNum == pageNum){ + return; + } + if (node.prev!=null){ + node.prev.next=node.next; + } + if (node.next!=null){ + node.next.prev=node.prev; + } + node.prev=null; + node.next=first; + first.prev=node; + first=node; + return; + } + node=node.next; + } + // 把最后一个节节点移到开头 + node = last; + last=last.prev; + last.next=null; + node.next=first; + first.prev=node; + first=node; + node.pageNum=pageNum; + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + public static void main(String[] args) { + + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + System.out.println(frame); + frame.access(2); + System.out.println(frame); + } +} diff --git a/group13/2931408816/lesson4/src/main/java/com/coding/basic/linklist/LinkedList.java b/group13/2931408816/lesson4/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/group13/2931408816/lesson4/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group13/2931408816/lesson4/src/test/java/com/coderising/jvm/loader/ClassFileloaderTest.java b/group13/2931408816/lesson4/src/test/java/com/coderising/jvm/loader/ClassFileloaderTest.java new file mode 100644 index 0000000000..8835efde3d --- /dev/null +++ b/group13/2931408816/lesson4/src/test/java/com/coderising/jvm/loader/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i + */ + +public class MyArrayList implements MyList { + + + private int size = 0; + + private final int initialCapacity = 3; + + private Object[] elementData = new Object[100]; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private int modCount = 0; + + /** + * һĬϳʼΪ3Ŀб + * + */ + public MyArrayList() { + elementData = new Object[initialCapacity]; + } + + /** + * һָʼĿб + * @param initialCapacity + */ + public MyArrayList(int initialCapacity) { + + if (initialCapacity < 0){ + throw new IllegalArgumentException("Illegal initialCapacity: "+ initialCapacity); + }else if(initialCapacity == 0){ + elementData = EMPTY_ELEMENTDATA; + }else{ + elementData = new Object[this.initialCapacity]; + } + } + + /** + * + * һָcollectionԪصбЩԪذոcollectionĵǵ˳ + * MySubClassMyClassࡣ + * Collection myCollection; + * Collection mySubCollection; + * ArrayList myList = new ArrayList(myCollection); + * ҲԣArrayList myList = new ArrayList(mySubCollection); + * MyClassMyClassCollectionԹArrayList + * @param c + */ + public MyArrayList(Collection c) { + elementData = c.toArray(); + if((size = elementData.length) != 0){ + //c.toArray might (incorrectly) not return Object[] (see 6260652) + //ٷbugתͲȫ + //Object[]Arrays.copyOfתΪObject[] + if (elementData.getClass() != Object[].class) + elementData = Arrays.copyOf(elementData, size, Object[].class); + }else{ + elementData = EMPTY_ELEMENTDATA; + } + } + + + /** + * ָԪбָλϵԪ + * @param index + * @param element + * @return Object(ǰλڸλϵľԪ) + */ + public Object set(int index, Object element) { + if (index >= size()) + throw new RuntimeException("The Index: "+index+" is out of band."); + + Object oldValue = elementData[index]; + elementData[index] = element; + return oldValue; + } + + /** + * Ԫбβ + * @param e + */ + public void add(Object e) { + if (e == null) { + throw new RuntimeException("The value should not be null."); + } + if (size() >= initialCapacity) { + ensureCapacity(size() + 1); + } + elementData [size] = e; + size++; + } + + /** + * Ԫӵбָλ + * @param index + * @param element + */ + public void add(int index, Object o) { + if (index >= size || index < 0) + throw new RuntimeException("The Index: "+index+" is out of band."); + // 鳤Ȳ㣬ݡ + ensureCapacity(size+1); + // elementDataдIndexλÿʼΪsize-indexԪأ + // ±Ϊindex+1λÿʼµelementDataС + // ǰλڸλõԪԼкԪһλá + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + + /** + * شбָλϵԪ + * @param index + * @return + */ + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("The index:" + index + " is out of band."); + } + return elementData [index]; + } + + /** + * ɾָλԪ + * @param ɾԪλã0ʼ + * @return Object(ɾָλϵľԪ) + */ + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("The index:" + index + " is out of band."); + } + modCount++; + Object oldElement = elementData[index]; + //˴ȻҲSystem.arraycopy ʵ + for (int i = index; i < size - 1; i++) { + elementData [i] = elementData [i + 1]; + } + elementData [size - 1] = null; + size--; + return oldElement; + } + + /** + * ݣÿռΪ 50%+1 + * @param ǰСֵ + */ + private void ensureCapacity(int minCapacity) { + modCount++; + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + //λ㣬൱ڳ2Щ int newCapacity = (oldCapacity * 3)/2 + 1; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + /** + * ListеԪظ. + * @return ListеԪظ + */ + public int size() { + return size; + } + + /** + * ListԪеһ + * @return ListԪеĵ + */ + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor; // һԪصλ + int lastRet = -1; // һԪصλ + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + + if (modCount != expectedModCount){ + throw new RuntimeException("This list is being modified."); + + } + + int i = cursor; + if (i >= size){ + throw new RuntimeException("No such element."); + } + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length){ + throw new RuntimeException("This list is being modified."); + } + + cursor = i + 1; + return elementData[lastRet = i]; + } + + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + + if (modCount != expectedModCount){ + throw new RuntimeException("This list is being modified."); + + } + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new RuntimeException("This list is being modified."); + } + } + } + +} diff --git a/group13/413007522/dataStructure/MyBinaryTree.java b/group13/413007522/dataStructure/MyBinaryTree.java new file mode 100644 index 0000000000..eb836db4d2 --- /dev/null +++ b/group13/413007522/dataStructure/MyBinaryTree.java @@ -0,0 +1,10 @@ +package cn.xl.c1; + +public class MyBinaryTree { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/group13/413007522/dataStructure/MyIterator.java b/group13/413007522/dataStructure/MyIterator.java new file mode 100644 index 0000000000..4e3ed63f16 --- /dev/null +++ b/group13/413007522/dataStructure/MyIterator.java @@ -0,0 +1,10 @@ +package cn.xl.c1; + +public class MyIterator { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/group13/413007522/dataStructure/MyLinkedList.java b/group13/413007522/dataStructure/MyLinkedList.java new file mode 100644 index 0000000000..2eff03b770 --- /dev/null +++ b/group13/413007522/dataStructure/MyLinkedList.java @@ -0,0 +1,218 @@ +package cn.xl.c1; + +import java.util.Iterator; + +/** + * + * @author XIAOLONG + * @param + * + */ +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node first; + + private Node last; + + + /** + * һ޲ι캯һList + * + */ + public MyLinkedList(){ + + } + + + + /** + * Ԫбβ + * @param Object(ӵԪ) + */ + public void add(Object o) { + + addLast(o); + } + + + /** + * бָλԪ,ǾԪ + * @param index λãObject Ԫ + */ + public void add(int index, Object o) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + Node x = node(index); + x.data = o; + + } + + /** + * ȡָλõԪdata + * @param index ҪȡԪλ + */ + public Object get(int index) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + return node(index).data; + } + + + /** + * ɾָλԪ + * @param index ɾбԪλ + */ + public Object remove(int index) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + final Node node = node(index); + final Object o = node.data; + if(first.equals(node)){ + removeFirst(); + }else if(last.equals(node)){ + removeLast(); + }else{ + final Node prev = node.prev; + final Node next = node.next; + + prev.next = next; + next.prev = prev; + node.prev = null; + node.next = null; + } + node.data = null; + size --; + return o; + } + + + /** + * ȡбǰsize + */ + public int size() { + return size; + } + + /** + * ͷԪ,ͷԪΪգøԪͬʱΪβԪ + * @param Object ӵͷԪأ + */ + public void addFirst(Object o){ + + final Node f = first; + final Node newNode = new Node(null,o,f); + if(f == null){ + last = newNode; + }else{ + f.prev = newNode; + } + size ++; + } + + /** + * βԪأβԪΪգͬʱøԪΪͷԪ + * @param Object(ӵβԪ) + */ + public void addLast(Object o){ + + final Node l = last; + final Node newNode = new Node(l,o,null); + if(l == null){ + first = newNode; + }else{ + l.next = newNode; + } + size ++; + } + + /** + * ƳһԪأƳԺбΪ first = next = null + * @return ƳԪ + */ + public Object removeFirst(){ + + final Node f = first; + final Object o = f.data; + final Node next = f.next ; + f.next = null; + f.data = null; + first = next; + if(next == null){ + last = next; + }else{ + next.prev = null; + } + size --; + return o; + } + + /** + * ƳһԪ + * @return ƳԪ + */ + public Object removeLast(){ + + final Node l = last; + final Object o = l.data; + final Node prev = l.prev; + l.data = null; + l.prev = null; + last = prev; + if(prev == null){ + last = null; + }else{ + prev.next = null; + } + size --; + return o; + } + public Iterator iterator(){ + return null; + } + + /** + * Nodeڲ + * + */ + private static class Node{ + Object data; + Node next; + Node prev; + + Node(Node prev,Object o,Node next){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + /** + * һȡ±λnodeķ + * ǰ±Сlistȵһ䣬ͷʼ βʼ + * @param index Ԫصλ + */ + private Node node(int index){ + if (index < (size >> 1)) { + Node x = first; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } +} diff --git a/group13/413007522/dataStructure/MyList.java b/group13/413007522/dataStructure/MyList.java new file mode 100644 index 0000000000..64b7179c01 --- /dev/null +++ b/group13/413007522/dataStructure/MyList.java @@ -0,0 +1,10 @@ + +package cn.xl.c1; + +public interface MyList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group13/413007522/dataStructure/MyQueue.java b/group13/413007522/dataStructure/MyQueue.java new file mode 100644 index 0000000000..99fcd742db --- /dev/null +++ b/group13/413007522/dataStructure/MyQueue.java @@ -0,0 +1,137 @@ +package cn.xl.c1; + +/** + * Queue一个先进先出(first in first out,FIFO)得队列 + * 所谓的队列,也是一个含有至少两个基本操作的抽象数据类型:插入新的元素;删除最久时间插入的元素。 + * 遵循FIFO(First in,first out,先进先出)的原则。 + * MyQueue采用环形数组实现 + * @author XIAOLONG + * + */ +public class MyQueue { + + private int size,head,tail; + + private Object[] elementData; + + private final int initialCapacity = 4; + + public MyQueue(){ + + head = tail = -1; + elementData = new Object[initialCapacity]; + + } + + /** + * 向队列中添加元素 + * @param o + */ + public void enQueue(Object o){ + + ensureCapacity(); + + if( head == -1) { + tail = head = 0; + } + size ++; + elementData[tail] = o; + tail ++; + + + } + + /** + * 删除栈顶元素,并返回旧栈顶元素 + * @return 旧栈顶元素 + */ + public Object deQueue(){ + Object element = elementData[head]; + if(head == tail){ + head = tail = -1; + }else if(head == elementData.length-1){ + head = 0; + }else{ + head ++; + } + size --; + return element; + } + + /** + * 判断队列是否为空 + * @return + */ + public boolean isEmpty(){ + return head == -1; + } + + /** + * 返回自身长度 + * @return + */ + public int size(){ + return size; + } + + /** + * 判断队列是否已满 + * @return + */ + public boolean isFull() { + return (head == 0 && tail == elementData.length); + } + + /** + * 扩展容量,如果队列有效数据已经占满空间则增加2,否则覆盖无效数据,重新分配数据空间 + * @param 当前队列所需最小容量size + */ + private void ensureCapacity(){ + + if(isFull()){ + Object [] oldData = elementData; + elementData = new Object[elementData.length + 2]; + System.arraycopy(oldData, head,elementData , 0, oldData.length); + }else if(head > 0){ + Object [] oldData = elementData; + System.arraycopy(oldData, head,elementData , 0, oldData.length-head); + tail = tail - head ; + head = 0; + } + } + + + public void printAll() { + for(Object i:elementData) + System.out.print(i+" "); + System.out.println(); + } + public static void main(String[] args) + { + MyQueue se=new MyQueue(); + se.enQueue(1); + se.enQueue(2); + se.enQueue(3); + se.enQueue(4); + System.out.println("原始容量下,队列元素为"); + se.printAll(); + + System.out.println("队列满后,继续增加元素5"); + se.enQueue(5); + se.printAll(); + + se.deQueue(); + System.out.println("删除队列首元素1,队列首元素为:"+se.elementData[se.head]); + + se.deQueue(); + + se.enQueue(6); + se.enQueue(7); + se.enQueue(8); + se.enQueue(9); + se.enQueue(10); + se.enQueue(11); + se.printAll(); + + } +} diff --git a/group13/413007522/dataStructure/MyStack.java b/group13/413007522/dataStructure/MyStack.java new file mode 100644 index 0000000000..c28e6b60ac --- /dev/null +++ b/group13/413007522/dataStructure/MyStack.java @@ -0,0 +1,124 @@ +package cn.xl.c1; + +import java.util.Arrays; +import java.util.EmptyStackException; + +/** + * Stackһȳlast in first outLIFOĶջ + * VectorĻչ5 + * @author XIAOLONG + * + */ +public class MyStack { + + private int elementCount; + + private Object[] elementData; + + /** + * ޲ι췽һջ + * + */ + public MyStack(){ + + } + + + /** + * Ԫջ + * @param item + * @return ջԪ + */ + public synchronized Object push(Object item){ + + ensureCapacity(elementCount+1); + elementData[elementCount] = item; + elementCount ++; + return item; + } + + /** + * ջԪƳظԪ + * @return ջԪ + */ + public synchronized Object pop(){ + Object obj; + + obj = peek(); + elementCount --; + elementData[elementCount] = null; + + return obj; + } + + /** + * 鿴ջԪ + * + * @return ջԪ + * @throws ջΪ ׳ EmptyStackException쳣 . + */ + public synchronized Object peek(){ + int len = elementCount; + + if(len == 0) + throw new EmptyStackException(); + + return elementData[len - 1]; + + } + + /** + * ջǷΪ + * + * @return True or false + */ + public boolean isEmpty(){ + + return elementCount == 0; + } + + /** + * ѯռջǷijԪ + * @param ѯԪ + * @return ԪشڷԪλãջԪλΪ1 + * Ԫջظ򷵻ؾջԪλã + * Ԫջвڣ򷵻 -1 + */ + public synchronized int search(Object o){ + + if(o == null){ + for(int i = elementCount -1;i >= 0; i--){ + if(elementData[i] == null){ + return elementCount - i; + } + } + }else{ + for(int i = elementCount -1;i >= 0; i-- ){ + if(o.equals(elementData[i])){ + return elementCount - i; + } + } + } + + return -1; + } + + /** + * չһ + * @param ǰջСsize + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + int newCapacity = oldCapacity << 1; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public static void main(String[] args){ + + + + } + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadStartup.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadStartup.java new file mode 100644 index 0000000000..cbbb593cc9 --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadStartup.java @@ -0,0 +1,14 @@ +package cn.xl.c3; + +public class DownloadStartup { + private static final String encoding = "utf-8"; + + public static void main(String[] args) { + DownloadTask downloadManager = new DownloadTask(5); + String urlStr = "http://imgadmin.voole.com/img/pic/2017/03/21/1000/2017032117552710008ww5f.jpg"; + //downloadManager.setThreadNum(1); + downloadManager.setSleepSeconds(5); + downloadManager.setFileDir("E://"); + downloadManager.download(urlStr, encoding); + } +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadTask.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadTask.java new file mode 100644 index 0000000000..624414daa2 --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadTask.java @@ -0,0 +1,411 @@ +package cn.xl.c3; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class DownloadTask { + public void download(String urlStr, String charset) { + this.charset = charset; + long contentLength = 0; + CountDownLatch latch = new CountDownLatch(threadNum); + long[] startPos = new long[threadNum]; + long endPos = 0; + + + try { + // urlлصļʽ + //String fileSeparator = System.getProperty("file.separator"); + this.fileName = urlStr.substring(urlStr.lastIndexOf("/") + 1); + + this.url = new URL(urlStr); + URLConnection con = url.openConnection(); + setHeader(con); + // õcontentij + contentLength = con.getContentLength(); + // contextΪthreadNumεĻÿεijȡ + this.threadLength = contentLength / threadNum; + + // һصʱļöϵ㣬µĿļڵ4˵ + startPos = setThreadBreakpoint(fileDir, fileName, contentLength, startPos); + + //ڶֶ߳ļ + ExecutorService exec = Executors.newCachedThreadPool(); + for (int i = 0; i < threadNum; i++) { + // ߳ݣÿݵʼλΪ(threadLength * i + س) + startPos[i] += threadLength * i; + + /**//*̵ֹ߳λãһ̼߳Ϊ(threadLength * (i + 1) - 1) + һ̵ֹ߳λüΪݵij*/ + if (i == threadNum - 1) { + endPos = contentLength; + } else { + endPos = threadLength * (i + 1) - 1; + } + // ִ̣߳С + ChildThread thread = new ChildThread(this, latch, i, startPos[i], endPos); + childThreads[i] = thread; + exec.execute(thread); + } + + try { + // ȴCountdownLatchźΪ0ʾ̶߳ + latch.await(); + exec.shutdown(); + + // ѷֶʱļедĿļСڵ3˵ + tempFileToTargetFile(childThreads); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + + ///////// + private long[] setThreadBreakpoint(String fileDir2, String fileName2, + long contentLength, long[] startPos) { + File file = new File(fileDir + fileName); + long localFileSize = file.length(); + + if (file.exists()) { + System.out.println("file " + fileName + " has exists!"); + // صĿļѴڣжĿļǷ + if (localFileSize < contentLength) { + System.out.println("Now download continue "); + + // Ŀļʱļöϵλãÿʱļij + File tempFileDir = new File(fileDir); + File[] files = tempFileDir.listFiles(); + for (int k = 0; k < files.length; k++) { + String tempFileName = files[k].getName(); + // ʱļʽΪĿļ+"_"+ + if (tempFileName != null && files[k].length() > 0 + && tempFileName.startsWith(fileName + "_")) { + int fileLongNum = Integer.parseInt(tempFileName + .substring(tempFileName.lastIndexOf("_") + 1, + tempFileName.lastIndexOf("_") + 2)); + // Ϊÿ߳صλ + startPos[fileLongNum] = files[k].length(); + } + } + } + } else { + // صĿļڣ򴴽ļ + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return startPos; + } + + //// + private void setHeader(URLConnection con) { + con.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3"); + con.setRequestProperty("Accept-Language", "en-us,en; q=0.7,zh-cn; q=0.3"); + con.setRequestProperty("Accept-Encoding", "aa"); + con.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8; q=0.7,*; q=0.7"); + con.setRequestProperty("Keep-Alive", "300"); + con.setRequestProperty("Connection", "keep-alive"); + con.setRequestProperty("If-Modified-Since", "Fri, 02 Jan 2009 17:00:05 GMT"); + con.setRequestProperty("If-None-Match", "\"1261d8-4290-df64d224\""); + con.setRequestProperty("Cache-Control", "max-age=0"); + con.setRequestProperty("Referer", "http://http://www.bt285.cn"); + } + + /// + private void tempFileToTargetFile(ChildThread[] childThreads) { + try { + BufferedOutputStream outputStream = new BufferedOutputStream( + new FileOutputStream(fileDir + fileName)); + + // ̴߳ʱļ˳дĿļ + for (int i = 0; i < threadNum; i++) { + if (statusError) { + for (int k = 0; k < threadNum; k++) { + if (childThreads[k].tempFile.length() == 0) + childThreads[k].tempFile.delete(); + } + System.out.println("񲻳ɹ߳"); + break; + } + + BufferedInputStream inputStream = new BufferedInputStream( + new FileInputStream(childThreads[i].tempFile)); + System.out.println("Now is file " + childThreads[i].id); + int len = 0; + int count = 0; + byte[] b = new byte[1024]; + while ((len = inputStream.read(b)) != -1) { + count += len; + outputStream.write(b, 0, len); + if ((count % 5000) == 0) { + outputStream.flush(); + } + + // b = new byte[1024]; + } + + inputStream.close(); + // ɾʱļ + if (childThreads[i].status == ChildThread.STATUS_HAS_FINISHED) { + childThreads[i].tempFile.delete(); + } + } + + outputStream.flush(); + outputStream.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + //////// + class ChildThread extends Thread { + public static final int STATUS_HASNOT_FINISHED = 0; + public static final int STATUS_HAS_FINISHED = 1; + public static final int STATUS_HTTPSTATUS_ERROR = 2; + private DownloadTask task; + private int id; + private long startPosition; + private long endPosition; + private final CountDownLatch latch; + private File tempFile = null; + // ߳״̬ + private int status = ChildThread.STATUS_HASNOT_FINISHED; + + public ChildThread(DownloadTask task, CountDownLatch latch, int id, long startPos, long endPos) { + super(); + this.task = task; + this.id = id; + this.startPosition = startPos; + this.endPosition = endPos; + this.latch = latch; + + try { + tempFile = new File(this.task.fileDir + this.task.fileName + "_" + id); + if(!tempFile.exists()){ + tempFile.createNewFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public void run() { + System.out.println("Thread " + id + " run "); + HttpURLConnection con = null; + InputStream inputStream = null; + BufferedOutputStream outputStream = null; + int count = 0; + long threadDownloadLength = endPosition - startPosition; + + try { + outputStream = new BufferedOutputStream(new FileOutputStream(tempFile.getPath(), true)); + } catch (FileNotFoundException e2) { + e2.printStackTrace(); + } + + for(; ; ){ + startPosition += count; + try { + // URLConnection + con = (HttpURLConnection) task.url.openConnection(); + setHeader(con); + con.setAllowUserInteraction(true); + // ӳʱʱΪ10000ms + con.setConnectTimeout(10000); + // öȡݳʱʱΪ10000ms + con.setReadTimeout(10000); + + if(startPosition < endPosition){ + // ݵֹ + con.setRequestProperty("Range", "bytes=" + startPosition + "-" + + endPosition); + System.out.println("Thread " + id + " startPosition is " + startPosition); + System.out.println("Thread " + id + " endPosition is " + endPosition); + + // жhttp statusǷΪHTTP/1.1 206 Partial Content200 OK + // ״̬statusΪSTATUS_HTTPSTATUS_ERROR + if (con.getResponseCode() != HttpURLConnection.HTTP_OK + && con.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) { + System.out.println("Thread " + id + ": code = " + + con.getResponseCode() + ", status = " + + con.getResponseMessage()); + status = ChildThread.STATUS_HTTPSTATUS_ERROR; + this.task.statusError = true; + outputStream.close(); + con.disconnect(); + System.out.println("Thread " + id + " finished."); + latch.countDown(); + break; + } + + inputStream = con.getInputStream(); + + int len = 0; + byte[] b = new byte[1024]; + while ((len = inputStream.read(b)) != -1) { + outputStream.write(b, 0, len); + count += len; + + // ÿ5000byteflushһ + if(count % 5000 == 0){ + outputStream.flush(); + } + } + + System.out.println("count is " + count); + if(count >= threadDownloadLength){ + //hasFinished = true; + } + outputStream.flush(); + outputStream.close(); + inputStream.close(); + con.disconnect(); + } + + this.status = this.STATUS_HAS_FINISHED; + //System.out.println("Thread " + id + " finished."); + latch.countDown(); + break; + } catch (IOException e) { + try { + outputStream.flush(); + TimeUnit.SECONDS.sleep(getSleepSeconds()); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } + continue; + } + } + } + } + + public DownloadTask(int threadNum){ + childThreads= new ChildThread[threadNum]; + this.threadNum = threadNum; + } + + + + private String charset; + private int threadNum; + private String fileName; + private URL url; + private Long threadLength; + private String fileDir; + private ChildThread[] childThreads ; + private boolean statusError; + private int SleepSeconds; + public String getCharset() { + return charset; + } + + + public void setCharset(String charset) { + this.charset = charset; + } + + + public int getThreadNum() { + return threadNum; + } + + + public void setThreadNum(int threadNum) { + this.threadNum = threadNum; + } + + + public String getFileName() { + return fileName; + } + + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + + public URL getUrl() { + return url; + } + + + public void setUrl(URL url) { + this.url = url; + } + + + public Long getThreadLength() { + return threadLength; + } + + + public void setThreadLength(Long threadLength) { + this.threadLength = threadLength; + } + + + public String getFileDir() { + return fileDir; + } + + + public void setFileDir(String fileDir) { + this.fileDir = fileDir; + } + + + public boolean isStatusError() { + return statusError; + } + + + public void setStatusError(boolean statusError) { + this.statusError = statusError; + } + + + public int getSleepSeconds() { + return SleepSeconds; + } + + + public void setSleepSeconds(int sleepSeconds) { + SleepSeconds = sleepSeconds; + } + + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadThread.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadThread.java new file mode 100644 index 0000000000..d50c0d3307 --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/DownloadThread.java @@ -0,0 +1,113 @@ +package cn.xl.c3; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URLConnection; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.TimeUnit; + +import cn.xl.c3.DownloadTask.ChildThread; +import cn.xl.c3.api.Connection; + +public class DownloadThread extends Thread{ + + public static final int STATUS_HASNOT_FINISHED = 0; + public static final int STATUS_HAS_FINISHED = 1; + public static final int STATUS_HTTPSTATUS_ERROR = 2; + Connection conn; + int startPos; + int endPos; + int id; + DownloadTask task; + CountDownLatch latch; + File tempFile = null; + // 线程状态码 + public int status = ChildThread.STATUS_HASNOT_FINISHED; + + public DownloadThread( String filePath, Connection conn,int id, int startPos, int endPos, CountDownLatch latch){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.latch = latch; + this.id = id; + try { + tempFile = new File(filePath + "_" + id); + if(!tempFile.exists()){ + tempFile.createNewFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + + public void run(){ + + try { + + InputStream inputStream = null; + BufferedOutputStream outputStream = null; + int count = 0; + long threadDownloadLength = endPos - startPos; + + try { + outputStream = new BufferedOutputStream(new FileOutputStream(tempFile.getPath(), true)); + } catch (FileNotFoundException e2) { + e2.printStackTrace(); + } + + for(; ; ){ + startPos += count; + + System.out.println("the id="+id+"thread ,startPos:"+startPos); + System.out.println("the id="+id+"thread ,endPos:"+endPos); + + inputStream = conn.getInputStream(startPos,endPos); + + int len = 0; + byte[] b = new byte[1024]; + while ((len = inputStream.read(b)) != -1) { + outputStream.write(b, 0, len); + count += len; + + // 每读满5000个byte,往磁盘上flush一下 + if(count % 5000 == 0){ + outputStream.flush(); + } + } + + System.out.println("count is " + count); + if(count >= threadDownloadLength){ + //hasFinished = true; + } + outputStream.flush(); + outputStream.close(); + inputStream.close(); + + this.status = this.STATUS_HAS_FINISHED; + //System.out.println("Thread " + id + " finished."); + latch.countDown(); + break; + + } + + + this.status = this.STATUS_HAS_FINISHED; + latch.countDown(); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/FileDownloader.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/FileDownloader.java new file mode 100644 index 0000000000..df7b2b22ec --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/FileDownloader.java @@ -0,0 +1,215 @@ +package cn.xl.c3; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import cn.xl.c3.DownloadTask.ChildThread; +import cn.xl.c3.api.Connection; +import cn.xl.c3.api.ConnectionException; +import cn.xl.c3.api.ConnectionManager; +import cn.xl.c3.api.DownloadListener; + +public class FileDownloader { + + private String url; + + private String fileName; + + private int threadLength; + + private String fileDir; + + private static final int DOWNLOAD_TRHEAD_NUM = 4; + + private DownloadThread[] childThreads ; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url,String _fileDir) { + this.url = _url; + this.fileDir = _fileDir; + } + + public void execute(){ + + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + + + Connection conn = null; + int[] startPos = new int[DOWNLOAD_TRHEAD_NUM]; + CountDownLatch latch = new CountDownLatch(DOWNLOAD_TRHEAD_NUM); + childThreads = new DownloadThread[DOWNLOAD_TRHEAD_NUM]; + int endPos = 0; + this.fileName = url.substring(url.lastIndexOf("/") + 1); + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + System.out.println("length===="+length); + + this.threadLength = length / DOWNLOAD_TRHEAD_NUM; + + ExecutorService exec = Executors.newCachedThreadPool(); + + // 第一步,分析已下载的临时文件,设置断点,如果是新的下载任务,则建立目标文件。在第4点中说明。 + startPos = setThreadBreakpoint(fileDir, fileName, length, startPos); + + for (int i = 0; i < DOWNLOAD_TRHEAD_NUM; i++) { + startPos[i] += threadLength * i; + if (i == DOWNLOAD_TRHEAD_NUM - 1) { + endPos = length; + } else { + endPos = threadLength * (i + 1) - 1; + } + DownloadThread downloadThread = new DownloadThread( + fileDir+fileName, + cm.open(this.url), + i, + startPos[i], + endPos, + latch); + + childThreads[i] = downloadThread; + exec.execute(downloadThread); + } + + try { + latch.await(); + exec.shutdown(); + tempFileToTargetFile(childThreads); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + + } + } + + } + + private void tempFileToTargetFile(DownloadThread[] childThreads) { + try { + BufferedOutputStream outputStream = new BufferedOutputStream( + new FileOutputStream(fileDir + fileName)); + + // 遍历所有子线程创建的临时文件,按顺序把下载内容写入目标文件中 + for (int i = 0; i < DOWNLOAD_TRHEAD_NUM; i++) { + /*if (statusError) { + for (int k = 0; k < threadNum; k++) { + if (childThreads[k].tempFile.length() == 0) + childThreads[k].tempFile.delete(); + } + System.out.println("本次下载任务不成功,请重新设置线程数。"); + break; + } */ + + BufferedInputStream inputStream = new BufferedInputStream( + new FileInputStream(childThreads[i].tempFile)); + System.out.println("Now is file " + childThreads[i].id); + int len = 0; + int count = 0; + byte[] b = new byte[1024]; + while ((len = inputStream.read(b)) != -1) { + count += len; + outputStream.write(b, 0, len); + if ((count % 5000) == 0) { + outputStream.flush(); + } + + // b = new byte[1024]; + } + + inputStream.close(); + childThreads[i].tempFile.delete(); + // 删除临时文件 + /*if (childThreads[i].status == ChildThread.STATUS_HAS_FINISHED) { + childThreads[i].tempFile.delete(); + } */ + } + + outputStream.flush(); + outputStream.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + private int[] setThreadBreakpoint(String fileDir2, String fileName2, + int contentLength, int[] startPos) { + File file = new File(fileDir + fileName); + long localFileSize = file.length(); + + if (file.exists()) { + System.out.println("file " + fileName + " has exists!"); + // 下载的目标文件已存在,判断目标文件是否完整 + if (localFileSize < contentLength) { + System.out.println("Now download continue "); + + // 遍历目标文件的所有临时文件,设置断点的位置,即每个临时文件的长度 + File tempFileDir = new File(fileDir); + File[] files = tempFileDir.listFiles(); + for (int k = 0; k < files.length; k++) { + String tempFileName = files[k].getName(); + // 临时文件的命名方式为:目标文件名+"_"+编号 + if (tempFileName != null && files[k].length() > 0 + && tempFileName.startsWith(fileName + "_")) { + int fileLongNum = Integer.parseInt(tempFileName + .substring(tempFileName.lastIndexOf("_") + 1, + tempFileName.lastIndexOf("_") + 2)); + // 为每个线程设置已下载的位置 + startPos[fileLongNum] = (int) files[k].length(); + } + } + } + } else { + // 如果下载的目标文件不存在,则创建新文件 + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return startPos; + } + + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/FileDownloaderTest.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/FileDownloaderTest.java new file mode 100644 index 0000000000..df2d0f6d70 --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/FileDownloaderTest.java @@ -0,0 +1,63 @@ +package cn.xl.c3; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import cn.xl.c3.api.ConnectionManager; +import cn.xl.c3.api.DownloadListener; +import cn.xl.c3.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + //String url = "http://image11.m1905.cn/uploadfile/2015/0211/thumb_1___3_20150211064226697882.jpg"; + String url = "http://imgadmin.voole.com/img/pic/2017/03/21/1000/2017032117552710008ww5f.jpg"; + + String filePath = "E:/test/"; + + FileDownloader downloader = new FileDownloader(url,filePath); + + ConnectionManager cm = new ConnectionManagerImpl(); + + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/Connection.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/Connection.java new file mode 100644 index 0000000000..90b52b215f --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/Connection.java @@ -0,0 +1,31 @@ +package cn.xl.c3.api; +import java.io.IOException; +import java.io.InputStream; + +public interface Connection { + + + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + /** + * + * + */ + public InputStream getInputStream(int startPos, int endPos); + + + public void close(); + + + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/ConnectionException.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/ConnectionException.java new file mode 100644 index 0000000000..799a9a4675 --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/ConnectionException.java @@ -0,0 +1,5 @@ +package cn.xl.c3.api; + +public class ConnectionException extends Exception { + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/ConnectionManager.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/ConnectionManager.java new file mode 100644 index 0000000000..95073321b8 --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package cn.xl.c3.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/DownloadListener.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/DownloadListener.java new file mode 100644 index 0000000000..bc233c01de --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/api/DownloadListener.java @@ -0,0 +1,5 @@ +package cn.xl.c3.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/impl/ConnectionImpl.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/impl/ConnectionImpl.java new file mode 100644 index 0000000000..18f29ca6df --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/impl/ConnectionImpl.java @@ -0,0 +1,116 @@ +package cn.xl.c3.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +import cn.xl.c3.api.Connection; +import cn.xl.c3.api.ConnectionException; + + +class ConnectionImpl implements Connection{ + + + URL url; + + static final int BUFFER_SIZE = 1024; + + ConnectionImpl(String _url) throws ConnectionException{ + try { + url = new URL(_url); + } catch (MalformedURLException e) { + + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); + + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + + endPos); + InputStream is = httpConn.getInputStream(); + + + /*byte[] b = new byte[endPos - startPos + 1]; + + is.read(b, 0, endPos - startPos + 1); + + is.close(); + + return b;*/ + + byte[] buff = new byte[BUFFER_SIZE]; + + int totalLen = endPos - startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while(baos.size() < totalLen){ + int len = is.read(buff); + if(len < 0){ + break; + } + baos.write(buff,0, len); + } + + if(baos.size() > totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + /*is.close(); + baos.close(); + httpConn.disconnect();*/ + return baos.toByteArray(); + } + + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return -1; + } + + @Override + public InputStream getInputStream(int startPos, int endPos){ + + HttpURLConnection httpConn = null; + InputStream is = null; + try { + httpConn = (HttpURLConnection)url.openConnection(); + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + + endPos); + is = httpConn.getInputStream(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + //if(httpConn != null) httpConn.disconnect(); + + return is; + } + + @Override + public void close() { + // TODO Auto-generated method stub + + + } + +} diff --git a/group13/413007522/lesson03/src/main/java/cn/xl/c3/impl/ConnectionManagerImpl.java b/group13/413007522/lesson03/src/main/java/cn/xl/c3/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..e5bd712bfb --- /dev/null +++ b/group13/413007522/lesson03/src/main/java/cn/xl/c3/impl/ConnectionManagerImpl.java @@ -0,0 +1,19 @@ +package cn.xl.c3.impl; + + +import cn.xl.c3.api.Connection; +import cn.xl.c3.api.ConnectionException; +import cn.xl.c3.api.ConnectionManager; + + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + + + +} diff --git a/group13/413007522/lesson04/src/main/java/cn/xl/basic/linklist/LRUPageFrame.java b/group13/413007522/lesson04/src/main/java/cn/xl/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..1985dae3fc --- /dev/null +++ b/group13/413007522/lesson04/src/main/java/cn/xl/basic/linklist/LRUPageFrame.java @@ -0,0 +1,127 @@ +package cn.xl.basic.linklist; + +/** + * 用双向链表实现LRU(Least Recently Used 近期最少使用算法) + * @author CoderXLoong + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node(Node _prev,int _pageNum,Node _next) { + this.prev = _prev; + this.pageNum = _pageNum; + this.next = _next; + } + } + + private int capacity; + private int size; + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + if(first != null && first.pageNum == pageNum){ + return; + } + + removeNode(pageNum); + if(size()+1 > capacity){ + removeLast(); + } + addFirst(pageNum); + + } + + + + private int size(){ + return size; + } + + + private void addFirst(int pageNum){ + final Node f = first; + final Node newNode = new Node(null,pageNum,f); + if(f == null){ + last = newNode; + }else{ + f.prev = newNode; + } + first = newNode; + size++; + } + + + private void removeLast(){ + + final Node l = last; + final Node prev = l.prev; + prev.next = null; + l.prev = null; + last = prev; + size --; + } + + + private void removeNode(int pageNum){ + Node node = first; + while(node != null){ + if(node.pageNum == pageNum){ + if(node == last){ + removeLast(); + }else{ + final Node prev = node.prev; + final Node next = node.next; + prev.next = next; + next.prev = prev; + node.prev = null; + node.next = null; + size--; + } + break; + }else{ + node = node.next; + } + } + + + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group13/413007522/lesson04/src/main/java/cn/xl/basic/linklist/LRUPageFrameTest.java b/group13/413007522/lesson04/src/main/java/cn/xl/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..e6f073d942 --- /dev/null +++ b/group13/413007522/lesson04/src/main/java/cn/xl/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package cn.xl.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group13/413007522/lesson04/src/main/java/cn/xl/jvm/loader/ClassFileLoader.java b/group13/413007522/lesson04/src/main/java/cn/xl/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..a8655a919f --- /dev/null +++ b/group13/413007522/lesson04/src/main/java/cn/xl/jvm/loader/ClassFileLoader.java @@ -0,0 +1,69 @@ +package cn.xl.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + + System.out.println("解析Class文件路径:"+getClassPath()+"/"+className.replace('.', '/')+".class"); + + File file = new File(getClassPath()+"/"+className.replace('.', '/')+".class"); + // 如果不存在直接返回 + if (!file.exists()) { + return null; + } + InputStream in = null; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + // 一次读一个字节 + in = new FileInputStream(file); + int tempbyte; + while ((tempbyte = in.read()) != -1) { + baos.write(tempbyte); + } + in.close(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + + return baos.toByteArray(); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuffer sbf = new StringBuffer(); + if(clzPaths.size() >= 1){ + sbf.append(clzPaths.get(0)); + } + for(int i = 1; i < clzPaths.size(); i++){ + sbf.append(";"); + sbf.append(clzPaths.get(i)); + } + + return sbf.toString(); + + } + + + +} diff --git a/group13/413007522/lesson04/src/main/java/cn/xl/jvm/loader/EmployeeV1.java b/group13/413007522/lesson04/src/main/java/cn/xl/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..93642d6066 --- /dev/null +++ b/group13/413007522/lesson04/src/main/java/cn/xl/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package cn.xl.jvm.loader; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group13/413007522/lesson04/src/test/java/jvm/loader/ClassFileloaderTest.java b/group13/413007522/lesson04/src/test/java/jvm/loader/ClassFileloaderTest.java new file mode 100644 index 0000000000..f3b2160c40 --- /dev/null +++ b/group13/413007522/lesson04/src/test/java/jvm/loader/ClassFileloaderTest.java @@ -0,0 +1,93 @@ +package jvm.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import cn.xl.jvm.loader.ClassFileLoader; + + + + + + +public class ClassFileloaderTest { + + + static String path1 = "D:/trainingworkspace/lesson01/target/classes"; + static String path2 = "D://??"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "cn.xl.jvm.loader.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1042, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "cn.xl.jvm.loader.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i list2=new LinkedList<>(); + LinkedList list2=new LinkedList<>(); list2.add(0); list2.add(1); list2.add(2); + list2.add(2); + System.out.println(list2.indexOf(2)); - list2.addLast(3); - list2.remove(0); +// list2.addLast(3); +// list2.remove(0); //list2.removeFirst(); - Iterator ite2=list2.iterator(); - ite2.next(); + /*Iterator ite2=list2.iterator(); while(ite2.hasNext()){ System.out.println(ite2.next()); }*/ - - + } + @Test + public void testArrayList() { //ArrayList - /*ArrayList list1=new ArrayList(); + ArrayList list1=new ArrayList(); + list1.contains(3); list1.add(0); list1.add(1); //list1.add(3, -1);//error @@ -48,6 +62,8 @@ public static void main(String[] args) { Iterator ite=list1.iterator(); while(ite.hasNext()){ System.out.println(ite.next()); - }*/ + } + fail("Not yet implemented"); } + } diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java new file mode 100644 index 0000000000..8ab66288b0 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/DownloadThread.java @@ -0,0 +1,70 @@ +package com.m0312.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +import com.m0312.download.api.Connection; +import com.m0312.download.api.DownloadListener; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String descFilePath; + private CyclicBarrier cyclicBarrier; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public DownloadThread( Connection conn, int startPos, int endPos, + String descFilePath,CyclicBarrier cyclicBarrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.descFilePath=descFilePath; + this.cyclicBarrier=cyclicBarrier; + } + @Override + public void run(){ + try { + /*byte[] bytes=conn.read(startPos, endPos); + os=new FileOutputStream(new File(descFilePath)); + os.write(bytes, startPos, endPos-startPos+1); + cyclicBarrier.await();//等待其他线程 + */ + byte[] buffer = conn.read(startPos , endPos); + RandomAccessFile file = new RandomAccessFile(descFilePath, "rw"); + file.seek(startPos); + file.write(buffer, 0, buffer.length); + file.close(); + cyclicBarrier.await(); + + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } + System.out.println("所有线程都下载完成"); + //通知 FileDownloader ,自己已经做完 + + } +} + + + + + + + diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java new file mode 100644 index 0000000000..b6d9102f96 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloader.java @@ -0,0 +1,129 @@ +package com.m0312.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.m0312.download.api.Connection; +import com.m0312.download.api.ConnectionException; +import com.m0312.download.api.ConnectionManager; +import com.m0312.download.api.DownloadListener; +import com.test.downfile.DownThread; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + private static final int THREAD_NUM = 3; + + //定义几个线程去下载 + final int DOWN_THREAD_NUM = 3; + final String OUT_FILE_NAME = "e:/testfile/down.png"; + InputStream[] isArr = new InputStream[DOWN_THREAD_NUM]; + RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM]; + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + String filename=url.substring(url.lastIndexOf("/")); + String descFilePath="E://testfile//"+filename; + + CyclicBarrier barrier=new CyclicBarrier(THREAD_NUM,new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + + } + }); + /*int every=length/3; + new DownloadThread(conn,0,every-1,descFilePath,barrier).start(); + new DownloadThread(conn,every,every*2-1,descFilePath,barrier).start(); + new DownloadThread(conn,every*2,length-1,descFilePath,barrier).start();*/ + + + isArr[0] = conn.getUrlCon().getInputStream(); + int fileLen = conn.getContentLength(); + System.out.println("网络资源的大小" + fileLen); + + // 每线程应该下载的字节数 + long numPerThred = fileLen / DOWN_THREAD_NUM; + // 整个下载资源整除后剩下的余数取模 + long left = fileLen % DOWN_THREAD_NUM; + int start=0; + int end=0; + for (int i = 0; i < DOWN_THREAD_NUM; i++) { + + // 为每个线程打开一个输入流、一个RandomAccessFile对象, + // 让每个线程分别负责下载资源的不同部分。 + //isArr[0]和outArr[0]已经使用,从不为0开始 + + // 分别启动多个线程来下载网络资源 + if (i == DOWN_THREAD_NUM - 1) { + // 最后一个线程下载指定numPerThred+left个字节 + start=(int) (i * numPerThred); + end=(int) ((i + 1) * numPerThred + + left); + } else { + // 每个线程负责下载一定的numPerThred个字节 + start=(int) (i * numPerThred); + end=(int) ((i + 1) * numPerThred); + + } + new DownloadThread(conn, start, end,OUT_FILE_NAME,barrier).start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloaderTest.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloaderTest.java new file mode 100644 index 0000000000..3c3957f933 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/FileDownloaderTest.java @@ -0,0 +1,62 @@ +package com.m0312.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.m0312.download.api.ConnectionManager; +import com.m0312.download.api.DownloadListener; +import com.m0312.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + +// String url = "http://localhost:8080/test.jpg"; +// String url = "http://120.76.28.31/fileroot/pdf/test/testReport-20160803185703.pdf"; +// String url = "http://127.0.0.3:8082/testdownload.pdf"; + String url = "http://127.0.0.3:8082/applogo.png"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/LinkedListTest.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/LinkedListTest.java new file mode 100644 index 0000000000..b9796b2321 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/LinkedListTest.java @@ -0,0 +1,137 @@ +package com.m0312.download; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.m0226.basic.Iterator; +import com.m0312.download.api.LinkedList; + +public class LinkedListTest { + + LinkedList list; + @Before + public void setUp() throws Exception { + list=new LinkedList(); + } + + public static void traverse(LinkedList list){ + Iterator ite=list.iterator(); + while(ite.hasNext()){ + System.out.print(ite.next()+","); + } + System.out.println("===end==="); + } + + @Test + public void testReverse() { + list.add(1); + list.add(2); + list.add(3); + list.add(4); + traverse(list); + list.reverse(); + traverse(list); + fail("Not yet implemented"); + } + + @Test + public void testRemoveFirstHalf() { + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + traverse(list); + list.removeFirstHalf(); + traverse(list); + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + traverse(list); + list.remove(1,2);//145 + traverse(list); + fail("Not yet implemented"); + } + @Test + public void testRemoveIntInt() { + fail("Not yet implemented"); + } + + @Test + public void testGetElements() { + /* 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] */ + list.add(0); + list.add(1); + list.add(222); + list.add(3); + list.add(444); + list.add(5); +// traverse(list); + LinkedList listindex=new LinkedList(); + listindex.add(2); + listindex.add(4); + int[] result=list.getElements(listindex);//0135 + for (int i : result) { + System.out.println(i); + } + + + fail("Not yet implemented"); + } + + @Test + public void testSubtract() { + list.add(0); + list.add(1); + list.add(222); + list.add(222); + list.add(3); + list.add(444); + list.add(5); + traverse(list); + LinkedList listindex=new LinkedList(); + listindex.add(222); + listindex.add(5); + list.subtract(listindex); + traverse(list); + fail("Not yet implemented"); + } + + @Test + public void testRemoveDuplicateValues() { + list.add(0); + list.add(1); + list.add(22); + list.add(22); + list.add(44); + list.add(5); + list.add(6); + traverse(list); + list.removeDuplicateValues(); + traverse(list); + System.out.println("size : "+list.size()); + fail("Not yet implemented"); + } + + @Test + public void testRemoveRange() { + fail("Not yet implemented"); + } + + @Test + public void testIntersection() { + fail("Not yet implemented"); + } + +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/api/Connection.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/Connection.java new file mode 100644 index 0000000000..c2d347e6f4 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/Connection.java @@ -0,0 +1,27 @@ +package com.m0312.download.api; + +import java.io.IOException; +import java.net.URLConnection; + +public interface Connection { + + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); + public void setUrlCon(URLConnection urlCon); + public URLConnection getUrlCon(); +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/api/ConnectionException.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/ConnectionException.java new file mode 100644 index 0000000000..2b840892e4 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.m0312.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/api/ConnectionManager.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/ConnectionManager.java new file mode 100644 index 0000000000..00a19497b4 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.m0312.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/api/DownloadListener.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/DownloadListener.java new file mode 100644 index 0000000000..0eadca2622 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.m0312.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/api/LinkedList.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/LinkedList.java new file mode 100644 index 0000000000..9b396349a3 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/api/LinkedList.java @@ -0,0 +1,361 @@ +package com.m0312.download.api; +import java.util.NoSuchElementException; +import java.util.Objects; + +import com.m0226.basic.ArrayList; +import com.m0226.basic.Iterator; +import com.m0226.basic.List; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + /** + * 与addLast()是一样的 + */ + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + Node prevNode=head; + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + Node newNode=new Node(); + newNode.data=o; + + newNode.next=curNode; + prevNode.next=newNode; + size++; + break; + } + curNode=curNode.next; + prevNode=prevNode.next; + count++; + } + + + } + public Object get(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + return curNode.data; + } + curNode=curNode.next; + count++; + } + return null; + } + public Object remove(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + Node prevNode=head; + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + prevNode.next=curNode.next; + Object object=curNode.data; + curNode.next=null; + curNode=null; + size--; + return object; + } + curNode=curNode.next; + prevNode=prevNode.next; + count++; + } + return null; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node objNode=new Node(); + objNode.data=o; + if(head==null) head=new Node(); + objNode.next=head.next; + size++; + head.next=objNode; + } + public void addLast(Object o){ + Node objNode=new Node(); + objNode.data=o; + if(head==null) head=new Node(); + + //也可以用iterator迭代,先不用吧 + Node curNode=head; + while(curNode.next!=null){ + curNode=curNode.next; + } + objNode.next=curNode.next; + curNode.next=objNode; + size++; + + } + public Object removeFirst(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node delNode=head.next; + head.next=delNode.next; + size--; + return delNode.data; + } + public Object removeLast(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node prevNode=head; + Node curNode=head.next; + while(curNode!=null){ + if(curNode.next==null){//说明是尾节点 + prevNode.next=curNode.next; + size--; + return curNode.data; + } + curNode=curNode.next; + prevNode=prevNode.next; + } + return null; + } + public Iterator iterator(){ + return new Iterator() { + private Node cur=head!=null?head.next:head; + @Override + public Object next() { + if(cur==null){ + throw new NoSuchElementException(); + } + Object object=cur.data; + cur=cur.next; + return object; + } + + @Override + public boolean hasNext() { + if(cur==null){ + return false; + }else{ + return true; + } + + } + }; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Object[] objarr=new Object[size]; + Node temp=head!=null?head.next:null; + int count=0; + while(temp!=null){ + objarr[count]=temp.data; + temp=temp.next; + count++; + } + temp=head; + for(int j=objarr.length-1;j>=0;j--){ + Node node=new Node(); + node.data=objarr[j]; + temp.next=node; + temp=node; + } + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(head==null||head.next==null)return; + int delenum=size/2; + int i=0; + Node temp=null; + Node nextNode=head.next; + while(isize-1||i<0) + throw new IndexOutOfBoundsException("Joy Index: "+i+", Size: "+size); + //如果i之后不足length个元素,该怎么处理,抛出异常,还是仅将剩下的移除 + int j=0; + int deleLen=0;//记录删除的个数 + + Node temp=null; + Node nextNode=head.next; + Node preNode=head; + //将node指针移动到i + while(j101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + Iterator ite=list.iterator(); + int[] result=new int[list.size]; + int index; + int i=0; + while(ite.hasNext()){ + index=(int) ite.next(); + if(index>=size){ + throw new IndexOutOfBoundsException("Joy Index: "+index+", Size: "+size); + } + result[i]=(int) get(index); + i++; + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + Iterator ite=list.iterator(); + int index=-1; + while(ite.hasNext()){ + Object obj=ite.next(); + index=indexOf(obj); + while(index>=0){ + remove(index); + size--; + index=indexOf(obj);//防止当前链表有重复元素 + } + } + } + /** + * 返回该值的索引,如果存在 + * @param o + * 2017年3月11日 下午5:42:15 + * @Author Joy + */ + public int indexOf(Object o){ + if(head==null||head.next==null) return -1; + int index=0; + + if(o==null){ + for(Node temp=head.next;temp!=null;temp=temp.next){ + if(temp.data==null){ + return index; + } + index++; + } + }else{ + for(Node temp=head.next;temp!=null;temp=temp.next){ + if(o.equals(temp.data)){ + return index; + } + index++; + } + } + return -1; + } + + /** + * ????? + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(head==null||head.next==null) return; + //递增 + Node cur=head.next; + Node nextNode=null; + Node temp=null; + for( ;cur!=null;cur=cur.next){ + nextNode=cur.next; + while(Objects.equals(cur.data, nextNode.data)){ + temp=nextNode; + nextNode=nextNode.next; + temp.next=null; + temp=null; + size--; + } + cur.next=nextNode; + System.out.println(nextNode.data+"*** size:"+size+",cur.next :"); + } + System.out.println("size : "+size); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionImpl.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..a3228c80b9 --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionImpl.java @@ -0,0 +1,41 @@ +package com.m0312.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; + +import com.m0312.download.api.Connection; + +public class ConnectionImpl implements Connection{ + URLConnection urlCon; + @Override + public byte[] read(int startPos, int endPos) throws IOException { + byte[] buffer=new byte[endPos-startPos]; + InputStream is=urlCon.getInputStream(); + is.skip(startPos); + is.read(buffer, 0, endPos-startPos); + is.close(); + return buffer; + } + + @Override + public int getContentLength() { + return urlCon.getContentLength(); + } + + @Override + public void close() { + if(urlCon!=null){ + //??? + } + } + @Override + public URLConnection getUrlCon() { + return urlCon; + } + @Override + public void setUrlCon(URLConnection urlCon) { + this.urlCon = urlCon; + } + +} diff --git a/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java b/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..142b40f2ad --- /dev/null +++ b/group14/1091149131/2017JavaPro/src/com/m0312/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,28 @@ +package com.m0312.download.impl; + +import java.io.File; +import java.net.URL; + +import com.m0312.download.api.Connection; +import com.m0312.download.api.ConnectionException; +import com.m0312.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + + @Override + public Connection open(String url) throws ConnectionException { + Connection con=new ConnectionImpl(); + + try { + URL website = new URL(url); + con.setUrlCon(website.openConnection());//urlcon是真正可以用的con连接 + + } catch (Exception e) { + } + + return con; + } + + +} diff --git a/group14/187114392/work_1_20170225/ReadMe.md b/group14/187114392/homework/ReadMe.md similarity index 100% rename from group14/187114392/work_1_20170225/ReadMe.md rename to group14/187114392/homework/ReadMe.md diff --git a/group14/187114392/work_1_20170225/src/Main.java b/group14/187114392/homework/src/Main.java similarity index 100% rename from group14/187114392/work_1_20170225/src/Main.java rename to group14/187114392/homework/src/Main.java diff --git a/group14/187114392/homework/src/com/array/ArrayUtil.java b/group14/187114392/homework/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..e5ddb476a6 --- /dev/null +++ b/group14/187114392/homework/src/com/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group14/187114392/homework/src/com/coderising/download/DownloadThread.java b/group14/187114392/homework/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..342b917a3f --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,41 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + + +public class DownloadThread extends Thread{ + + Connection conn; + CountDownLatch latch; + String localpath; + RandomAccessFile raf; + int startPos; + int endPos; + + public DownloadThread(Connection conn, int startPos, int endPos, String localpath ,RandomAccessFile raf , CountDownLatch latch){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.latch = latch; + this.localpath = localpath; + this.raf = raf; + } + + public void run(){ + try { + RandomAccessFile raf = new RandomAccessFile(localpath,"rwd"); + byte[] slice_bytes = conn.read(startPos, endPos); + raf.seek(startPos); + raf.write(slice_bytes,0,slice_bytes.length); + raf.close(); + latch.countDown(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + } + } +} diff --git a/group14/187114392/homework/src/com/coderising/download/FileDownloader.java b/group14/187114392/homework/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f79497f4e5 --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + +// new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} \ No newline at end of file diff --git a/group14/187114392/homework/src/com/coderising/download/FileDownloader_real.java b/group14/187114392/homework/src/com/coderising/download/FileDownloader_real.java new file mode 100644 index 0000000000..0ede039aeb --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/FileDownloader_real.java @@ -0,0 +1,99 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + + +public class FileDownloader_real { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + CountDownLatch latch; + + String localpath; + + int thread_count; + + public FileDownloader_real(String _url, int thread_count , String localpath , CountDownLatch latch) { + this.url = _url; + this.thread_count = thread_count; + this.latch = latch; + this.localpath = localpath; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + System.out.printf("length is :%s \n" ,length); + int slice_size = length / thread_count; + RandomAccessFile raf = new RandomAccessFile(localpath,"rwd"); + raf.setLength(length); + raf.close(); + for (int i = 0; i < thread_count; i++) { + int start_pos = i * slice_size; + int end_pos = start_pos + slice_size - 1; + if (i == thread_count - 1) { + end_pos = length - 1; + } + Connection conn_t = cm.open(this.url); + new DownloadThread(conn_t,start_pos,end_pos,localpath,raf,latch).start(); + } + latch.await(); + } + catch (ConnectionException e) { + e.printStackTrace(); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + finally { + if(conn != null){ + conn.close(); + } + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group14/187114392/homework/src/com/coderising/download/api/Connection.java b/group14/187114392/homework/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group14/187114392/homework/src/com/coderising/download/api/ConnectionException.java b/group14/187114392/homework/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group14/187114392/homework/src/com/coderising/download/api/ConnectionManager.java b/group14/187114392/homework/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group14/187114392/homework/src/com/coderising/download/api/DownloadListener.java b/group14/187114392/homework/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group14/187114392/homework/src/com/coderising/download/impl/ConnectionImpl.java b/group14/187114392/homework/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..dc27cee4f7 --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,47 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + HttpURLConnection urlConnection = null; + + public ConnectionImpl(HttpURLConnection urlConnection) { + this.urlConnection = urlConnection; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + urlConnection.setRequestMethod("GET"); + urlConnection.setRequestProperty("Range","bytes=" + startPos + "-" + endPos); + urlConnection.setConnectTimeout(5000); + ByteArrayOutputStream buffer_array = new ByteArrayOutputStream(endPos - startPos); + if (urlConnection.getResponseCode() == 206) { + InputStream inputStream = urlConnection.getInputStream(); + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + buffer_array.write(buffer,0,len); + } + System.out.printf("input stream ,startp :%s , endp:%s , result length is :%d \n",startPos,endPos,buffer_array.size()); + inputStream.close(); + buffer_array.close(); + } + urlConnection.disconnect(); + return buffer_array.toByteArray(); + } + + @Override + public int getContentLength() { + return urlConnection.getContentLength(); + } + + @Override + public void close() { + urlConnection.disconnect(); + } + +} diff --git a/group14/187114392/homework/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group14/187114392/homework/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..a3cba48a51 --- /dev/null +++ b/group14/187114392/homework/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,44 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.SystemDefaultCredentialsProvider; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { +// HttpGet request = new HttpGet(url); +// String result = ""; +// try { +// HttpResponse response = HttpClients.createDefault().execute(request); +// if(response.getStatusLine().getStatusCode()==200){ +// result = EntityUtils.toString(response.getEntity()); +// } +// System.out.println("result length is " + result.length()); +// } catch (IOException e) { +// e.printStackTrace(); +// } + ConnectionImpl conn_impl = null; + + try { + URL url_path = new URL(url); + HttpURLConnection urlconnection = (HttpURLConnection) url_path.openConnection(); + conn_impl = new ConnectionImpl(urlconnection); + } catch (IOException e) { + + } + return conn_impl; + } + +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/ArrayList.java b/group14/187114392/homework/src/com/coding/basic/ArrayList.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/ArrayList.java rename to group14/187114392/homework/src/com/coding/basic/ArrayList.java diff --git a/group14/187114392/homework/src/com/coding/basic/BinaryTreeNode.java b/group14/187114392/homework/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group14/187114392/homework/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group14/187114392/homework/src/com/coding/basic/Iterator.java b/group14/187114392/homework/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group14/187114392/homework/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/LinkedList.java b/group14/187114392/homework/src/com/coding/basic/LinkedList.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/LinkedList.java rename to group14/187114392/homework/src/com/coding/basic/LinkedList.java diff --git a/group14/187114392/homework/src/com/coding/basic/List.java b/group14/187114392/homework/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group14/187114392/homework/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/Queue.java b/group14/187114392/homework/src/com/coding/basic/Queue.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/Queue.java rename to group14/187114392/homework/src/com/coding/basic/Queue.java diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/Stack.java b/group14/187114392/homework/src/com/coding/basic/Stack.java similarity index 100% rename from group14/187114392/work_1_20170225/src/com/coding/basic/Stack.java rename to group14/187114392/homework/src/com/coding/basic/Stack.java diff --git a/group14/187114392/work_1_20170225/test/ArrayList_Test.java b/group14/187114392/homework/test/ArrayList_Test.java similarity index 100% rename from group14/187114392/work_1_20170225/test/ArrayList_Test.java rename to group14/187114392/homework/test/ArrayList_Test.java diff --git a/group14/187114392/homework/test/FileDownloaderTest.java b/group14/187114392/homework/test/FileDownloaderTest.java new file mode 100644 index 0000000000..fa1263d487 --- /dev/null +++ b/group14/187114392/homework/test/FileDownloaderTest.java @@ -0,0 +1,107 @@ +import com.coderising.download.FileDownloader; +import com.coderising.download.FileDownloader_real; +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionImpl; +import com.coderising.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.util.concurrent.CountDownLatch; +import java.io.IOException; + +public class FileDownloaderTest { + boolean downloadFinished = false; + int thread_count; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testFirstHttpGet() { + + String url = "http://127.0.0.1/test/climb.jpg"; + + ConnectionManager cm = new ConnectionManagerImpl(); + Connection conn = null; + try { + conn = cm.open(url); + Integer content_length = conn.getContentLength(); + } catch (ConnectionException e) { + e.printStackTrace(); + } + } + + @Test + public void testDownload() { + thread_count = 10; +// String url = "http://localhost:8080/test.jpg"; + CountDownLatch latch = new CountDownLatch(thread_count); + + String url = "http://127.0.0.1/test/climb.jpg"; + String localpath = "G:\\Projects\\187114392\\haha.jpg"; + FileDownloader_real downloader = new FileDownloader_real(url,thread_count,localpath, latch); + + ConnectionManager cm = new ConnectionManagerImpl(); + + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + downloader.execute(); + // 等待多线程下载程序执行完毕 +// while (!downloadFinished) { +// try { +// System.out.println("还没有下载完成,休眠五秒"); +// //休眠5秒 +// Thread.sleep(5000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } + System.out.println("下载完成!"); + try { + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testIdeaJarDownload2() { + thread_count = 9; + CountDownLatch latch = new CountDownLatch(thread_count); + + String filename = "idea.jar"; + String url = "http://127.0.0.1/test/" + filename; + String localpath = "G:\\Projects\\187114392\\" + filename; + FileDownloader_real downloader = new FileDownloader_real(url,thread_count,localpath, latch); + ConnectionManager cm = new ConnectionManagerImpl(); + + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + downloader.execute(); + System.out.println("下载完成!"); + } + + + +} diff --git a/group14/187114392/work_1_20170225/test/LinkedList_Test.java b/group14/187114392/homework/test/LinkedList_Test.java similarity index 100% rename from group14/187114392/work_1_20170225/test/LinkedList_Test.java rename to group14/187114392/homework/test/LinkedList_Test.java diff --git a/group14/187114392/work_1_20170225/test/Queue_Test.java b/group14/187114392/homework/test/Queue_Test.java similarity index 100% rename from group14/187114392/work_1_20170225/test/Queue_Test.java rename to group14/187114392/homework/test/Queue_Test.java diff --git a/group14/187114392/work_1_20170225/test/Stack_Test.java b/group14/187114392/homework/test/Stack_Test.java similarity index 100% rename from group14/187114392/work_1_20170225/test/Stack_Test.java rename to group14/187114392/homework/test/Stack_Test.java diff --git a/group14/187114392/work_1_20170225/.classpath b/group14/187114392/work_1_20170225/.classpath deleted file mode 100644 index fceb4801b5..0000000000 --- a/group14/187114392/work_1_20170225/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/group14/187114392/work_1_20170225/.gitignore b/group14/187114392/work_1_20170225/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/group14/187114392/work_1_20170225/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/group14/187114392/work_1_20170225/.idea/.name b/group14/187114392/work_1_20170225/.idea/.name deleted file mode 100644 index 9769cfad31..0000000000 --- a/group14/187114392/work_1_20170225/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -2017Learning \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/compiler.xml b/group14/187114392/work_1_20170225/.idea/compiler.xml deleted file mode 100644 index 96cc43efa6..0000000000 --- a/group14/187114392/work_1_20170225/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/copyright/profiles_settings.xml b/group14/187114392/work_1_20170225/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3377..0000000000 --- a/group14/187114392/work_1_20170225/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/encodings.xml b/group14/187114392/work_1_20170225/.idea/encodings.xml deleted file mode 100644 index 97626ba454..0000000000 --- a/group14/187114392/work_1_20170225/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/junitgenerator-prj-settings.xml b/group14/187114392/work_1_20170225/.idea/junitgenerator-prj-settings.xml deleted file mode 100644 index b6bb9db746..0000000000 --- a/group14/187114392/work_1_20170225/.idea/junitgenerator-prj-settings.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/misc.xml b/group14/187114392/work_1_20170225/.idea/misc.xml deleted file mode 100644 index 6a48f33378..0000000000 --- a/group14/187114392/work_1_20170225/.idea/misc.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/modules.xml b/group14/187114392/work_1_20170225/.idea/modules.xml deleted file mode 100644 index f37bb20093..0000000000 --- a/group14/187114392/work_1_20170225/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/uiDesigner.xml b/group14/187114392/work_1_20170225/.idea/uiDesigner.xml deleted file mode 100644 index e96534fb27..0000000000 --- a/group14/187114392/work_1_20170225/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/work_1_20170225.iml b/group14/187114392/work_1_20170225/.idea/work_1_20170225.iml deleted file mode 100644 index d6ebd48059..0000000000 --- a/group14/187114392/work_1_20170225/.idea/work_1_20170225.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/workspace.xml b/group14/187114392/work_1_20170225/.idea/workspace.xml deleted file mode 100644 index 8c5615cc95..0000000000 --- a/group14/187114392/work_1_20170225/.idea/workspace.xml +++ /dev/null @@ -1,1295 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1488028819234 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.project b/group14/187114392/work_1_20170225/.project deleted file mode 100644 index fab8d7f04c..0000000000 --- a/group14/187114392/work_1_20170225/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - 2017Learning - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group14/187114392/work_1_20170225/.settings/org.eclipse.jdt.core.prefs b/group14/187114392/work_1_20170225/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 3a21537071..0000000000 --- a/group14/187114392/work_1_20170225/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group14/187114392/work_1_20170225/2017Learning.iml b/group14/187114392/work_1_20170225/2017Learning.iml deleted file mode 100644 index c4678227ee..0000000000 --- a/group14/187114392/work_1_20170225/2017Learning.iml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group14/598808350/2017project/src/com/coderising/download/DownloadThread.java b/group14/598808350/2017project/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..d17f1a9a0c --- /dev/null +++ b/group14/598808350/2017project/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,40 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String localFile; + CyclicBarrier barrier; + + public DownloadThread( Connection conn, int startPos, int endPos,String localFile,CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + try{ + System.out.println("begin to read ["+startPos+"-"+endPos+"]"); + byte[] data = conn.read(startPos, endPos); + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + file.seek(startPos); + file.write(data); + + file.close(); + conn.close(); + barrier.await(); + + }catch(Exception e){ + e.printStackTrace(); + } + } +} diff --git a/group14/598808350/2017project/src/com/coderising/download/FileDownloader.java b/group14/598808350/2017project/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3e8124f0b --- /dev/null +++ b/group14/598808350/2017project/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,127 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + private String url; + + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int DOWNLOAD_TRHEAD_NUM = 3; + + public FileDownloader(String _url,String localFile) { + this.url = _url; + this.localFile = localFile; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM,new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + + + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + createPlaceHolderFile(this.localFile,length); + + int[][] rangs = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM,length); + for(int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + URLConnection conn = null; + try { + conn = url.openConnection(); + return conn.getContentLength(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group14/598808350/2017project/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group14/598808350/2017project/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..0ebb1aad18 --- /dev/null +++ b/group14/598808350/2017project/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + + @Override + public Connection open(String urlStr) throws ConnectionException { + return new ConnectionImpl(urlStr); + } + +} diff --git a/group14/598808350/2017project/src/com/coderising/linkedlist/LinkedList.java b/group14/598808350/2017project/src/com/coderising/linkedlist/LinkedList.java new file mode 100644 index 0000000000..ce395d359d --- /dev/null +++ b/group14/598808350/2017project/src/com/coderising/linkedlist/LinkedList.java @@ -0,0 +1,330 @@ +package com.coderising.linkedlist; + +import java.util.Stack; + +public class LinkedList { + + private Node head; + private int size; + + public LinkedList(){ + this.head = new Node(null,null); + } + public void add(Object o){ + Node lastNode = head; + for(int i=0;i7->10 , ���ú��Ϊ 10->7->3 + */ + public void reverse(){ + Stack nodes = new Stack(); + + Node currentNode = head; + while(currentNode != null){ + nodes.push(currentNode); + Node nextNode = currentNode.next; + currentNode.next = null; + currentNode = nextNode; + } + head = nodes.pop(); + currentNode = head; + + while(!nodes.isEmpty()){ + Node nextNode = nodes.pop(); + currentNode.next = nextNode; + currentNode = nextNode; + + } + } + + /** + * ɾ��һ���������ǰ�벿�� + * ���磺list = 2->5->7->8 , ɾ���Ժ��ֵΪ 7->8 + * ���list = 2->5->7->8->10 ,ɾ���Ժ��ֵΪ7,8,10 + + */ + public void removeFirstHalf(){ + int l = size/2; + for(int i=0;i s){ + stNode = node.next; + s++; + } + return stNode; + } + + /** + * �ٶ���ǰ�����list������������е����� + * �ӵ�ǰ������ȡ����Щlist��ָ����Ԫ�� + * ���統ǰ���� = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * ���صĽ��Ӧ����[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list,Integer[] listB){ + int [] result = new int[list.size()]; + int res_index = 0; + for(int i=0;i min){ + start = index; + } + if((int)node.data < max){ + end = index; + break; + } + node = node.next; + index++; + } + + for(int i=start;i" + endPos); + } + public void run(){ + try { + File file = new File("test.jpg"); + RandomAccessFile out = null; + if (file != null) { + out = new RandomAccessFile(file,"rwd"); + } + + byte[] buffer = new byte[1024]; + /* out.seek(startPos); + out.write(conn.read(startPos,endPos));*/ + InputStream in = conn.getHttpURLConnection().getInputStream(); + in.skip(startPos); + int len = 0; + while ((len = in.read(buffer)) != 1) { + if (len < 0) { + break; + }else { + //System.out.println("len length"+len); + out.write(buffer, 0, len); + } + } + + out.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1502_1617273078/data-structure/src/com/coderising/download/FileDownloader.java b/group15/1502_1617273078/data-structure/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c0750bfe65 --- /dev/null +++ b/group15/1502_1617273078/data-structure/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,97 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import java.io.IOException; +import java.io.RandomAccessFile; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + public static synchronized void writefile(int index, RandomAccessFile randomAccessFile, byte[] bytes) throws IOException { + randomAccessFile.seek(index); + randomAccessFile.write(bytes); + } + + public void execute(int threadnum) throws IOException { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + + + try { + + conn = cm.open(this.url); + + int filelength = conn.getContentLength(); + //randomAccessFile.setLength(filelength); + int[] index = new int[threadnum+1]; + for (int i = 0; i totalLen) { + byte[] datas = baos.toByteArray(); + return Arrays.copyOf(datas, totalLen); + } + + + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + int length = httpURLConnection.getContentLength(); + return length; + } + + @Override + public void close() { + httpURLConnection.disconnect(); + + } + + public void setHttpURLConnection(HttpURLConnection httpURLConnection) { + this.httpURLConnection = httpURLConnection; + } + + public HttpURLConnection getHttpURLConnection() { + return httpURLConnection; + } +} diff --git a/group15/1502_1617273078/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group15/1502_1617273078/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..d894491a1c --- /dev/null +++ b/group15/1502_1617273078/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,30 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException, IOException { + URL url1 = new URL(url); + HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); + conn.setRequestMethod("GET"); + //conn.setRequestProperty(); + conn.setConnectTimeout(10*1000); + //防止屏蔽程序抓取而返回403错误 + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + ConnectionImpl connection = new ConnectionImpl(); + connection.setHttpURLConnection(conn); + //conn.connect(); + //conn.getContentLength(); + return connection; + } + +} diff --git a/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/LoginAction.java b/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/Struts.java b/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/Struts.java similarity index 100% rename from group15/1502_1617273078/src/com/coderising/litestruts/Struts.java rename to group15/1502_1617273078/data-structure/src/com/coderising/litestruts/Struts.java diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java b/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/StrutsTest.java similarity index 100% rename from group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java rename to group15/1502_1617273078/data-structure/src/com/coderising/litestruts/StrutsTest.java diff --git a/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/View.java b/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1502_1617273078/data-structure/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group15/1502_1617273078/src/com/coding/basic/ArrayList.java b/group15/1502_1617273078/data-structure/src/com/coding/basic/ArrayList.java similarity index 100% rename from group15/1502_1617273078/src/com/coding/basic/ArrayList.java rename to group15/1502_1617273078/data-structure/src/com/coding/basic/ArrayList.java diff --git a/group15/1502_1617273078/data-structure/src/com/coding/basic/Iterator.java b/group15/1502_1617273078/data-structure/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1502_1617273078/data-structure/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group15/1502_1617273078/data-structure/src/com/coding/basic/LinkedList.java b/group15/1502_1617273078/data-structure/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..84e3e5fc23 --- /dev/null +++ b/group15/1502_1617273078/data-structure/src/com/coding/basic/LinkedList.java @@ -0,0 +1,382 @@ +package com.coding.basic; + + + +public class LinkedList implements List { + + private Node head; + private int thesize; + + + public void add(Object o){ + if (head == null) { + head = new Node(o,null); + /* head.data = o; + head.next = null;*/ + thesize++; + } else { + addLast(o); + } + } + public void add(int index , Object o){ + if (index > thesize) { + throw new IndexOutOfBoundsException(); + } else if (index == thesize) { + addLast(o); + } else if(index= thesize) { + throw new IndexOutOfBoundsException(); + } else if(index==0){ + return head.data; + } else + { + Node x = head; + for (int j = 1; j 7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + int cusize = thesize; + //创建list副本,内容一致 + LinkedList listbak=new LinkedList(); + for (int i = 0; i 5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + Node node=head; + int sign=thesize; + for (int i = 1; i <=sign/2 ; i++) { + node = node.next; + thesize--; + + } + head = node; + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始(删除的元素包括了i) + * @param i + * @param length + */ + public void remove(int i, int length){ + Node node = head; + if (i == 0) { + for (int j = 1; j <=length ; j++) { + node = node.next; + thesize--; + } + head = node; + } else if (i != 0 && length < thesize-i) { + int sizesign = thesize; + Node f; + Node l; + for (int j =1; j 101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] res = new int[list.size()]; + for (int i = 0; i max){ + + } else if (min < (Integer) (get(size() - 1))&&(Integer) (get(size() - 1))(Integer) (get(0))){ + Node node = head; + if ((Integer) head.data > min) { + Node newhead = new Node(null,null); + head = newhead; + thesize = 0; + }else + for (int i = 1; i min) { + node.next = null; + thesize = i + 1; + }else { + node = node.next; + } + } + } else if (min < (Integer) (get(0))&&(Integer) (get(0)) max) { + head = node; + thesize = thesize - i; + break; + } + } + }else { + Node node = head; + Node nodemin=null; + Node nodemax = null; + int minsign = 0; + int maxsign=0; + for (int i = 1; i min) { + nodemin = node; + minsign = i-1; + break; + } + node = node.next; + } + for (int i = 1; i max) { + nodemax = node.next; + maxsign = i+1; + break; + } + node = node.next; + } + nodemin.next = nodemax; + System.out.println(minsign); + System.out.println(maxsign); + thesize = thesize - (maxsign - minsign); + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList list1 = new LinkedList(); + for (int i = 0; i thesize) { + throw new IndexOutOfBoundsException(); + } else if (index == thesize) { + addLast(o); + } else if(index= thesize) { + throw new IndexOutOfBoundsException(); + } else{ + Node x = head; + int i=0; + do { + x = x.next; + i++; + } while (i == index); + return x.data; + } + } + public Object remove(int index){ + Node x = head; + for (int i = 1; i thesize) { + throw new IndexOutOfBoundsException(); + } else if (index == thesize) { + addLast(o); + } else if(index= thesize) { + throw new IndexOutOfBoundsException(); + } else if(index==0){ + return head.data; + } else + { + Node x = head; + for (int j = 1; j 7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + int cusize = thesize; + //创建list副本,内容一致 + LinkedList listbak=new LinkedList(); + for (int i = 0; i 5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + Node node=head; + int sign=thesize; + for (int i = 1; i <=sign/2 ; i++) { + node = node.next; + thesize--; + + } + head = node; + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始(删除的元素包括了i) + * @param i + * @param length + */ + public void remove(int i, int length){ + Node node = head; + if (i == 0) { + for (int j = 1; j <=length ; j++) { + node = node.next; + thesize--; + } + head = node; + } else if (i != 0 && length < thesize-i) { + int sizesign = thesize; + Node f; + Node l; + for (int j =1; j 101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] res = new int[list.size()]; + for (int i = 0; i max){ + + } else if (min < (Integer) (get(size() - 1))&&(Integer) (get(size() - 1))(Integer) (get(0))){ + Node node = head; + if ((Integer) head.data > min) { + Node newhead = new Node(null,null); + head = newhead; + thesize = 0; + }else + for (int i = 1; i min) { + node.next = null; + thesize = i + 1; + }else { + node = node.next; + } + } + } else if (min < (Integer) (get(0))&&(Integer) (get(0)) max) { + head = node; + thesize = thesize - i; + break; + } + } + }else { + Node node = head; + Node nodemin=null; + Node nodemax = null; + int minsign = 0; + int maxsign=0; + for (int i = 1; i min) { + nodemin = node; + minsign = i-1; + break; + } + node = node.next; + } + for (int i = 1; i max) { + nodemax = node.next; + maxsign = i+1; + break; + } + node = node.next; + } + nodemin.next = nodemax; + System.out.println(minsign); + System.out.println(maxsign); + thesize = thesize - (maxsign - minsign); + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList list1 = new LinkedList(); + for (int i = 0; i +* @since
03/12/2017
+* @version 1.0 +*/ +public class LinkedListTest extends TestCase { +public LinkedListTest(String name) { +super(name); +} + +public void setUp() throws Exception { +super.setUp(); +} + +public void tearDown() throws Exception { +super.tearDown(); +} + +/** +* +* Method: add(Object o) +* +*/ +public void testAddO() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: add(int index, Object o) +* +*/ +public void testAddForIndexO() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: get(int index) +* +*/ +public void testGet() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: remove(int index) +* +*/ +public void testRemoveIndex() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: size() +* +*/ +public void testSize() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: addFirst(Object o) +* +*/ +public void testAddFirst() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: addLast(Object o) +* +*/ +public void testAddLast() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: removeFirst() +* +*/ +public void testRemoveFirst() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: removeLast() +* +*/ +public void testRemoveLast() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: iterator() +* +*/ +public void testIterator() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: reverse() +* +*/ +public void testReverse() throws Exception { +//TODO: Test goes here... + LinkedList list = new LinkedList(); + list.add(3); + list.add(8); + list.add(10); + list.reverse(); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next()+" "); + } +} + +/** +* +* Method: removeFirstHalf() +* +*/ +public void testRemoveFirstHalf() throws Exception { +//TODO: Test goes here... + LinkedList list = new LinkedList(); + list.add(2); + + list.add(3); + list.add(8); + list.add(10); + list.add(11); + + list.removeFirstHalf(); + // linklist.addFirst(2); + //System.out.println(linklist.size()); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + // String s= new String(iterator.next()); + System.out.print(iterator.next()+" "); + } +} + +/** +* +* Method: remove(int i, int length) +* +*/ +public void testRemoveForILength() throws Exception { +//TODO: Test goes here... + LinkedList list = new LinkedList(); + list.add(2); + + list.add(3); + list.add(8); + list.add(10); + list.add(11); + + list.remove(3,6); + // linklist.addFirst(2); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + // String s= new String(iterator.next()); + System.out.print(iterator.next()+" "); + } + +} + +/** +* +* Method: getElements(LinkedList linklist) +* +*/ +public void testGetElements() throws Exception { +//TODO: Test goes here... + LinkedList list = new LinkedList(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + //linklist.remove(3,6); + LinkedList listb = new LinkedList(); + listb.add(1); + listb.add(3); + listb.add(4); + listb.add(6); + int[] res; + res=list.getElements(listb); + //System.out.println(linklist.size()); + for (int i = 0; i +* @since
 12, 2017
+* @version 1.0 +*/ +public class LinkedListbakTest { + +@Before +public void before() throws Exception { +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: add(Object o) +* +*/ +@Test +public void testAddO() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: add(int index, Object o) +* +*/ +@Test +public void testAddForIndexO() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: get(int index) +* +*/ +@Test +public void testGet() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: remove(int index) +* +*/ +@Test +public void testRemove() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: size() +* +*/ +@Test +public void testSize() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: addFirst(Object o) +* +*/ +@Test +public void testAddFirst() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: addLast(Object o) +* +*/ +@Test +public void testAddLast() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: removeFirst() +* +*/ +@Test +public void testRemoveFirst() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: removeLast() +* +*/ +@Test +public void testRemoveLast() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: iterator() +* +*/ +@Test +public void testIterator() throws Exception { +//TODO: Test goes here... + LinkedListbak list = new LinkedListbak(); + list.add("3"); + list.add("8"); + list.add("10"); + //linklist.reverse(); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + // String s= new String(iterator.next()); + System.out.print(iterator.next()); + } +} + + +} diff --git a/group15/1502_1617273078/data-structure/test.jpg b/group15/1502_1617273078/data-structure/test.jpg new file mode 100644 index 0000000000..95f6fe5b31 Binary files /dev/null and b/group15/1502_1617273078/data-structure/test.jpg differ diff --git a/group15/1502_1617273078/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/group15/1502_1617273078/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..2e7d56be17 --- /dev/null +++ b/group15/1502_1617273078/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,86 @@ +package com.coderising.jvm.loader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + File file = new File(className); + + List list = new ArrayList(); + FileInputStream s = null; + try { + s = new FileInputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + byte[] buffer = new byte[1024]; + int len = 0; + try { + while ((len = s.read(buffer)) != 1) { + if (len < 0) { + break; + }else { + for (int i = 0; i - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group15/1502_1617273078/src/com/coding/basic/LinkedList.java b/group15/1502_1617273078/src/com/coding/basic/LinkedList.java deleted file mode 100644 index f10225fb5e..0000000000 --- a/group15/1502_1617273078/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,163 +0,0 @@ -package com.coding.basic; - -public class LinkedList implements List { - private int thesize; - private Node head; - public void add(Object o){ - if (head == null) { - head = new Node(); - head.data = o; - head.next = null; - thesize++; - } else { - addLast(o); - //thesize++; - } - } - public void add(int index , Object o){ - if (index > thesize) { - throw new IndexOutOfBoundsException(); - } else if (index == thesize) { - addLast(o); - //thesize++; - } else if(index= thesize) { - throw new IndexOutOfBoundsException(); - } else{ - Node x = head; - int i=0; - do { - x = x.next; - i++; - } while (i == index); - return x.data; - } - } - public Object remove(int index){ - Node x = head; - for (int i = 1; i 0){ + for(int i=0;i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(this.head.next==null) + return; + int[] ints=new int[size]; + Node n=head; + for(int i=size-1;i>-1;i--) { + ints[i] = (int) n.data; + n=n.next; + } + LinkedList temp=new LinkedList(ints); + head=temp.head; + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 [4 2 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 [5 2 + + */ + public void removeFirstHalf(){ + int middle=size/2; + head=getNode(middle); + size=size-middle; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length) throws Exception { + size=size-length; + if(i+length>size) { + throw new Exception("不够长"); + } + Node start; + if(i==0){ + start=head; + }else { + start=getNode(i-1); + } + + Node n=start; + while (length>-1){ + n=n.next; + length--; + } + start.next=n; + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list) throws CloneNotSupportedException { + int length=list.size(); + if (length<1) + return null; + int[] result=new int[length]; + LinkedList newList= this; + + Node indexNode= list.head;//第一个坐标节点 + int index=(Integer) indexNode.data; + Node newFirstNode=newList.getNode(index);//第一个目标节点 + result[0]= (int) newFirstNode.data;//数放到结果数组 + if(length==1) + return result; + + int i=1; + while (i= min) {//第一个进去范围的 + startNode = prevNode; + break; + } + prevNode=n; + n = n.next; + } + int count=0; + while (n!=null) { + data=(int)n.data; + if (data >= max) {//第一个出范围的 + endNode = n; + break; + } + count++;//没出范围就继续计数 + n = n.next;//下一个 + } + startNode.next=endNode; + size=size-count; + if ((int)head.data>=min){ + size--; + head=head.next; + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList list3=new LinkedList(); + Node aNode=this.head; + Node bNode=list.head; + while (aNode!=null&&bNode!=null){ + if((int)aNode.data>(int)bNode.data){ + bNode=bNode.next; + }else if((int)bNode.data>(int)aNode.data){ + aNode=aNode.next; + }else { + list3.add((int)bNode.data); + bNode=bNode.next; + aNode=aNode.next; + } + } + + + return list3; + } +} diff --git a/group15/1503_1311822904/downland&LinkedList/LinkedListTest.java b/group15/1503_1311822904/downland&LinkedList/LinkedListTest.java new file mode 100644 index 0000000000..057351008a --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/LinkedListTest.java @@ -0,0 +1,265 @@ +package test.com.coding.basic; + + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class LinkedListTest { + int[] a={0,101,202,303,404,505,606}; +LinkedList linkedList ; +@Before +public void before() throws Exception { + linkedList =new LinkedList(a); +} + +@After +public void after() throws Exception { + +} + +/** +* +* Method: add(Object o) +* +*/ +@Test +public void testAddO() throws Exception { + linkedList.add(5); + int[] b={0,101,202,303,404,505,606,5}; + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: add(int index, Object o) +* +*/ +@Test +public void testAddForIndexO() throws Exception { + linkedList.add(2,5); + int[] b={0,101,5,202,303,404,505,606}; + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: get(int index) +* +*/ +@Test +public void testGet() throws Exception { + Assert.assertEquals(505,linkedList.get(5)); +} + +/** +* +* Method: remove(int index) +* +*/ +@Test +public void testRemoveIndex() throws Exception { + int[] b={0,101,202,303,404,606}; + Assert.assertEquals(505,linkedList.remove(5)); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: size() +* +*/ +@Test +public void testSize() throws Exception { + Assert.assertEquals(7,linkedList.size()); +} + +/** +* +* Method: addFirst(Object o) +* +*/ +@Test +public void testAddFirst() throws Exception { + int[] b={-99,0,101,202,303,404,505,606}; + linkedList.addFirst(-99); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: addLast(Object o) +* +*/ +@Test +public void testAddLast() throws Exception { + int[] b={0,101,202,303,404,505,606,-99}; + linkedList.addLast(-99); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* + +*/ +@Test +public void testRemoveFirst() throws Exception { + int[] b={101,202,303,404,505,606,}; + linkedList.removeFirst(); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: removeLast() +* +*/ +@Test +public void testRemoveLast() throws Exception { + int[] b={0,101,202,303,404,505}; + linkedList.removeLast(); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: iterator() +* +*/ +@Test +public void testIterator() throws Exception { + LinkedList temp=new LinkedList(); + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + temp.add(iterator.next()); + } + Assert.assertEquals(temp.toString(),linkedList.toString()); +} + +/** +* +* Method: reverse() +* +*/ +@Test +public void testReverse() throws Exception { + int[] b={9,7,3}; + int[] a={3,7,9}; + linkedList=new LinkedList(a); + linkedList.reverse(); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 [4 2 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 [5 2 + + + */ +@Test +public void testRemoveFirstHalf() throws Exception { + + + int[] b={303,404,505,606}; + + linkedList.removeFirstHalf(); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: remove(int i, int length) +* +*/ +@Test +public void testRemoveForILength() throws Exception { + int[] b={0,101,404,505,606}; + linkedList.remove(2,2); + Assert.assertEquals((new LinkedList(b)).toString(),linkedList.toString()); +} + +/** +* +* Method: getElements(LinkedList list) + * 11->101->202->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] +* +*/ +@Test +public void testGetElements() throws Exception { + int[] b={1,3,4,6}; + int[] c = linkedList.getElements(new LinkedList(b)); + int[] d={101,303,404,606}; + Assert.assertEquals(new LinkedList(d).toString(),new LinkedList(c).toString()); +} + +/** +* +* Method: subtract(LinkedList list) +* +*/ +@Test +public void testSubtract() throws Exception { + int[] b={1,303,606}; + LinkedList list2=new LinkedList(b); + linkedList.subtract(list2); + + int[] result={0,101,202,404,505}; + Assert.assertEquals(new LinkedList(result).toString(),linkedList.toString()); +} + +/** +* +* Method: removeDuplicateValues() +* +*/ +@Test +public void testRemoveDuplicateValues() throws Exception { + int[] a={1,1,1,1,2,2,2,3,3,4,5,6,7,7,7,8,9,11,11,11}; + int[] b={1,2,3,4,5,6,7,8,9,11}; + LinkedList ah= new LinkedList(a); + ah.removeDuplicateValues(); + Assert.assertEquals(new LinkedList(b).toString(),ah.toString()); +} + +/** +* +* Method: removeRange(int min, int max) +* +*/ +@Test +public void testRemoveRange() throws Exception { + int[] a={0,101,202,303,404,505,606}; + int[] b={0,101, 404,505,606}; + LinkedList bl=new LinkedList(b); + linkedList.removeRange(200,400); + Assert.assertEquals(bl.toString(),linkedList.toString()); +} + +/** +* +* Method: intersection(LinkedList list) +* +*/ +@Test +public void testIntersection() throws Exception { + int[] a={0,101,202,303,404 }; + int[] b={0,101, 404,505,606}; + int[] c={0,101, 404}; + LinkedList bl=new LinkedList(b); + LinkedList al=new LinkedList(a); + LinkedList cl=new LinkedList(c); + + Assert.assertEquals(cl.toString(),al.intersection(bl).toString()); +} + + +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/DownloadThread.java b/group15/1503_1311822904/downland&LinkedList/src/DownloadThread.java new file mode 100644 index 0000000000..18b2dc23cf --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/DownloadThread.java @@ -0,0 +1,41 @@ +import api.Connection; + +import java.io.IOException; +import java.util.*; +import java.util.concurrent.CountDownLatch; + +public class DownloadThread extends Thread{ + private CountDownLatch threadsSignal; + private Connection conn; + private int startPos; + private int endPos; + static Map partMap = (Map) Collections.synchronizedMap(new TreeMap( + new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1.compareTo(o2); + } + })); + + + + public DownloadThread(Connection conn, int startPos, int endPos, CountDownLatch threadSignal){ + this.threadsSignal = threadSignal; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ //TODO 具体下载的线程 + + try { + partMap.put(startPos,conn.read(startPos,endPos)); + } catch (IOException e) { + e.printStackTrace(); + }finally { + if(conn != null){ + conn.close(); + } + threadsSignal.countDown();//线程结束时计数器减1 + } + } +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/FileDownloader.java b/group15/1503_1311822904/downland&LinkedList/src/FileDownloader.java new file mode 100644 index 0000000000..3137a5ce4d --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/FileDownloader.java @@ -0,0 +1,91 @@ +import api.Connection; +import api.ConnectionManager; +import api.DownloadListener; + +import java.io.FileOutputStream; +import java.util.concurrent.CountDownLatch; + + +public class FileDownloader { + + String url; + + DownloadListener downloadListener; + + ConnectionManager connectionManager; + + + public FileDownloader(String _url) { + this.url = _url; + + } + int threadNum=3; + public void execute(){//TODO 主体 + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection connection = null; + try { + connection = connectionManager.open(this.url); + int totalLength = connection.getContentLength(); + + //计算出每一块的大小 + int perLength =totalLength /threadNum +1; + + CountDownLatch threadSignal = new CountDownLatch(threadNum);//初始化countDown + for (int i = 0; i < threadNum; i++) {//开threadNum个线程 + int length = perLength; + //如果是最后一块, 则使用总数来减去前面块的总和 + if (i == (threadNum - 1)) { + length = totalLength- i * perLength; + } + connection = connectionManager.open(this.url); + new DownloadThread(connection,i * perLength,i* perLength+length-1,threadSignal).start(); + + } + FileOutputStream fos = new FileOutputStream("D:\\new.jpg"); + + threadSignal.await();//等待所有子线程执行完 + for(Integer i:DownloadThread.partMap.keySet()){ + fos.write(DownloadThread.partMap.get(i)); + } + fos.close(); + getDownloadListener().notifyFinished(); + } catch (Exception e) { + e.printStackTrace(); + + } + + + + + } + + public void setDownloadListener(DownloadListener downloadListener) { + this.downloadListener = downloadListener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.connectionManager = ucm; + } + + public DownloadListener getDownloadListener(){ + return this.downloadListener; + } + +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/FileDownloaderTest.java b/group15/1503_1311822904/downland&LinkedList/src/FileDownloaderTest.java new file mode 100644 index 0000000000..94dd275394 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/FileDownloaderTest.java @@ -0,0 +1,53 @@ +import api.DownloadListener; +import impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://h.hiphotos.baidu.com/baike/s%3D220/sign=d37711ea2b2eb938e86d7df0e56385fe/32fa828ba61ea8d37a8f660f930a304e251f580f.jpg"; + + FileDownloader fileDownloader = new FileDownloader(url); + + fileDownloader.setConnectionManager(new ConnectionManagerImpl()); + + fileDownloader.setDownloadListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + fileDownloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/api/Connection.java b/group15/1503_1311822904/downland&LinkedList/src/api/Connection.java new file mode 100644 index 0000000000..dd940f0351 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/api/Connection.java @@ -0,0 +1,23 @@ +package api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return 字节数组 + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/api/ConnectionException.java b/group15/1503_1311822904/downland&LinkedList/src/api/ConnectionException.java new file mode 100644 index 0000000000..60a2043e44 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/api/ConnectionException.java @@ -0,0 +1,5 @@ +package api; + +public class ConnectionException extends Exception { + +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/api/ConnectionManager.java b/group15/1503_1311822904/downland&LinkedList/src/api/ConnectionManager.java new file mode 100644 index 0000000000..29fdb371e1 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/api/DownloadListener.java b/group15/1503_1311822904/downland&LinkedList/src/api/DownloadListener.java new file mode 100644 index 0000000000..b8ab21d462 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/api/DownloadListener.java @@ -0,0 +1,5 @@ +package api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/impl/ConnectionImpl.java b/group15/1503_1311822904/downland&LinkedList/src/impl/ConnectionImpl.java new file mode 100644 index 0000000000..66a1d12f62 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/impl/ConnectionImpl.java @@ -0,0 +1,44 @@ +package impl; + +import api.Connection; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +public class ConnectionImpl implements Connection { + private HttpURLConnection urlConnection; + private InputStream inputStream; + public ConnectionImpl(HttpURLConnection urlConnection) { + this.urlConnection=urlConnection; + + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException {//TODO 实际下载 + urlConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + urlConnection.connect(); + InputStream inputStream = urlConnection.getInputStream(); + byte[] buffer = new byte[endPos-startPos+1]; + inputStream.read(buffer); + return buffer; + } + + @Override + public int getContentLength() { + try { + urlConnection.connect(); + } catch (IOException e) { + e.printStackTrace(); + } + return urlConnection.getContentLength(); + + } + + @Override + public void close() { + + + } + +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/impl/ConnectionManagerImpl.java b/group15/1503_1311822904/downland&LinkedList/src/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..f3649be2be --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/impl/ConnectionManagerImpl.java @@ -0,0 +1,27 @@ +package impl; + +import api.Connection; +import api.ConnectionException; +import api.ConnectionManager; + +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + private URL url; + @Override + public Connection open(String urlStr) throws ConnectionException { + Connection connection; + try { + url= new URL(urlStr); + HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); + connection=new ConnectionImpl(urlConnection); + } catch (Exception e) { + e.printStackTrace(); + throw new ConnectionException(); + } + + return connection; + } + +} diff --git a/group15/1503_1311822904/downland&LinkedList/src/impl/URLConnectionDownloader.java b/group15/1503_1311822904/downland&LinkedList/src/impl/URLConnectionDownloader.java new file mode 100644 index 0000000000..74c75bd4a1 --- /dev/null +++ b/group15/1503_1311822904/downland&LinkedList/src/impl/URLConnectionDownloader.java @@ -0,0 +1,47 @@ +package impl; + +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; + +/** + * 使用URLConnection下载文件或图片并保存到本地。 + * + * @author 老紫竹(laozizhu.com) + */ +public class URLConnectionDownloader { + public static void main(String[] args) throws Exception { + download("http://www.laozizhu.com/images/logo.gif", "laozizhu.com.gif"); + } + + /** + * 下载文件到本地 + * + * @param urlString 被下载的文件地址 + * @param filename 本地文件名 + * @throws Exception 各种异常 + */ + public static void download(String urlString, String filename) throws Exception { + // 构造URL + URL url = new URL(urlString); + // 打开连接 + URLConnection con = url.openConnection(); + // 输入流 + InputStream is = con.getInputStream(); + // 1K的数据缓冲 + byte[] bs = new byte[1024]; + // 读取到的数据长度 + int len; + // 输出的文件流 + OutputStream os = new FileOutputStream(filename); + // 开始读取 + while ((len = is.read(bs)) != -1) { + os.write(bs, 0, len); + } + // 完毕,关闭所有链接 + os.close(); + is.close(); + } +} diff --git a/group15/1503_1311822904/myCollection/src/LinkedList.java b/group15/1503_1311822904/myCollection/src/LinkedList.java index 69fad47ace..d0ac0f732c 100644 --- a/group15/1503_1311822904/myCollection/src/LinkedList.java +++ b/group15/1503_1311822904/myCollection/src/LinkedList.java @@ -149,34 +149,111 @@ public String toString(){ return s; } - public static void main(String[] arg){ - LinkedList a=new LinkedList(); - // a.removeFirst(); - a.addFirst("first"); - a.addLast("ll"); - a.add(0); - a.add(1); - a.add("2"); - a.add("3"); - a.add("4"); - a.add("5"); - a.add("six"); - a.add("七"); - a.add(8); - System.out.println(a); - Iterator iterator=a.iterator(); - while (iterator.hasNext()) - System.out.println(iterator.next()); - /*System.out.println(a.size); - System.out.println(a); - System.out.println(a.remove(3)); - System.out.println(a.remove(3)); - a.removeFirst(); - a.removeLast(); - System.out.println(a);*/ + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + PLinkedList p= (PLinkedList) this.clone(); + this.clear(); + for(int i=p.size()-1;i>-1;i--){ + this.add(p.get(i)); + } + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 [4 2 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 [5 2 + + */ + public void removeFirstHalf(){ + PLinkedList p= (PLinkedList) this.clone(); + this.clear(); + for(int i=p.size()/2;isize) { + throw new Exception("不够长"); + } + for(int j=1;j<=length;j++){ + this.remove(i); + } + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result=new int[list.size()]; + for(int i=0 ;i p= (PLinkedList) this.clone(); + for(T t:this){ + p.remove(t); + if(p.contains(t)){ + this.remove(t); + } + } - //System.out.println(a.get(3)); - //System.out.println(a.get(99)); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int middle=this.size(); + int i=middle; + int j=middle+1; + + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; } } diff --git a/group15/1507_977996067/src/task3/download/DownloadThread.java b/group15/1507_977996067/src/task3/download/DownloadThread.java new file mode 100644 index 0000000000..291cb6f289 --- /dev/null +++ b/group15/1507_977996067/src/task3/download/DownloadThread.java @@ -0,0 +1,48 @@ +package task3.download; + +import task3.download.api.Connection; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread { + + Connection conn; + int startPos; + int endPos; + File file; + + public DownloadThread(Connection conn, int startPos, int endPos) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + public DownloadThread(Connection conn, int startPos, int endPos, File file) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.file = file; + } + + public void run() { + RandomAccessFile randomAccessFile = null; + try { + byte[] read = conn.read(startPos, endPos); + randomAccessFile = new RandomAccessFile(file, "rw"); + randomAccessFile.skipBytes(startPos); + randomAccessFile.write(read, 0, endPos - startPos + 1); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (randomAccessFile != null) + try { + randomAccessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/group15/1507_977996067/src/task3/download/FileDownloader.java b/group15/1507_977996067/src/task3/download/FileDownloader.java new file mode 100644 index 0000000000..55c8b43dca --- /dev/null +++ b/group15/1507_977996067/src/task3/download/FileDownloader.java @@ -0,0 +1,80 @@ +package task3.download; + +import task3.download.api.Connection; +import task3.download.api.ConnectionException; +import task3.download.api.ConnectionManager; +import task3.download.api.DownloadListener; + +import java.io.File; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + + File targetFile = new File("f:/" + url.substring(url.lastIndexOf("/"))); + try { + if (cm == null) + throw new RuntimeException("connection manager not ready"); + conn = cm.open(this.url); + if (conn == null) + throw new RuntimeException("connect time out"); + int length = conn.getContentLength(); + //多线程个数 + int count = length / 102400 + 1; + for (int i = 0; i < count; i++) { + int startPos = i * 102400; + int endPos = (i == (count - 1)) ? (length - 1) : (startPos + 102399); + System.out.println(endPos); + Thread th = new DownloadThread(conn, startPos, endPos, targetFile); + th.start(); + th.join(); + } + listener.notifyFinished(); + } catch (InterruptedException | ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group15/1507_977996067/src/task3/download/FileDownloaderTest.java b/group15/1507_977996067/src/task3/download/FileDownloaderTest.java new file mode 100644 index 0000000000..536c225f63 --- /dev/null +++ b/group15/1507_977996067/src/task3/download/FileDownloaderTest.java @@ -0,0 +1,55 @@ +package task3.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import task3.download.api.ConnectionManager; +import task3.download.api.DownloadListener; +import task3.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + ConnectionManager cm = new ConnectionManagerImpl(); + + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + downloader.execute(); + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + } + +} diff --git a/group15/1507_977996067/src/task3/download/api/Connection.java b/group15/1507_977996067/src/task3/download/api/Connection.java new file mode 100644 index 0000000000..e9ce626831 --- /dev/null +++ b/group15/1507_977996067/src/task3/download/api/Connection.java @@ -0,0 +1,23 @@ +package task3.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group15/1507_977996067/src/task3/download/api/ConnectionException.java b/group15/1507_977996067/src/task3/download/api/ConnectionException.java new file mode 100644 index 0000000000..a9c2c5ef83 --- /dev/null +++ b/group15/1507_977996067/src/task3/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package task3.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group15/1507_977996067/src/task3/download/api/ConnectionManager.java b/group15/1507_977996067/src/task3/download/api/ConnectionManager.java new file mode 100644 index 0000000000..2275f4d12a --- /dev/null +++ b/group15/1507_977996067/src/task3/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package task3.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group15/1507_977996067/src/task3/download/api/DownloadListener.java b/group15/1507_977996067/src/task3/download/api/DownloadListener.java new file mode 100644 index 0000000000..6d0cb69e7c --- /dev/null +++ b/group15/1507_977996067/src/task3/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package task3.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1507_977996067/src/task3/download/impl/ConnectionImpl.java b/group15/1507_977996067/src/task3/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..d2388394a7 --- /dev/null +++ b/group15/1507_977996067/src/task3/download/impl/ConnectionImpl.java @@ -0,0 +1,44 @@ +package task3.download.impl; + +import task3.download.api.Connection; + +import java.io.IOException; +import java.io.InputStream; + +public class ConnectionImpl implements Connection { + + private InputStream inputStream; + + public ConnectionImpl(InputStream inputStream) { + this.inputStream = inputStream; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + byte[] buffer = new byte[endPos - startPos + 1]; + inputStream.read(buffer, 0, buffer.length); + return buffer; + } + + @Override + public int getContentLength() { + int length = 0; + try { + length = inputStream.available(); + System.out.println("接收到的数据长度为 " + length); + } catch (IOException e) { + e.printStackTrace(); + } + return length; + } + + @Override + public void close() { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/group15/1507_977996067/src/task3/download/impl/ConnectionManagerImpl.java b/group15/1507_977996067/src/task3/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..3db283bc27 --- /dev/null +++ b/group15/1507_977996067/src/task3/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,23 @@ +package task3.download.impl; + + +import task3.download.api.Connection; +import task3.download.api.ConnectionException; +import task3.download.api.ConnectionManager; + +import java.io.FileInputStream; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + ConnectionImpl connection = null; + try { + connection = new ConnectionImpl(new FileInputStream("f://pictures/b3.jpg")); + } catch (Exception e) { + e.printStackTrace(); + } + return connection; + } + +} diff --git a/group15/1507_977996067/src/task3/linkedlist/MyLinkedList.java b/group15/1507_977996067/src/task3/linkedlist/MyLinkedList.java new file mode 100644 index 0000000000..14092d3ae9 --- /dev/null +++ b/group15/1507_977996067/src/task3/linkedlist/MyLinkedList.java @@ -0,0 +1,286 @@ +package task3.linkedlist; + +import java.util.Iterator; + +public class MyLinkedList> { + + //存放的元素数量 + private int size; + + private Node head; + + public MyLinkedList() { + head = new Node<>(null, null); + } + + public void add(T o) { + add(size, o); + } + + public void add(int index, T o) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index " + index + " 不合法"); + Node targetNode = new Node<>(null, o); + Node targetPrevNode = getPrevNode(index); + targetNode.next = targetPrevNode.next; + targetPrevNode.next = targetNode; + size++; + } + + public T get(int index) { + checkIndexRange(index); + return getPrevNode(index).next.data; + } + + public Node getNode(int index) { + checkIndexRange(index); + return getPrevNode(index).next; + } + + + public T remove(int index) { + checkIndexRange(index); + Node prevNode = getPrevNode(index); + Node nodeToRemove = prevNode.next; + prevNode.next = nodeToRemove.next; + size--; + return nodeToRemove.data; + } + + public int size() { + return size; + } + + public void addFirst(T o) { + add(0, o); + + } + + public void addLast(T o) { + add(size, o); + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size - 1); + } + + + public Iterator iterator() { + return new MyLinkedItr(); + } + + /** + * 找到位置为index的前一个node + * + * @param index 索引值 + */ + + private Node getPrevNode(int index) { + Node targetPrevNode = head; + for (int i = 0; i < index; i++) { + targetPrevNode = targetPrevNode.next; + } + return targetPrevNode; + } + + /** + * 检查索引是否越界 + * + * @param index 索引值 + */ + private void checkIndexRange(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + } + + private static class Node { + private Node next; + private T data; + + private Node(Node next, T data) { + this.next = next; + this.data = data; + } + } + + private class MyLinkedItr implements Iterator { + + private Node currentNode = head; + + @Override + public boolean hasNext() { + return currentNode.next != null; + } + + @Override + public T next() { + Node nextNode = currentNode.next; + T data = nextNode.data; + currentNode = nextNode; + return data; + } + + @Override + public void remove() { + currentNode.next = currentNode.next.next; + } + } + + @Override + public String toString() { + if (size == 0) + return "[]"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < size; i++) { + sb.append(get(i)).append(","); + } + return sb.substring(0, sb.length() - 1); + } + + /** + * ================================== + * 3.12作业 + * ================================== + *

+ * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (size == 0) + return; + int length = size; + for (int i = length - 1; i >= 0; i--) { + add(get(i)); + } + remove(0, length); // :( + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + remove(0, size / 2); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + */ + public void remove(int i, int length) { + if (length == 0) + return; + if (i + length > size) + throw new IndexOutOfBoundsException("长度不够"); + Node startNode = getPrevNode(i); + startNode.next = (i + length == size) ? null : getNode(i + length); + size -= length; + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + */ + @SuppressWarnings("unchecked") + public T[] getElements(MyLinkedList list) { + int size = list.size(); + Comparable[] result = new Comparable[size]; + int count = 0; + for (int i = 0; i < size; i++) { + result[count++] = get(list.get(i)); + } + return (T[]) result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + */ + + public void subtract(MyLinkedList list) { + int length = list.size(); + for (int i = 0; i < length; i++) { + for (int j = 0; j < size; j++) { + if (get(j).equals(list.get(i))) { + remove(j); + break; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Iterator iterator = iterator(); + int pos = 0; + while (iterator.hasNext()) { + //当前索引的值等于下一个索引的值时,就把当前索引删掉 + if (get(pos).equals(get(pos + 1))) { + remove(pos); + } + pos++; + iterator.next(); + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + */ + public void removeRange(T min, T max) { + if (min.compareTo(max) >= 0) + throw new RuntimeException("Are you kidding me ?"); + int minIndex = 0; + int maxIndex = size; + for (int i = 0; i < size; i++) { + if (get(i).compareTo(min) > 0) { + minIndex = i; + break; + } + } + for (int i = size - 1; i >= 0; i--) { + if (get(i).compareTo(max) < 0) { + maxIndex = i; + break; + } + } + remove(minIndex, maxIndex - minIndex + 1); + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + */ + public MyLinkedList intersection(MyLinkedList list) { + MyLinkedList resultList = new MyLinkedList<>(); + int firstLength = size; + int secondLength = list.size(); + int firstPos = 0; + int secondPos = 0; + while (firstPos < firstLength && secondPos < secondLength) { + T firstItem = get(firstPos); + T secondItem = list.get(secondPos); + int compareResult = firstItem.compareTo(secondItem); + if (compareResult == 0) { + resultList.add(firstItem); + firstPos++; + secondPos++; + } else if (compareResult < 0) + firstPos++; + else + secondPos++; + } + return resultList; + } +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task3/linkedlist/MyLinkedListTest.java b/group15/1507_977996067/src/task3/linkedlist/MyLinkedListTest.java new file mode 100644 index 0000000000..845960ff97 --- /dev/null +++ b/group15/1507_977996067/src/task3/linkedlist/MyLinkedListTest.java @@ -0,0 +1,135 @@ +package task3.linkedlist; + +import org.junit.Test; + +import java.util.Arrays; + +public class MyLinkedListTest { + + @Test + public void testReverse() { + MyLinkedList list = new MyLinkedList<>(); + list.add(3); + list.add(7); + list.add(10); + System.out.println(list); + list.reverse(); + System.out.println(list); + } + + @Test + public void testRemoveFirstHalf() { + MyLinkedList list = new MyLinkedList<>(); + list.add(2); + list.add(5); + list.add(7); + list.add(8); + list.add(10); + System.out.println(list); + list.removeFirstHalf(); + System.out.println(list); + } + + @Test + public void testRemove() { + MyLinkedList list = new MyLinkedList<>(); + list.add(3); + list.add(7); + list.add(10); + System.out.println(list); + list.remove(2, 1); + System.out.println(list); + } + + @Test + public void testGetElements() { + MyLinkedList list = new MyLinkedList<>(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + MyLinkedList listB = new MyLinkedList<>(); + listB.add(1); + listB.add(3); + listB.add(4); + listB.add(6); + Arrays.stream(list.getElements(listB)).forEach(System.out::println); + } + + @Test + public void testSubtract() { + MyLinkedList list = new MyLinkedList<>(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + MyLinkedList listB = new MyLinkedList<>(); + listB.add(101); + listB.add(501); + listB.add(201); + listB.add(601); + list.subtract(listB); + System.out.println(list); + } + + @Test + public void testRemoveDuplicateValues() { + MyLinkedList list = new MyLinkedList<>(); + list.add(11); + list.add(11); + list.add(201); + list.add(301); + list.add(401); + list.add(401); + list.add(601); + list.add(601); + list.removeDuplicateValues(); + System.out.println(list); + } + + @Test + public void testRemoveRange() { + MyLinkedList list = new MyLinkedList<>(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + list.removeRange(10,101); + System.out.println(list); + } + + @Test + public void testIntersection() { + MyLinkedList list = new MyLinkedList<>(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + MyLinkedList listB = new MyLinkedList<>(); + listB.add(101); + listB.add(201); + listB.add(501); + listB.add(601); + listB.add(701); + System.out.println(list.intersection(listB)); + } +} diff --git a/group15/1507_977996067/src/task4/loader/ClassFileLoader.java b/group15/1507_977996067/src/task4/loader/ClassFileLoader.java new file mode 100644 index 0000000000..5b93ed09fe --- /dev/null +++ b/group15/1507_977996067/src/task4/loader/ClassFileLoader.java @@ -0,0 +1,49 @@ +package task4.loader; + +import java.io.*; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList<>(); + + public byte[] readBinaryCode(String className) { + boolean validClassName = className.endsWith(".class"); + className = className.replaceAll("\\.", "/"); + if (!validClassName) { + className += ".class"; + } else { + className = className.replace("/class", ".class"); + } + for (String clzPath : clzPaths) { + if (!clzPath.endsWith("/")) + clzPath += "/"; + try { + FileInputStream stream = new FileInputStream(clzPath + className); + byte[] buffer = new byte[stream.available()]; + while (stream.read(buffer) != -1) { + } + return buffer; + } catch (IOException e) { + continue; + } + } + return null; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + StringBuilder sb = new StringBuilder(); + for (String clzPath : clzPaths) { + sb.append(clzPath).append(";"); + } + return sb.substring(0, sb.length() - 1); + } + +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task4/loader/ClassFileloaderTest.java b/group15/1507_977996067/src/task4/loader/ClassFileloaderTest.java new file mode 100644 index 0000000000..4f6e8ede0f --- /dev/null +++ b/group15/1507_977996067/src/task4/loader/ClassFileloaderTest.java @@ -0,0 +1,79 @@ +package task4.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + + + static String path1 = "E:/Idea/coding2017/group15/1507_977996067/out/"; + static String path2 = "C:/temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "task4.loader.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1034, byteCodes.length); + + } + + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "task4.loader.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group15/1507_977996067/src/task4/loader/EmployeeV1.java b/group15/1507_977996067/src/task4/loader/EmployeeV1.java new file mode 100644 index 0000000000..00a6683dda --- /dev/null +++ b/group15/1507_977996067/src/task4/loader/EmployeeV1.java @@ -0,0 +1,29 @@ +package task4.loader; + +public class EmployeeV1 { + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} diff --git a/group15/1507_977996067/src/task4/lru/LRUPageFrame.java b/group15/1507_977996067/src/task4/lru/LRUPageFrame.java new file mode 100644 index 0000000000..9dfa1a66a0 --- /dev/null +++ b/group15/1507_977996067/src/task4/lru/LRUPageFrame.java @@ -0,0 +1,122 @@ +package task4.lru; + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + + public Node(Node prev, Node next, int pageNum) { + this.prev = prev; + this.next = next; + this.pageNum = pageNum; + } + } + + private int capacity; + private int size = 0; + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + */ + public void access(int pageNum) { + if (size == 0) { + last = first = new Node(first, last, pageNum); +// last = new Node(first, null, pageNum); + size++; + } else if (size > 0 && size < capacity) { + Node _node = get(pageNum); + if (_node == null) { + Node newNode = new Node(null, first, pageNum); + first.prev = newNode; + first = newNode; + clear(); + size++; + } else { + exchange(_node); + } + } else { + Node _node = get(pageNum); + if (_node == null) { + last = last.prev; + Node newNode = new Node(null, first, pageNum); + first.prev = newNode; + first = newNode; + clear(); + } else exchange(_node); + } + } + + private void exchange(Node node) { + Node nextNode = node.next; + Node prevNode = node.prev; + if (prevNode == null) + return;//头部的话什么都不用做 + if (nextNode != null) { + nextNode.prev = prevNode; + prevNode.next = nextNode; + last = getLast(nextNode); + } else { + last = prevNode; + } + first.prev = node; + node.next = first; + first = node; + clear(); + } + + private void clear() { + last.next = null; + first.prev = null; + } + + private Node getLast(Node startNode) { + Node currentNode = startNode; + while (currentNode.next != null) { + currentNode = currentNode.next; + } + return currentNode; + } + + private Node get(int pageNum) { + Node currentNode = last; + for (int i = 0; i < size; i++) { + if (pageNum == currentNode.pageNum) { + return currentNode; + } + currentNode = currentNode.prev; + } + return null; + } + + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task4/lru/LRUPageFrameTest.java b/group15/1507_977996067/src/task4/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..e4567f92e0 --- /dev/null +++ b/group15/1507_977996067/src/task4/lru/LRUPageFrameTest.java @@ -0,0 +1,29 @@ +package task4.lru; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group15/1510_739253131/README.md b/group15/1510_739253131/README.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group15/1510_739253131/demo1.jpg b/group15/1510_739253131/demo1.jpg new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java b/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java index 3137c7568b..d45544c837 100644 --- a/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java +++ b/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java @@ -4,18 +4,19 @@ import java.io.Serializable; import java.util.Arrays; +import java.util.Objects; /** * 用数组实现ArrayList基本功能:add,remove,size,contains,toArray方法 - * @Version: 0.0 + * @Version: 0.1 * Created by Bruce.Jiao on 17-2-23. */ -public class ArrayListV00 implements Serializable { +public class ArrayListV00 implements Serializable { /** * 存放集合元素的数组 */ - private transient Object[] elementData; + private Object[] elementData; /** * 集合中元素的个数 */ @@ -34,9 +35,9 @@ public ArrayListV00() throws MyException { * @param initCapacity * 用户传入的集合大小,底层数组的初始化大小 */ - public ArrayListV00(int initCapacity) throws MyException{ + public ArrayListV00(int initCapacity) { if(initCapacity < 0){ - throw new MyException("集合大小不能小于0"); + //throw new MyException("集合大小不能小于0"); } elementData = new Object[initCapacity]; } @@ -48,13 +49,30 @@ public ArrayListV00(int initCapacity) throws MyException{ * 添加的元素,允许添加null * @return true:添加成功 ; false:添加失败 */ - public boolean add(Object value) { - // 添加元素之前,对数组长度进行判断,此处需要传入当前元素个数+1, + public boolean add(T value) { ensureCapacity(size + 1); elementData[size++] = value; return true; } + public void add(int index, T value) { + if(index < 0 || index > size) { + //抛出异常 + } + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index+1 , size-index); + elementData[index] = value; + size++; + } + + public T set(int index, T value) { + if(index < 0 || index > size) { + return null;//抛出异常 + } + elementData[index] = value; + return value; + } + /** * 返回指定位置的元素 数组和集合,下标从1开始 * @@ -62,13 +80,14 @@ public boolean add(Object value) { * 用户指定的位置 * @return */ - public Object get(int index) throws MyException { + @SuppressWarnings("unchecked") + public T get(int index) { // 判断是否越界,注意:此处判断依据是size,而不能是elementData.length, // 集合元素个数size小于等于elementData.length if (index >= size || index < 0) { - throw new MyException("给定数值超出集合范围"); + return null;//throw new MyException("给定数值超出集合范围"); } - return elementData[index]; + return (T) elementData[index]; } /** @@ -78,11 +97,11 @@ public Object get(int index) throws MyException { * 用户指定位置,从0开始 * @return 返回删除掉的指定位置的元素 */ - public Object remove(int index) throws MyException { + public T remove(int index) { if (index >= size || index < 0) { - throw new MyException("给定数值超出集合范围"); + return null;//throw new MyException("给定数值超出集合范围"); } - Object value = elementData[index]; + T value = (T) elementData[index]; // 数组中被删除元素后边的所有元素的个数,此处不能使用elementData.length int length = size - 1 - index; // 被删除位置后还有元素,将数组中被删除位置往后(不包含被删除位置)的所有元素往前移动一位 @@ -93,23 +112,53 @@ public Object remove(int index) throws MyException { return value; } + /** + * 删除元素 + * @Version:0.1 + * @return true:删除成功;false:删除失败 + */ + public boolean remove(T value) { + int index = indexOf(value); + if (index < 0) { + return false; + } + System.arraycopy(elementData, index+1, elementData, index, elementData.length-1-index); + elementData[size--] = null; + return true; + } + + public int indexOf(T value) { + for(int i = 0 ; i < elementData.length ; i++){ + if(Objects.equals(elementData[i], value)) { + return i; + } + } + return -1; + } + /** * 判断集合中是否包含指定的元素 - * * @param value * 用户制定的元素 * @return true:包含指定元素;false:不包含指定元素 */ public boolean contains(Object value) { - for (int i = 0; i < elementData.length; i++) { - if (value == null) { - if (elementData[i] == null) { - return true; - } - } else { - if (value.equals(elementData[i])) { - return true; - } + //v0.0版本 +// for (int i = 0; i < elementData.length; i++) { +// if (value == null) { +// if (elementData[i] == null) { +// return true; +// } +// } else { +// if (value.equals(elementData[i])) { +// return true; +// } +// } +// } + //v0.1版本,根据老师作业讲解进行修改 + for(Object o : elementData) { + if (Objects.equals(o,value)) { + return true; } } return false; @@ -117,9 +166,9 @@ public boolean contains(Object value) { /** * 得到集合对应的静态数组 - * * @return 底层数组 */ + @SuppressWarnings("unchecked") public Object[] toArray() { //elementData可能会包含null元素,不能直接返回,需返回一个包含集合所有元素的新数组 // return elementData; @@ -135,6 +184,41 @@ public int size() { return size; } + public boolean isEmpty() { + return size == 0; + } + + public void clear() { + for (int i = 0 ; i < size ; i++) { + elementData[i] = null; + } + size = 0; + } + + public IteratorV00 iterator() { + return new Iterator(); + } + + //非静态内部类,和外部类实例绑定的,可以访问实例方法和属性 + private class Iterator implements IteratorV00 { + private int position; + + Iterator(){} + + @Override + public boolean hasNext() { + return position < size; + } + + @Override + public T next() { + if(hasNext()){ + return get(position++); + } + return null; + } + } + /** * 传入的数值与数组长度进行比较,长度小于传入数值,对数组进行扩容 * @@ -147,14 +231,17 @@ public void ensureCapacity(int minCapacity) { if (minCapacity > oldCapacity) { // 此处用新的局部变量引用指向原有数组的内存地址,仅为了避免复制数组元素到新数组时候,发生原有数组内存地址被覆盖的情况 Object[] oldArray = elementData; - // 先得到现有数组长度1.5倍的值 - int newCapacity = oldCapacity + oldCapacity >> 1; - // 如果增加1.5倍后的数值仍然小于传入的数值,将传入的数值赋给新数组长度 - if (minCapacity > newCapacity) { - newCapacity = minCapacity; - } - // 将elementData引用指向一个新的扩容后的数组,并且将原有数组的元素复制到新数组中 - elementData = Arrays.copyOf(elementData, newCapacity); +//v0.0初級版本 +// int newCapacity = oldCapacity + oldCapacity >> 1; +// if (minCapacity > newCapacity) { +// newCapacity = minCapacity; +// } +// elementData = Arrays.copyOf(elementData, newCapacity); + //v0.1升級版本 + int newCapacity = Math.max(minCapacity, oldCapacity + oldCapacity >> 1); + Object[] newElementData = new Object[newCapacity]; + System.arraycopy(elementData,0,newElementData,0,oldCapacity); + elementData = newElementData; } } diff --git a/group15/1510_739253131/src/com/bruce/homework0226/BinaryTreeNode.java b/group15/1510_739253131/src/com/bruce/homework0226/BinaryTreeNode.java new file mode 100644 index 0000000000..b86aa85995 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/BinaryTreeNode.java @@ -0,0 +1,108 @@ +package com.bruce.homework0226; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){} + + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode insert(T data){ + if(this.data == null){ + this.data = data; + return this; + } + if(this.data.compareTo(data) > 0) { + if(this.left == null) { + this.left = new BinaryTreeNode(); + this.left.data = data; + return this.left; + } else { + return this.left.insert(data); + } + } else if(this.data.compareTo(data) < 0) { + if(this.right == null) { + this.right = new BinaryTreeNode(); + this.right.data = data; + return this.right; + } else { + return this.right.insert(data); + } + } else { + return this; + } + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode search(T data){ + if(data == null || this.data == null) { + return null; + } + if(this.data.compareTo(data) > 0) { + if(this.left == null) { + return null; + } else { + return this.left.search(data); + } + } else if(this.data.compareTo(data) < 0) { + if(this.right == null) { + return null; + } else { + return this.right.search(data); + } + } else { + return this; + } + } + + //TODO 未确定 + @SuppressWarnings("unchecked") + public BinaryTreeNode delete(T data){ + BinaryTreeNode treeNode = search(data); + if(treeNode == null) { + return null; + } + if(this.data.compareTo(data) > 0) { + return this.left.delete(data); + } else if(this.data.compareTo(data) < 0) { + return this.right.delete(data); + } else { + if(this.left == null) { + if(this.right == null) { + this.data = null; + } else { + this.right = this; + } + } else { + if(this.right == null) { + this.left = this; + } else { + this.left = this; + this.left.right = this.right; + } + } + } + return this; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0226/IteratorV00.java b/group15/1510_739253131/src/com/bruce/homework0226/IteratorV00.java new file mode 100644 index 0000000000..1f409bd9ad --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/IteratorV00.java @@ -0,0 +1,9 @@ +package com.bruce.homework0226; + +/** + * Created by Bruce.Jiao on 2017/3/5. + */ +public interface IteratorV00 { + public boolean hasNext(); + public T next(); +} diff --git a/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java b/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java index b0aa452f6d..2110d2036e 100644 --- a/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java +++ b/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java @@ -33,8 +33,6 @@ public void testArrayList(){ System.out.println("集合为:"+ Arrays.toString(arrayList.toArray())); System.out.println("集合底层数组长度:"+ arrayList.arrayLength()); // System.out.println("集合下标-1处的元素:"+arrayList.get(-1)); - } catch (MyException e) { - System.out.println("发生异常>>>"+e); } catch (Exception e) { e.printStackTrace(); } diff --git a/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV00.java b/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV00.java index ddfa9dc1a8..485b6cfde8 100644 --- a/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV00.java +++ b/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV00.java @@ -4,7 +4,6 @@ import java.io.Serializable; import java.util.Arrays; -import java.util.Collection; /** * 实现LinkedList的基本功能 @@ -18,19 +17,21 @@ public class LinkedListV00 implements Serializable { private transient int size = 0; /** - * 前一个节点 + * 头节点 */ - private transient Node first; + private transient Node head; /** - * 后一个节点 + * 尾节点 */ private transient Node last; /** * 空构造 */ - public LinkedListV00(){} + public LinkedListV00(){ + head = new Node(null, null, null); + } /** * 添加一个节点 @@ -99,11 +100,11 @@ public Object[] toArray(){ */ private static class Node{ E element; - Node previous; + Node prev; Node next; - Node(Node previous,E element,Node next){ + Node(Node prev,E element,Node next){ this.element = element; - this.previous = previous; + this.prev = prev; this.next = next; } } @@ -118,7 +119,7 @@ private Node node(int index){ //如果index小于size的一半,即目标节点在链表前半部分 if(index < (size >> 1)){ //从第一个节点挨个向后查找,一直到(index-1)处,将其next赋值给x - x = first; + x = head; for(int i = 0; i node(int index){ //从最后一个节点挨个向前查找,一直查找到(index+1)处,将其previous赋值给x x = last; for(int i = size-1;i>index;i--){ - x = x.previous; + x = x.prev; } } //返回x @@ -148,7 +149,7 @@ private void linkNext(E e){ //如果n为null,说明还是一个空的双向链表,将新节点newNode赋值给first //否则,将newNode赋值给n的next if(n == null){ - first = newNode; + head = newNode;//第一次添加的时候,将该元素放在头节点位置 }else{ n.next = newNode; } @@ -167,16 +168,16 @@ private E unlink(Node node){ //拿到传入节点的next节点 final Node next = node.next; //拿到传入节点的previous节点 - final Node previous = node.previous; + final Node previous = node.prev; //如果传入节点的previous=null,说明是第一个节点 if(previous == null){ //将链表第一个节点指向本节点的下一个节点next,即把原有的第一个节点解除 - first = next; + head = next; }else{ //将本节点前一个节点的next指向本节点后一个节点,即跳过了本节点 previous.next = next; //将本节点的previous节点设置为null - node.previous = null; + node.prev = null; } //如果传入节点的next=null,说明是最后一个节点 if(next == null){ @@ -184,7 +185,7 @@ private E unlink(Node node){ last = previous; }else{ //将本节点下一个节点的previous节点指向本节点的前一个节点,即跳过了本节点 - next.previous = previous; + next.prev = previous; //本节点的next节点设置为null node.next = null; } diff --git a/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV01.java b/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV01.java new file mode 100644 index 0000000000..f4defc7d3f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/LinkedListV01.java @@ -0,0 +1,139 @@ +package com.bruce.homework0226; + +import java.util.Objects; + +public class LinkedListV01 { + private int size; + private Node head; + private Node last; + + public LinkedListV01() { + //保证了初始化一个对象的时候,头节点不为空 + this.head = new Node(null); + } + + public boolean add (T element) { + //双向链表,双向都需要维护 + if(last == null){ + last = new Node<>(element); + head.next = last; + last.pre = head; + }else{ + Node oldLast = last; + last = new Node<>(element); + last.pre = oldLast; + oldLast.next = last; + } + size++; + return true; + } + + public boolean add(int index, T element) { + Node node = getNode(index); + Node newNode = new Node(element); + Node pre = node.pre; + pre.next = newNode; + newNode.pre = pre; + newNode.next = node; + size++; + return true; + } + + public boolean remove(T element){ + Node node = head; + //下一个节点不为null + while(node.next != null){ + node = node.next; + if(Objects.equals(node.element, element)){ + if(node.next != null){ + node.next.pre = node.pre; + } + node.pre.next = node.next; + size--; + return true; + } + } + //下一个节点为null,说明是尾节点 + if(node != head){ + last = node; + } + //head.next=null,说明是一个空的链表,即仅有一个空head节点 + return false; + } + + public T remove(int index){ + Node node = getNode(index); + Node pre = node.pre; + Node next = node.next; + pre.next = next; + next.pre = pre; + size--; + return node.element; + } + + public void clear(){ + for(Node x = head; x != null; ){ + Node next = x.next; + x.pre = null; + x.next = null; + x.element = null; + } + head = last = null; + size = 0; + } + + public int size(){ + return size; + } + + public boolean isEmpty(){ + return size == 0; + } + + public boolean contains(Object o){ + for(int i = 0; i < size; i++){ + if(Objects.equals(getNode(i).element, o)){ + return true; + } + } + return false; + } + + public Node getNode(int index){ + if(index < 0 || index >size){ + return null; + } + Node node = head; + for(int i = 0; i < index; i++){ + node = node.next; + } + return node; + } + + public T get(int index) { + return getNode(index).element; + } + + public int indexOf(T element){ + Node node = head; + int index = 0; + while(node.next != null){ + node = node.next; + if(Objects.equals(node.element, element)){ + return index; + } + index++; + } + return -1; + } + + private static class Node { + T element; + Node pre; + Node next; + + Node(T element) { + this.element = element; + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java b/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java index 29cfe7e9fd..900df94251 100644 --- a/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java +++ b/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java @@ -10,7 +10,7 @@ * @Version: 0.0 * Created by Bruce.Jiao on 17-2-24. */ -public class StackV00 implements Serializable{ +public class StackV00 implements Serializable{ /** * 底层存放栈元素的数组 @@ -61,7 +61,7 @@ public StackV00(int initCapacity, int capacityIncrement) throws MyException{ * @param value 添加的元素,可以为null * @return 添加成功后的元素 */ - public Object push(Object value){ + public T push(T value){ ensureCapacity(size+1); //将新增的元素放在size索引处,并且将size加1 elementData[size++] = value; @@ -72,9 +72,11 @@ public Object push(Object value){ * 从栈中获取元素,拿到当前所有元素中最后添加进来的元素 * @return 最后的元素 */ - public Object pop(){ + public T pop(){ //拿到最后的元素,在栈中将该元素删除,将size减1 - return elementData[--size]; + T data = (T) elementData[size-1]; + elementData[size--] = null; + return data; } /** diff --git a/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java index eb5487e76d..8b766870d9 100644 --- a/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java @@ -9,11 +9,17 @@ /** * Created by Bruce.Jiao on 2017/3/2. */ -public class JuintArrayUtil extends TestCase { +public class JuintArrayUtil { + + ArrayUtil au; + + @Before + public void init(){ + au = new ArrayUtil(); + } @Test public void testReverse(){ - ArrayUtil au = new ArrayUtil(); int[] demo0 = {}; int[] demo1 = {6}; int[] demo = {7, 9, 30, 3, 4, 6}; @@ -29,7 +35,6 @@ public void testReverse(){ @Test public void testRemoveZero(){ - ArrayUtil au = new ArrayUtil(); int[] one = {0}; int[] many = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; System.out.println(Arrays.toString(au.removeZero(one))); @@ -40,7 +45,6 @@ public void testRemoveZero(){ @Test public void testMerge(){ - ArrayUtil au = new ArrayUtil(); int[] arr1 = {3,4,5,6,7,8,9}; int[] arr2 = {1,3,5,6,7,9,10,12,13}; int[] arr3 = null; @@ -52,32 +56,27 @@ public void testMerge(){ @Test public void testGrow(){ - ArrayUtil au = new ArrayUtil(); int[] arr = {3,4,5,6,7,8,9}; System.out.println(Arrays.toString(au.grow(arr,5))); } @Test public void testFibonacci(){ - ArrayUtil au = new ArrayUtil(); System.out.println(Arrays.toString(au.fibonacci(15))); } @Test public void testPrimes(){ - ArrayUtil au = new ArrayUtil(); System.out.println(Arrays.toString(au.getPrimes(23))); } @Test public void testPerfectNumbers(){ - ArrayUtil au = new ArrayUtil(); - System.out.println(Arrays.toString(au.getPerfectNumbers(23))); + System.out.println(Arrays.toString(au.getPerfectNumbers(Integer.MAX_VALUE))); } @Test public void testJoin(){ - ArrayUtil au = new ArrayUtil(); int[] array = {1,6,8,8,8,8,8,8,8,8,8,}; System.out.println(au.join(array,"-")); } diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/Configuration.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/Configuration.java new file mode 100644 index 0000000000..a703c5a1a3 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/Configuration.java @@ -0,0 +1,106 @@ +package com.bruce.homework0305.demostruts; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 用来存放解析后的struts.xml数据 + */ +public class Configuration { + + private Map actionConfigMap = new HashMap<>(); + + public Configuration(String fileName){ + try { + //拿到当前类的报名,拼接出struts.xml的路径,将文件读到输入流 + String path = this.getClass().getPackage().getName(); + path = path.replace(".", "/"); + InputStream is = this.getClass().getResourceAsStream("/" + path + "/" + fileName); + //对输入流进行解析 + parseXml(is); + is.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + //用Jdom解析xml + private void parseXml(InputStream is) { + try { + SAXBuilder builder = new SAXBuilder(); + Document document = builder.build(is); + Element root = document.getRootElement(); + List actions = root.getChildren("action"); + for(Element element: actions) { + String actionName = element.getAttributeValue("name"); + String actionClz = element.getAttributeValue("class"); + ActionConfig ac = new ActionConfig(actionName, actionClz); + List results = element.getChildren("result"); + for(Element result: results) { + String resultName = result.getAttributeValue("name"); + String resultJsp = result.getValue(); + ac.addViewResult(resultName, resultJsp); + } + actionConfigMap.put(actionName, ac); + } + } catch (JDOMException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 通过action的name值拿到对应的class路径 + * @param actionName + * @return + */ + public String getClassName(String actionName) { + return actionConfigMap.get(actionName).getClassName(); + } + + /** + * 根据action的name值和result的name值,拿到对应的jsp路径 + * @param actionName + * @param resultName + * @return + */ + public String getResultView(String actionName, String resultName) { + return actionConfigMap.get(actionName).getViewName(resultName); + } + + /** + * 内部静态类,用来存放struts.xml解析出来的action信息 + */ + private static class ActionConfig{ + private String name; + private String clz; + Map results = new HashMap<>(); + + public ActionConfig(String actionName,String clzName){ + this.name = actionName; + this.clz = clzName; + } + + public void addViewResult(String resultName, String jspName){ + results.put(resultName, jspName); + } + + public String getClassName() { + return clz; + } + + public String getViewName(String resultName) { + return results.get(resultName); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ConfigurationTest.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ConfigurationTest.java new file mode 100644 index 0000000000..5de7ad7095 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ConfigurationTest.java @@ -0,0 +1,29 @@ +package com.bruce.homework0305.demostruts; + +import org.junit.Assert; +import org.junit.Test; + +public class ConfigurationTest { + + Configuration cfg = new Configuration("struts.xml"); + @Test + public void testGetClassName(){ + String login = cfg.getClassName("login"); + Assert.assertEquals("com.bruce.homework0305.demostruts.LoginAction",login); + String logout = cfg.getClassName("logout"); + Assert.assertEquals("com.bruce.homework0305.demostruts.LoginAction",login); + } + + @Test + public void getResultView(){ + String resultView = cfg.getResultView("login", "success"); + Assert.assertEquals("/jsp/homepage.jsp",resultView); + String resultView1 = cfg.getResultView("login", "fail"); + Assert.assertEquals("/jsp/showLogin.jsp",resultView1); + String resultView2 = cfg.getResultView("logout", "success"); + Assert.assertEquals("/jsp/welcome.jsp",resultView2); + String resultView3 = cfg.getResultView("logout", "error"); + Assert.assertEquals("/jsp/error.jsp",resultView3); + } + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/LoginAction.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/LoginAction.java new file mode 100644 index 0000000000..7c1c8c8962 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.bruce.homework0305.demostruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ReflectUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ReflectUtil.java new file mode 100644 index 0000000000..de3f13f9b0 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ReflectUtil.java @@ -0,0 +1,65 @@ +package com.bruce.homework0305.demostruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectUtil { + + public static List getSetterMethods(Class clz){ + return getMethods(clz, "set"); + } + + public static List getGetterMethods(Class clz){ + return getMethods(clz, "get"); + } + + public static void setParameters(Object o, Map params){ + List setterMethods = getSetterMethods(o.getClass()); + for(Method method: setterMethods) { + for(String name: params.keySet()) { + if(method.getName().equalsIgnoreCase("set"+name)) { + try { + method.invoke(o, params.get(name)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } + + public static Map getParameters(Object o) { + Map parameterMap = new HashMap<>(); + List getterMethods = getGetterMethods(o.getClass()); + for(Method method : getterMethods) { + String methodName = method.getName(); + String parameterName = methodName.replace("get","").toLowerCase(); + try { + Object value = method.invoke(o); + parameterMap.put(parameterName, value); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + return parameterMap; + } + + private static List getMethods(Class clz, String methodStart){ + List methods = new ArrayList<>(); + Method[] declaredMethods = clz.getDeclaredMethods(); + for (Method method: declaredMethods) { + if(method.getName().startsWith(methodStart)) { + methods.add(method); + } + } + return methods; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ReflectUtilTest.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ReflectUtilTest.java new file mode 100644 index 0000000000..3e4a380548 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/ReflectUtilTest.java @@ -0,0 +1,51 @@ +package com.bruce.homework0305.demostruts; + +import junit.framework.Assert; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectUtilTest { + + @Test + public void testGetSetterMethods() throws Exception{ + String name = "com.bruce.homework0305.demostruts.LoginAction"; + Class aClass = Class.forName(name); + List setterMethods = ReflectUtil.getSetterMethods(aClass); + List expectNames = new ArrayList<>(); + expectNames.add("setName"); + expectNames.add("setPassword"); + List acctualNames = new ArrayList<>(); + for(Method method: setterMethods) { + acctualNames.add(method.getName()); + } + Assert.assertTrue(acctualNames.containsAll(expectNames)); + } + + @Test + public void testSetParameters() throws Exception{ + String name = "com.bruce.homework0305.demostruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + Map params = new HashMap<>(); + params.put("name","test"); + params.put("password","1234"); + ReflectUtil.setParameters(o, params); + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test",f.get(o)); + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234",f.get(o)); + } + + public void testGetParameters() throws Exception{ + String name = "com.bruce.homework0305.demostruts.LoginAction"; + Class clz = Class.forName(name); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/Struts.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/Struts.java new file mode 100644 index 0000000000..bbdc51166f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/Struts.java @@ -0,0 +1,53 @@ +package com.bruce.homework0305.demostruts; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + private static final Configuration cfg = new Configuration("struts.xml"); + + public static View runAction(String actionName, Map parameters) throws DocumentException, + ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { + + /* + 0. 读取配置文件struts.xml + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + + String className = cfg.getClassName(actionName); + if(className == null){ + return null; + } + Class clz = Class.forName(className); + Object action = clz.newInstance(); + ReflectUtil.setParameters(action, parameters); + Method execute = clz.getDeclaredMethod("execute"); + String resultName = (String) execute.invoke(action); + String resultView = cfg.getResultView(actionName,resultName); + Map params = ReflectUtil.getParameters(action); + View view = new View(); + view.setJsp(resultView); + view.setParameters(params); + return view; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/StrutsTest.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/StrutsTest.java new file mode 100644 index 0000000000..8726636484 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/StrutsTest.java @@ -0,0 +1,66 @@ +package com.bruce.homework0305.demostruts; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + try { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + + @Test + public void testLoginActionFailed() { + try { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/demostruts/View.java b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/View.java new file mode 100644 index 0000000000..4fb2eb99c5 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/demostruts/View.java @@ -0,0 +1,23 @@ +package com.bruce.homework0305.demostruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/DownloadThread.java b/group15/1510_739253131/src/com/bruce/homework0312/download/DownloadThread.java new file mode 100644 index 0000000000..2a75d99919 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/DownloadThread.java @@ -0,0 +1,44 @@ +package com.bruce.homework0312.download; + +import com.bruce.homework0312.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + private Connection conn; + private int startPos; + private int endPos; + private String localFile; + private CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + public void run(){ + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + try { + byte[] data = conn.read(startPos, endPos); + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + file.seek(startPos); + file.write(data); + file.close(); + conn.close(); + barrier.await(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/FileDownloader.java b/group15/1510_739253131/src/com/bruce/homework0312/download/FileDownloader.java new file mode 100644 index 0000000000..1708b38c3f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/FileDownloader.java @@ -0,0 +1,111 @@ +package com.bruce.homework0312.download; + +import com.bruce.homework0312.download.api.Connection; +import com.bruce.homework0312.download.api.ConnectionException; +import com.bruce.homework0312.download.api.ConnectionManager; +import com.bruce.homework0312.download.api.DownloadListener; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + + +public class FileDownloader { + + private String url; + + private String localFile; + + private DownloadListener listener; + + private ConnectionManager cm; + + private static final int DOWNLOAD_THREAD_COUNT = 3; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_COUNT, new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + createPlaceHolderFile(this.localFile, length); + int[][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_COUNT, length); + for (int i = 0; i < DOWNLOAD_THREAD_COUNT; i++) { + DownloadThread thread = new DownloadThread(conn, ranges[i][0], ranges[i][1], localFile, barrier); + thread.start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + } + + //创建一个大小为length的空文件,准备存放下载内容 + private void createPlaceHolderFile(String localFile, int length) throws IOException { + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + for (int i = 0; i < length; i++) { + file.write(0); + } + file.close(); + } + + //根据文件总长度和下载的线程数量,“切分”文件 + private int[][] allocateDownloadRange (int threadCount, int totalLength) { + //二维数组第二维长度为2,用于存放startPos和endPo + int[][] ranges = new int[threadCount][2]; + int perLen = totalLength / threadCount; + int left = totalLength % threadCount; + for (int i = 0; i < threadCount; i++) { + ranges[i][0] = i * perLen; + ranges[i][1] = (i+1) * perLen - 1; + if (i == (threadCount-1)) { + ranges[i][1] += left; + } + } + return ranges; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/FileDownloaderTest.java b/group15/1510_739253131/src/com/bruce/homework0312/download/FileDownloaderTest.java new file mode 100644 index 0000000000..c53b28c93d --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/FileDownloaderTest.java @@ -0,0 +1,49 @@ +package com.bruce.homework0312.download; + +import com.bruce.homework0312.download.api.ConnectionManager; +import com.bruce.homework0312.download.api.DownloadListener; +import com.bruce.homework0312.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + String localFile = "F:/study/test.png"; + FileDownloader downloader = new FileDownloader(url, localFile); + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/api/Connection.java b/group15/1510_739253131/src/com/bruce/homework0312/download/api/Connection.java new file mode 100644 index 0000000000..0995f6856f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.bruce.homework0312.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/api/ConnectionException.java b/group15/1510_739253131/src/com/bruce/homework0312/download/api/ConnectionException.java new file mode 100644 index 0000000000..38963d6a64 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/api/ConnectionException.java @@ -0,0 +1,9 @@ +package com.bruce.homework0312.download.api; + + +public class ConnectionException extends Exception { + + public ConnectionException(Exception e){ + super(e); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/api/ConnectionManager.java b/group15/1510_739253131/src/com/bruce/homework0312/download/api/ConnectionManager.java new file mode 100644 index 0000000000..58a1df0b19 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/api/ConnectionManager.java @@ -0,0 +1,12 @@ +package com.bruce.homework0312.download.api; + +import java.io.IOException; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException, IOException; +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/api/DownloadListener.java b/group15/1510_739253131/src/com/bruce/homework0312/download/api/DownloadListener.java new file mode 100644 index 0000000000..af61b4bf77 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.bruce.homework0312.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/impl/ConnectionImpl.java b/group15/1510_739253131/src/com/bruce/homework0312/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..495cda5669 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/impl/ConnectionImpl.java @@ -0,0 +1,74 @@ +package com.bruce.homework0312.download.impl; + +import com.bruce.homework0312.download.api.Connection; +import com.bruce.homework0312.download.api.ConnectionException; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +class ConnectionImpl implements Connection{ + + URL url; + static final int BUFFER_SIZE = 1024; + + ConnectionImpl(String _url) throws ConnectionException{ + try { + //根据字符串路径拿到一个URL对象 + url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream inputStream = httpConn.getInputStream(); + //跳过inputStream前startPos字节数据,但由于skip内部是从头读取并且跳过的,该方法达不到预期效果 +// inputStream.skip(startPos); + byte[] buffer = new byte[BUFFER_SIZE]; + int totalLen = endPos - startPos + 1; + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + while (outputStream.size() < totalLen) { + //从输入流中读取最多buffer.length个字节,并将其存储在缓冲区数组buffer中 + int read = inputStream.read(buffer); + //读到文件末尾时,inputStream.read(buffer)返回-1 + if (read < 0) { + break; + } + //将buffer中从0开始到read个字节写入outputStream + outputStream.write(buffer, 0, read); + } + + if (outputStream.size() > totalLen) { + byte[] data = outputStream.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return outputStream.toByteArray(); + } + + @Override + public int getContentLength() { + URLConnection conn; + try { + conn = url.openConnection(); + return conn.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + } + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/download/impl/ConnectionManagerImpl.java b/group15/1510_739253131/src/com/bruce/homework0312/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..0872894be8 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,18 @@ +package com.bruce.homework0312.download.impl; + +import com.bruce.homework0312.download.api.Connection; +import com.bruce.homework0312.download.api.ConnectionException; +import com.bruce.homework0312.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String path) throws ConnectionException, IOException { + return new ConnectionImpl(path); + } + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/linkedlist/LinkedListV02.java b/group15/1510_739253131/src/com/bruce/homework0312/linkedlist/LinkedListV02.java new file mode 100644 index 0000000000..0ae4c674c9 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/linkedlist/LinkedListV02.java @@ -0,0 +1,292 @@ +package com.bruce.homework0312.linkedlist; + +import com.bruce.homework0226.LinkedListV01; + +import java.util.Objects; + +public class LinkedListV02 { + private int size = 0; + private Node head; + private Node last; + + public LinkedListV02() { + //保证了初始化一个对象的时候,头节点不为空 + this.head = new Node(null); + } + + public boolean add (T element) { + //双向链表,双向都需要维护 + if(last == null){ + last = new Node<>(element); + head.next = last; + last.pre = head; + }else{ + Node oldLast = last; + last = new Node<>(element); + last.pre = oldLast; + oldLast.next = last; + } + size++; + return true; + } + + public boolean add(int index, T element) { + Node node = getNode(index); + Node newNode = new Node(element); + Node pre = node.pre; + pre.next = newNode; + newNode.pre = pre; + newNode.next = node; + size++; + return true; + } + + public boolean remove(T element){ + Node node = head; + //下一个节点不为null + while(node.next != null){ + node = node.next; + if(Objects.equals(node.element, element)){ + if(node.next != null){ + node.next.pre = node.pre; + } + node.pre.next = node.next; + size--; + return true; + } + } + //下一个节点为null,说明是尾节点 + if(node != head){ + last = node; + } + //head.next=null,说明是一个空的链表,即仅有一个空head节点 + return false; + } + + public T remove(int index){ + Node node = getNode(index); + Node pre = node.pre; + Node next = node.next; + pre.next = next; + next.pre = pre; + size--; + return node.element; + } + + public void clear(){ + for(Node x = head; x != null; ){ + Node next = x.next; + x.pre = null; + x.next = null; + x.element = null; + } + head = last = null; + size = 0; + } + + public int size(){ + return size; + } + + public boolean isEmpty(){ + return size == 0; + } + + public boolean contains(Object o){ + for(int i = 0; i < size; i++){ + if(Objects.equals(getNode(i).element, o)){ + return true; + } + } + return false; + } + + public Node getNode(int index){ + if(index < 0 || index >size){ + return null; + } + Node node = head; + for(int i = 0; i < index; i++){ + node = node.next; + } + return node; + } + + public T get(int index) { + return getNode(index).element; + } + + public int indexOf(T element){ + Node node = head; + int index = 0; + while(node.next != null){ + node = node.next; + if(Objects.equals(node.element, element)){ + return index; + } + index++; + } + return -1; + } + + private static class Node { + T element; + Node pre; + Node next; + + Node(T element) { + this.element = element; + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + T t; + for(int i = 0; i < size; i++) { + t = getNode(i).element; + getNode(i).element = getNode(size-1-i).element; + getNode(size-1-i).element = t; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(size < 2) { + return; + } + int half = size >> 1; + for(int i = 0; i < half; i++) { + remove(i); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if((i+length)<0 || (i+length)>size) { + return;//抛出异常 + } + for(int n = i - 1; n <= length; n++) { + remove(n); + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedListV02 list){ + if(list == null) { + return null; + } + int[] result = new int[list.size()]; + for(int i = 0; i < list.size(); i++) { + if(list.get(i) < size) { + result[i] = (Integer) this.get(i); + } + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedListV02 list){ + if(list == null || list.size() == 0) { + return; + } + for(int i = 0; i < list.size(); i++) { + if(this.contains(list.get(i))) { + this.remove((T) list.get(i)); + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + LinkedListV02 newList = new LinkedListV02<>(); + for(int i = 0; i < size; i++) { + if(!newList.contains(this.get(i))) { + newList.add(this.get(i)); + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(min > max) { + return; + } + if((Integer)head.element > min && (Integer)last.element < max) { + clear(); + } else if ((Integer)head.element > min && (Integer)last.element > max) { + Node temp1 = last; + Node temp2 = head; + while(temp1.pre != null) { + temp1 = temp1.pre; + if(Objects.equals(temp1.element, max)) { + last = temp1; + } else { + temp1.pre = null; + temp1.element = null; + } + temp1.next = null; + } + while(temp2.next != null) { + temp2 = temp2.next; + if(Objects.equals(temp2.element, min)) { + head = temp2; + } else { + temp2.next = null; + temp2.element = null; + } + temp2.pre = null; + } + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedListV02 intersection(LinkedListV02 list){ + if(list == null || list.size() == 0) { + return null; + } + LinkedListV02 newList = new LinkedListV02(); + for(int i = 0; i < list.size(); i ++) { + if(this.contains(list.get(i))) { + newList.add(list.get(i)); + } + } + return newList; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/mydownload/DownloadFileMultiThread.java b/group15/1510_739253131/src/com/bruce/homework0312/mydownload/DownloadFileMultiThread.java new file mode 100644 index 0000000000..5885ce822c --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/mydownload/DownloadFileMultiThread.java @@ -0,0 +1,88 @@ +package com.bruce.homework0312.mydownload; + +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Created by Bruce.Jiao on 2017/3/11. + */ +public class DownloadFileMultiThread { + private String path; + private int threadCount; + + public DownloadFileMultiThread(String path, int threadCount){ + this.path = path; + this.threadCount = threadCount; + } + + public void download() throws Exception { + URL url = new URL(path); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(6000); + conn.setRequestMethod("GET"); + //从服务器请求全部资源返回200,请求部分资源返回206 + int code = conn.getResponseCode(); + if(code == 200) { + //返回的文件的长度 + int length = conn.getContentLength(); + //在客户端本地创建一个大小跟服务器端文件大小一样的本地临时文件 + RandomAccessFile raf = new RandomAccessFile("log4j.jar","rw"); + raf.setLength(length); + raf.close(); + int blockSize = length/threadCount; + for(int threadId = 1; threadId < threadCount; threadId++) { + int startIndex = (threadId - 1)*blockSize; + int endIndex = threadId*blockSize - 1; + if(threadId == threadCount) { + endIndex = length; + } + new DownloadThread(path, threadId, startIndex, endIndex); + } + } + } + + public static class DownloadThread extends Thread { + private String path; + private int threadId; + private int startIndex; + private int endIndex; + + public DownloadThread(String path, int threadId, int startIndex, int endIndex) { + super(); + this.path = path; + this.threadId = threadId; + this.startIndex = startIndex; + this.endIndex = endIndex; + } + + @Override + public void run() { + try { + URL url = new URL(path); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(6000); + conn.setRequestMethod("GET"); + //请求服务器下载部分文件,指定文件的位置 + conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); + int code = conn.getResponseCode(); + //已经设置了请求的位置,返回的是当前位置对应的文件的输入流 + InputStream is = conn.getInputStream(); + RandomAccessFile raf = new RandomAccessFile("log4j.jar", "rw"); + //定位文件 + raf.seek(startIndex); + int len = 0; + byte[] buffer = new byte[1024]; + while((len = is.read(buffer)) != -1) { + raf.write(buffer, 0, len); + } + is.close(); + raf.close(); + System.out.println("线程:" + threadId + "下载完毕"); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0312/mydownload/DownloadTest.java b/group15/1510_739253131/src/com/bruce/homework0312/mydownload/DownloadTest.java new file mode 100644 index 0000000000..22fb46e494 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0312/mydownload/DownloadTest.java @@ -0,0 +1,17 @@ +package com.bruce.homework0312.mydownload; + +import org.junit.Test; + +public class DownloadTest { + + @Test + public void test() { + String path = "http://mirrors.tuna.tsinghua.edu.cn/apache/logging/log4j/2.8.1/apache-log4j-2.8.1-bin.zip"; + DownloadFileMultiThread dfmt = new DownloadFileMultiThread(path, 3); + try { + dfmt.download(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0402/jvm/loader/ClassFileLoader.java b/group15/1510_739253131/src/com/bruce/homework0402/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..0f6d5e60d0 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0402/jvm/loader/ClassFileLoader.java @@ -0,0 +1,61 @@ +package com.bruce.homework0402.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList<>(); + + //TODO 方法需要调试 + public byte[] readBinaryCode(String className) { + InputStream is = null; + try { + className = className.replace(".", "/"); + String path = getClassPath() + "/" + className +".class"; + URL url = new URL(path); + byte[] buff = new byte[1024*2]; + int len = -1; + is = url.openStream(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + while((len = is.read(buff)) != -1) { + outputStream.write(buff, 0, len); + } + return outputStream.toByteArray(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < clzPaths.size(); i++) { + sb.append(clzPaths.get(i)); + if(i != (clzPaths.size()-1)) { + sb.append(";"); + } + } + return sb.toString(); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0402/jvm/test/ClassFileloaderTest.java b/group15/1510_739253131/src/com/bruce/homework0402/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..71007b752c --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0402/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,71 @@ +package com.bruce.homework0402.jvm.test; + +import com.bruce.homework0402.jvm.loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileloaderTest { + + static String path1 = "/F:/coding2017/group15/1510_739253131/out/production/1510_739253131"; + static String path2 = "/C:/temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1+";"+path2,clzPath); + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.bruce.homework0402.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.bruce.homework0402.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + String acctualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i capacity) {//添加新节点后,size大于capacity,把最后一个节点去掉,last 指针向前移动一位 + last = last.prev; + last.next = null; + } + } else { + moveToFirst(node); + } + } + } + + private Node exist(int pageNum) { + Node node = first; + while(node != null) { + if (node.pageNum == pageNum) { + return node; + } + node = node.next; + } + return null; + } + + private static class Node { + Node prev; + Node next; + int pageNum; + Node() {} + Node(int pageNum) { + this.pageNum = pageNum; + } + } + + private void moveLastToFirst() { + //将first和last双向链接起来 + last.next = first; + first.prev = last; + //移动first和last的“指针” + first = last; + last = last.prev; + //打断first和last的双向链接 + last.next = null; + first.prev = null; + } + + private void addToFirst(int pageNum) { + Node node = new Node(pageNum); + node.next = first; + node.prev = null; + first.prev = node; + first = node; + size++; + } + + private void moveToFirst(Node node) { + node.prev.next = node.next; + node.next.prev = node.prev; + node.next = first; + node.prev = null; + first.prev = node; + first = node; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0402/lru/LRUPageFrameTest.java b/group15/1510_739253131/src/com/bruce/homework0402/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..26fa845d46 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0402/lru/LRUPageFrameTest.java @@ -0,0 +1,37 @@ +package com.bruce.homework0402.lru; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + System.out.println(frame.toString()); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + System.out.println(frame.toString()); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + System.out.println(frame.toString()); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + System.out.println(frame.toString()); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + System.out.println(frame.toString()); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + System.out.println(frame.toString()); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group15/1511_714512544/.idea/encodings.xml b/group15/1511_714512544/.idea/encodings.xml index cfca28230a..6e94f060a7 100644 --- a/group15/1511_714512544/.idea/encodings.xml +++ b/group15/1511_714512544/.idea/encodings.xml @@ -1,6 +1,7 @@ + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/workspace.xml b/group15/1511_714512544/.idea/workspace.xml index cb4932867a..79ca1863c1 100644 --- a/group15/1511_714512544/.idea/workspace.xml +++ b/group15/1511_714512544/.idea/workspace.xml @@ -1,7 +1,7 @@ - + - + @@ -47,25 +46,19 @@ @@ -75,25 +68,15 @@ true DEFINITION_ORDER - - - - - + @@ -107,8 +90,6 @@ - - @@ -134,8 +115,22 @@ + + + - + + + + @@ -157,7 +152,7 @@ + + - - - - - - - - - - - - - - - - - - - - + + - - + + - - + - - + - - + - - + - - + + - @@ -786,19 +779,19 @@ - - - - - + + + + + - - - - - + + + + + @@ -810,198 +803,105 @@ - - 1487829138626 + + 1488937211416 - 1487829266287 + 1488937445293 - 1487829432867 + 1489156650791 - 1487856987141 + 1489206126088 - 1487857271746 + 1489209258691 - 1487857304368 + 1489211483985 - 1488004359192 + 1489303022487 - - 1488019343082 - - - 1488019461939 - - - 1488157251542 - - - 1488157483941 - - - 1488331339727 - - - 1488372676888 - - - 1488535940734 - - - 1488543482582 - - - 1488544609996 - - - 1488547243625 - - - 1488603987319 - - - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - @@ -1017,30 +917,28 @@ + - - + + + + + + + + + - + - - - - - - - - - - - + @@ -1079,238 +977,248 @@ - - - - - - - - - + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - - + + - + - - - + + - + - - + + - - + + - + + + + + + + + - + + + + + - + - - + + - + - + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - + - + - - + + @@ -1321,246 +1229,155 @@ - + - - + + - + - - + + - + - - + + - + - - + + - + - + - + - - + + - - - - + + + + + + + - + - - + + - + - - + + - + - - + + + - + - - + + - + - - + + + - + - - + + + - + - - - - - - - - - + + + - + - - + + - - + + + + + - + - - - - - - - - - - - - - - - - - + + + - + - - - + + + + + + - + - - - + + + + + + - - - - - - - - - - - - - No facets are configured - - - - - - - - - - - - - - - 1.8 - - - - - - - - 1511_714512544 - - - - - - - - 1.8 - - - - - - - - - - - - - - \ No newline at end of file diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class index e84a8b4b40..42fd928c33 100644 Binary files a/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class index 4097ca6433..9a0793e3b3 100644 Binary files a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class index 466c28ba4f..3750b53b31 100644 Binary files a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class index 7ac1a0fa80..484783effe 100644 Binary files a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class differ diff --git "a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221AVL.md" "b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221AVL.md" new file mode 100644 index 0000000000..e6a6969727 --- /dev/null +++ "b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221AVL.md" @@ -0,0 +1,5 @@ + +1. 每个结点的平衡因子就是该结点的左子树的高度减去右子树的高度,平衡二叉树的每个结点的平衡因子的绝对值不会超过2。 +2. 右旋口诀:左子作父,父为右子,右孙变左孙 + 左旋口诀:右子作父,父为左子,左孙变右孙 + diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class index 2b2fb7b768..8375422df0 100644 Binary files a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class differ diff --git a/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java b/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java index cd48c18e32..a3013ef153 100644 --- a/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java +++ b/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java @@ -12,6 +12,7 @@ public class ArrayUtil { * @return */ public void reverseArray(int[] origin){ + if(origin == null) return ; //边界条件 int len = origin.length; int temp = 0; for (int i = 0; i < len/2; i++) { @@ -30,6 +31,7 @@ public void reverseArray(int[] origin){ */ public int[] removeZero(int[] oldArray){ + if(oldArray == null) return null; //边界条件 int[] temp = new int[oldArray.length]; int index = 0; for (int i : oldArray) { @@ -52,6 +54,10 @@ public int[] removeZero(int[] oldArray){ */ public int[] merge(int[] array1, int[] array2){ + if(array1 == null && array2 == null ) return null; + if(array1 == null) return array2; + if(array2 == null) return array1; + int[] array3 = new int[array1.length+array2.length]; for (int i = 0; i < array1.length; i++) { array3[i] = array1[i]; diff --git a/group15/1511_714512544/src/com/coderising/download/Demo.java b/group15/1511_714512544/src/com/coderising/download/Demo.java new file mode 100644 index 0000000000..72f9d8efc0 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/Demo.java @@ -0,0 +1,99 @@ +package com.coderising.download; + +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Demo { + public static String path = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png"; + public static int threadCount = 3; + public static void main(String[] args) throws Exception{ + //1.连接服务器,获取一个文件,获取文件的长度,在本地创建一个跟服务器一样大小的临时文件 + URL url = new URL(path); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + conn.setConnectTimeout(5000); + conn.setRequestMethod("GET"); + int code = conn.getResponseCode(); + if (code == 200) { + //服务器端返回的数据的长度,实际上就是文件的长度 + int length = conn.getContentLength(); + System.out.println("文件总长度:"+length); + //在客户端本地创建出来一个大小跟服务器端一样大小的临时文件 + RandomAccessFile raf = new RandomAccessFile("d:/1.png", "rwd"); + //指定创建的这个文件的长度 + raf.setLength(length); + raf.close(); + //假设是3个线程去下载资源。 + //平均每一个线程下载的文件大小. + int blockSize = length / threadCount; + for (int threadId = 1; threadId <= threadCount; threadId++) { + //第一个线程下载的开始位置 + int startIndex = (threadId - 1) * blockSize; + int endIndex = threadId * blockSize - 1; + if (threadId == threadCount) {//最后一个线程下载的长度要稍微长一点 + endIndex = length; + } + System.out.println("线程:"+threadId+"下载:---"+startIndex+"--->"+endIndex); + new DownLoadThread(path, threadId, startIndex, endIndex).start(); + } + + }else { + System.out.printf("服务器错误!"); + } + } + + /** + * 下载文件的子线程 每一个线程下载对应位置的文件 + * @author jie + * + */ + public static class DownLoadThread extends Thread{ + private int threadId; + private int startIndex; + private int endIndex; + /** + * @param path 下载文件在服务器上的路径 + * @param threadId 线程Id + * @param startIndex 线程下载的开始位置 + * @param endIndex 线程下载的结束位置 + */ + public DownLoadThread(String path, int threadId, int startIndex, int endIndex) { + super(); + this.threadId = threadId; + this.startIndex = startIndex; + this.endIndex = endIndex; + } + + @Override + public void run() { + try { + URL url = new URL(path); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + conn.setConnectTimeout(5000); + conn.setRequestMethod("GET"); + //重要:请求服务器下载部分文件 指定文件的位置 + conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); + //从服务器请求全部资源返回200 ok如果从服务器请求部分资源 返回 206 ok + int code = conn.getResponseCode(); + System.out.println("code:"+code); + InputStream is = conn.getInputStream();//已经设置了请求的位置,返回的是当前位置对应的文件的输入流 + RandomAccessFile raf = new RandomAccessFile("d:/1.png", "rwd"); + //随机写文件的时候从哪个位置开始写 + raf.seek(startIndex);//定位文件 + + int len = 0; + byte[] buffer = new byte[1024]; + while ((len = is.read(buffer)) != -1) { + raf.write(buffer, 0, len); + } + is.close(); + raf.close(); + System.out.println("线程:"+threadId+"下载完毕"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + } +} \ No newline at end of file diff --git a/group15/1511_714512544/src/com/coderising/download/DownloadThread.java b/group15/1511_714512544/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..9173329f7b --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,58 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionImpl; +import com.coderising.download.impl.ConnectionManagerImpl; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.net.URL; + +public class DownloadThread extends Thread{ + int startPos; + int endPos; + String url; + String savePath; + DownloadListener listener; + private static int count = 0; + private Object lock = new Object(); //对象锁 + + public DownloadThread(String url, String savePath, DownloadListener listener, int startPos, int endPos){ + this.startPos = startPos; + this.endPos = endPos; + this.url = url; + this.savePath = savePath; + this.listener = listener; + } + public void run(){ + RandomAccessFile raf = null; + //实现 + try { + Connection conn = new ConnectionManagerImpl().open(url); + raf = new RandomAccessFile(savePath,"rwd"); + + byte[] data= conn.read(startPos,endPos); + + raf.seek(startPos); + raf.write(data); + synchronized (lock){ //加对象锁 + count ++; + if(count == 3){ + listener.notifyFinished(); + } + } + + } catch (Exception e) { + throw new RuntimeException("读取错误"); + }finally { + try { + if(raf != null){ + raf.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/group15/1511_714512544/src/com/coderising/download/FileDownloader.java b/group15/1511_714512544/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c620180530 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,81 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +import java.io.RandomAccessFile; + +/** + * 文件下载器 + */ +public class FileDownloader { + String url; //下载路径 + String savePath = "d:/1.png"; //保存路径 + DownloadListener listener ; //下载监听器 + ConnectionManager cm ; //连接管理 + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + RandomAccessFile raf = null; + try { + cm = new ConnectionManagerImpl(); + conn = cm.open(this.url); + int length = conn.getContentLength(); + + raf = new RandomAccessFile(savePath,"rwd"); + if(raf.length() == 0){ + raf.setLength(length); + } + raf.close(); + + for (int i = 0; i <= 2; i++) { + int startPos = i*length/3; + int endPos = length*(i+1)/3-1; + if(i == 2) { + endPos = length-1; + } + new DownloadThread(url, savePath,listener ,startPos, endPos).start(); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group15/1511_714512544/src/com/coderising/download/FileDownloaderTest.java b/group15/1511_714512544/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..11d8087369 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,58 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; //是否下载完 + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://www.baidu.com/img/bd_logo1.png"; + + FileDownloader downloader = new FileDownloader(url); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); //设置连接管理器 + + downloader.setListener(new DownloadListener() { //设置监听器 + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); //执行下载 + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("Down!!!"); + + + + } + +} diff --git a/group15/1511_714512544/src/com/coderising/download/api/Connection.java b/group15/1511_714512544/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..d2c1b08dba --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/api/Connection.java @@ -0,0 +1,24 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); + +} diff --git a/group15/1511_714512544/src/com/coderising/download/api/ConnectionException.java b/group15/1511_714512544/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group15/1511_714512544/src/com/coderising/download/api/ConnectionManager.java b/group15/1511_714512544/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group15/1511_714512544/src/com/coderising/download/api/DownloadListener.java b/group15/1511_714512544/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1511_714512544/src/com/coderising/download/impl/ConnectionImpl.java b/group15/1511_714512544/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..d655844039 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,68 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + private HttpURLConnection connection; + + public ConnectionImpl(HttpURLConnection connection) { + this.connection = connection; + } + + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + @Override + + public byte[] read(int startPos, int endPos) throws IOException { + byte[] buffer = new byte[endPos-startPos+1]; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + connection.setRequestProperty("Range", "bytes="+startPos+"-"+endPos); + int res = connection.getResponseCode(); + if(res == 206){ //下载部分内容请求成功 + InputStream in = connection.getInputStream(); + + int len = 0; + byte[] b = new byte[1024]; + while ((len = in.read(b)) != -1) { + bos.write(b, 0, len); + } + buffer = bos.toByteArray(); + } + + return buffer; + } + /** + * 得到数据内容的长度 + * @return + */ + @Override + public int getContentLength() { + return connection.getContentLength(); + } + /** + * 关闭连接 + */ + @Override + public void close() { + InputStream in; + try { + in = connection.getInputStream(); + if(in != null){ + in.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/group15/1511_714512544/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group15/1511_714512544/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..55d27dc156 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,38 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + @Override + public Connection open(String url) throws ConnectionException { + try { + URL u = new URL(url); + HttpURLConnection conn = (HttpURLConnection) u.openConnection(); + conn.setConnectTimeout(5000); + conn.setRequestMethod("GET"); + int code = conn.getResponseCode(); + if(code == 200){ + return new ConnectionImpl(conn); + }else { + throw new RuntimeException("打开连接失败"); + } + } catch (MalformedURLException e) { + throw new RuntimeException("url非法"); + } catch (IOException e) { + throw new RuntimeException("IO异常"); + } + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/LinkedList.java b/group15/1511_714512544/src/com/coding/basic/LinkedList.java index e806c0e6a2..423a56e2a8 100644 --- a/group15/1511_714512544/src/com/coding/basic/LinkedList.java +++ b/group15/1511_714512544/src/com/coding/basic/LinkedList.java @@ -1,6 +1,7 @@ package com.coding.basic; import java.util.NoSuchElementException; +import java.util.Objects; public class LinkedList implements List { private Node head; //首节点 @@ -51,6 +52,7 @@ public Object get(int index){ //取出指定节点处的元素,从0开始 return temp.data; } + public Object remove(int index){ //删除指定索引处的节点 if(index > size -1 || index < 0) throw new RuntimeException("IndexOutOfBounds"); if(index == 0) { //第一个元素或只有一个元素 @@ -154,4 +156,214 @@ public Node(Object data, Node next) { this.next = next; } } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node next = null; //当前节点下面一个 + Node pre = null; //当前节点前面一个 + + while(head != null){ + next = head.next; + head.next = pre; + pre = head; + head = next; + } + head = pre; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(size() == 0 || size() == 1) return; + + int half = size()/2; //一半数目 + int sum = 0; //已移除总个数 + while(sum != half){ + head = head.next; + sum ++; + size --; + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(length < 0 ) throw new RuntimeException("长度非法"); + if(i < 0 || i >= size() || (i+length >size())) throw new RuntimeException("索引越界"); + + if(i == 0){ + int sum = 0; + while(sum != length){ + head = head.next; + sum ++; + size --; + } + return; + } + Node pre = findNode(i-1); //前一节点 + Node next = findNode(i+length); //后一节点 + pre.next = next; + size -= length; + } + //查找某个位置的Node + private Node findNode(int i){ + if(i < 0 ||i > size()) throw new RuntimeException("索引越界"); + if(i == size() ) return null; + int index = 0; + Node temp = head; + while(index != i){ + temp = temp.next; + index ++; + } + return temp; + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int sum = list.size(); + int[] arr = new int[sum]; + for (int i = 0; i < sum; i++) { + Integer index = (Integer) list.get(i); + Node temp = findNode(index); + if(temp == null){ + throw new RuntimeException("索引越界"); + } + arr[i] = (Integer) temp.data; + } + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + public void subtract(LinkedList list){ + for (int i = 0; i < list.size; i++) { + int index = indexOf(list.get(i)); + if(index < 0) continue; //没找到相关节点 + if(index == 0){ + head = head.next; + size --; + }else { + Node pre = findNode(index-1); + Node next = findNode(index+1); + pre.next = next; + size --; + } + } + } + //索引对象,返回索引值 + private int indexOf(Object o){//返回索引 + for (int i = 0; i < size(); i++) { + if(Objects.equals(o, get(i))){ //判断两个Object对象是否相等 + return i; + } + } + return -1; + } + + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(size() == 0 || size() == 1) return; + Node current = head; + + while(current.next != null){ + if(Objects.equals(current.data, current.next.data)){ + current.next = current.next.next; + size --; + }else { + current = current.next; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(size() == 0) return; + if(((Integer)get(0)) >= max ) return; + if( ((Integer)get(size()-1)) <= min ) return; + + if(((Integer)get(0)) <= min && ((Integer)get(size()-1)) < max && ((Integer)get(size()-1)) > min){ + for (int i = 1; i < size(); i++) { + if(((Integer)get(i)) >min) {findNode(i-1).next = null;size = i;return;} + } + }else if(((Integer)get(0)) > min && ((Integer)get(0)) < max && ((Integer)get(size()-1)) >= max){ + for (int i = 0; i < size(); i++) { + if(((Integer)get(i)) >= max){head = findNode(i);size -= 4;return;} + } + }else if(((Integer)get(0)) <= min && ((Integer)get(size()-1)) >= max){ + Node t1=null, t2 =null; + int index1=0, index2=0; + for (int i = 1; i < size(); i++) { + if(((Integer)get(i)) > min){index1 = i;t1 = findNode(i-1);break;} + } + for (int i = 1; i < size(); i++) { + if(((Integer)get(i)) >= max){index2 = i;t2 = findNode(i);break;} + } + t1.next = t2; + size -= (index2-index1); + }else { + head = null; + size = 0; + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + int[] arr1 = new int[this.size()]; + int[] arr2 = new int[list.size()]; + + for (int i = 0; i < this.size(); i++) { + Integer num = (Integer) this.get(i); + arr1[i] = num; + } + for (int i = 0; i < list.size(); i++) { + Integer num = (Integer) list.get(i); + arr2[i] = num; + } + LinkedList l = new LinkedList(); + for (int i = 0; i < arr1.length; i++) { + for (int j = 0; j < arr2.length; j++) { + if(arr1[i] == arr2[j]){ + l.add(arr1[i]); + break; + } + } + } + return l; + } + + } diff --git a/group15/1511_714512544/src/com/coding/basic/List.java b/group15/1511_714512544/src/com/coding/basic/List.java index 4cc4f6fe5b..878c96ebd5 100644 --- a/group15/1511_714512544/src/com/coding/basic/List.java +++ b/group15/1511_714512544/src/com/coding/basic/List.java @@ -13,4 +13,7 @@ public interface List { Object remove(int index); int size(); + + + } diff --git "a/group15/1511_714512544/src/com/coding/basic/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221AVL.md" "b/group15/1511_714512544/src/com/coding/basic/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221AVL.md" new file mode 100644 index 0000000000..e6a6969727 --- /dev/null +++ "b/group15/1511_714512544/src/com/coding/basic/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221AVL.md" @@ -0,0 +1,5 @@ + +1. 每个结点的平衡因子就是该结点的左子树的高度减去右子树的高度,平衡二叉树的每个结点的平衡因子的绝对值不会超过2。 +2. 右旋口诀:左子作父,父为右子,右孙变左孙 + 左旋口诀:右子作父,父为左子,左孙变右孙 + diff --git a/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java b/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java index e5a2617247..0df3ba4a00 100644 --- a/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java +++ b/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java @@ -4,6 +4,8 @@ import com.coding.basic.LinkedList; import org.junit.Test; +import java.util.Arrays; + import static org.junit.Assert.*; /** @@ -146,4 +148,147 @@ public void iterator() throws Exception { } } + @Test + public void reverse(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.reverse(); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void removeFirstHalf(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(4); + list.add(5); + list.add(6); + list.removeFirstHalf(); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void removeByLength(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(4); + list.add(5); + list.add(6); + list.remove(1,2); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void getElements(){ + LinkedList list = new LinkedList(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + LinkedList index = new LinkedList(); + index.add(1); + index.add(3); + index.add(4); + index.add(6); + int[] arr = list.getElements(index); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void subtract(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(4); + list.add(5); + list.add(6); + LinkedList l2 = new LinkedList(); + l2.add(1); + l2.add(3); + l2.add(4); + l2.add(6); + list.subtract(l2); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void removeDuplicateValues(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(2); + list.add(4); + list.add(5); + list.add(5); + list.add(6); + list.add(8); + list.add(10); + list.removeDuplicateValues(); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void removeRange(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(2); + list.add(4); + list.add(5); + list.add(5); + list.add(6); + list.add(8); + list.add(10); + list.removeRange(3,9); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void intersection(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(4); + list.add(5); + list.add(6); + LinkedList l2 = new LinkedList(); + l2.add(1); + l2.add(3); + l2.add(4); + l2.add(6); + LinkedList l3 = list.intersection(l2); + Iterator iterator = l3.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } } \ No newline at end of file diff --git "a/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" "b/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" index 3a34a9a3fb..00bbadef42 100644 --- "a/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" +++ "b/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" @@ -1,3 +1,5 @@ (1)介绍CPU,内存,硬盘,指令以及他们之间的关系的文章地址:http://www.jianshu.com/p/f86ca5072c5d (2)程序的机器及表示: http://www.jianshu.com/p/1eed6fe682cd + +(3)测试驱动开发 http://www.jianshu.com/p/db21bbd6d370 diff --git a/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coderising/jvm/loader/ClassFileLoader.java b/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..1ef6855a58 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,54 @@ +package jvm_LRU_170402.coderising.jvm.loader; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + byte[] result = null; + className = className.replace('.', '\\'); + String path = clzPaths.get(0)+"\\"+className+".class"; + RandomAccessFile raf = null; + try { + raf = new RandomAccessFile(path, "r"); + int length = (int) raf.length(); + result = new byte[length]; + raf.read(result); + + } catch (IOException e) { + e.printStackTrace(); + }finally{ + if(raf != null){ + try{ + raf.close(); + }catch(IOException e){ + e.printStackTrace(); + } + } + } + return result; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < clzPaths.size(); i++) { + if(i==clzPaths.size()-1){ + sb.append(clzPaths.get(i)); + break; + } + sb.append(clzPaths.get(i)); + sb.append(";"); + } + return sb.toString(); + } +} diff --git a/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coderising/jvm/test/ClassFileloaderTest.java b/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..471308083c --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,89 @@ +package jvm_LRU_170402.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import jvm_LRU_170402.coderising.jvm.loader.ClassFileLoader; + + +public class ClassFileloaderTest { + + + static String path1 = "d:\\HomeWork\\bin"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "task0402.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1066, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "task0402.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i 0) { + setFirst(newNode); + capacity--; + } else { + setFirst(newNode); + popNode(last.prev); + } + + } + + private void popNode(Node target) { + target.prev.next = target.next; + target.next.prev = target.prev; + } + + private void setFirst(Node target) { + target.prev = first; + target.next = first.next; + first.next.prev = target; + first.next = target; + } + + public Node findNode(int pageNum) { + Node node = first; + while (node.next != last) { + node = node.next; + if (pageNum == node.pageNum) { + return node; + } + } + return null; + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first.next; + while (node != last) { + buffer.append(node.pageNum); + + node = node.next; + if (node != last) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coding/basic/linklist/LRUPageFrameTest.java b/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..639a5ff656 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/jvm_LRU_170402/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package jvm_LRU_170402.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/ArrayList.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/ArrayList.java new file mode 100644 index 0000000000..5d38553f43 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/ArrayList.java @@ -0,0 +1,111 @@ +package task0228.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + int len = size + 1; + + if (len > elementData.length) { + + Object[] newElemDate = new Object[elementData.length + 1]; + + System.arraycopy(elementData, 0, newElemDate, 0, elementData.length); + elementData = newElemDate; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + + if (index == size) { + add(o); + } else { + + Object[] newElemData = new Object[elementData.length + 1]; + + System.arraycopy(elementData, 0, newElemData, 0, index); + newElemData[index] = o; + + System.arraycopy(elementData, index, newElemData, index + 1, size - index); + + elementData = newElemData; + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Object removeElement = elementData[index]; + + if(index != (size-1)){ + + Object[] newElemData = new Object[elementData.length]; + + System.arraycopy(elementData, 0, newElemData, 0, index); + + System.arraycopy(elementData, index+1, newElemData, index, size - index -1); + } + + size--; + return removeElement; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + private int cursor = 0; + + + private MyIterator() {} + + @Override + public boolean hasNext() { + return cursor < size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if (cursor >= size) { + throw new IndexOutOfBoundsException(); + } + return get(cursor++); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (cursor <= 0) { + throw new NoSuchElementException(); + } + Object val = ArrayList.this.remove(--cursor); + return val; + } + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/BinaryTreeNode.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e26c68aa6c --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package task0228.coding.basic; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + // жϵǰڵԪ + if (data == null) { + setData(o); + } else { + Integer i = (Integer) o; + // ǰڵжҽڵ + if (i.compareTo((Integer) data) == -1) { + if(right == null) + right = new BinaryTreeNode(); + return right.insert(i); + } else if (i.compareTo((Integer) data) == 1) { + if(left == null) + left = new BinaryTreeNode(); + return left.insert(i); + } + return null; + } + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Iterator.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Iterator.java new file mode 100644 index 0000000000..a42b0148cb --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package task0228.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/LinkedList.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/LinkedList.java new file mode 100644 index 0000000000..226351337c --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/LinkedList.java @@ -0,0 +1,170 @@ +package task0228.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; + private int size; + + public void add(Object o) { + // жͷǷ + if (head == null) { + head = new Node(o, null); + } else { + Node newNode = head; + while (newNode.next != null) { + newNode = newNode.next; + } + newNode.next = new Node(o, null); + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Node node = head; + if (index != 0) { + // ǵһֵҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } else { + // һֵͽͷڵָ + Node newNode = new Node(o, head); + head = newNode; + size++; + } + } + + public Object get(int index) { + indexCheck(index); + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + indexCheck(index); + + Node node = head; + Node removeNode; + if (index == 0) { + //ɾһڵͰͷڵָԭĵڶڵ + removeNode = head; + head = head.next; + } else { + //ҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + //ǰһڵָ룬ָɾڵָĽڵ + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + //ûԪؾ쳣 + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Object val = head.data; + head = head.next; + size--; + return val; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Node node = head; + while (node.next != null) { + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator{ + private int poi = -1; + private LinkedList list ; + private MyIterator(LinkedList list) { + this.list= list; + } + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return (poi + 1) < list.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= list.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return list.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + poi--; + return val; + } + + } + + private void indexCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/List.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/List.java new file mode 100644 index 0000000000..2b6d70155f --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/List.java @@ -0,0 +1,9 @@ +package task0228.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Queue.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Queue.java new file mode 100644 index 0000000000..87a19d952f --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package task0228.coding.basic; + +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o);; + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object val = list.removeFirst(); + size--; + return val; + } + + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + + public int size(){ + return size; + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Stack.java b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Stack.java new file mode 100644 index 0000000000..504993e9a0 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0228/coding/basic/Stack.java @@ -0,0 +1,41 @@ +package task0228.coding.basic; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + Object val = elementData.remove(len); + size--; + return val; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + public int size(){ + return size; + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0305/coding/basic/array/ArrayUtil.java b/group15/1513_121469918/HomeWork/src/task0305/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..ad2eda4e84 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0305/coding/basic/array/ArrayUtil.java @@ -0,0 +1,291 @@ +package task0305.coding.basic.array; + + +import java.util.Arrays; +import java.util.TreeSet; + +import task0228.coding.basic.ArrayList; +import task0228.coding.basic.Iterator; + + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if(origin == null){ + return ; + } + int len = origin.length; + for (int i = 0; i < len / 2; i++) { + int temp = origin[i]; + origin[i] = origin[len - 1 - i]; + origin[len - 1 - i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int count = 0; + + // 创建一个临时数组装没有零的旧数组 + int[] temp = new int[oldArray.length - count]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + // 如果值为0,统计、跳过不加入新数组 + count++; + continue; + } else { + temp[i - count] = oldArray[i]; + } + } + // 定义返回数组的长度 + int len = oldArray.length - count; + int[] resultArray = new int[len]; + System.arraycopy(temp, 0, resultArray, 0, len); + return resultArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { +/* int[] result = array1; + // 去除重复元素 + for (int i = 0; i < array2.length; i++) { + boolean sameVal = false; + for (int j = 0; j < array1.length; j++) { + if (array1[j] == array2[i]) { + sameVal = true; + break; + } + } + if(sameVal == false){ + result = grow(result, 1); + result[result.length-1] = array2[i]; + } + } + //冒泡排序 + for (int i = 0; i < result.length-1; i++) { + for (int j = i+1; j < result.length; j++) { + if(result[i]>result[j]){ + int temp = result[i]; + result[i] = result[j]; + result[j] =temp; + } + } + } + return result; +*/ + int len1=0,len2=0;//arr1长度len1 + ArrayList list = new ArrayList(); + for (int k=0;k < array1.length+array2.length; k++) { + //如果两个数组都还有元素 + if(len1array2[len2]){ + list.add(array2[len2]); + len2++; + }else{ + list.add(array1[len1]); + len1++; + len2++; + } + }else if(len1==array1.length && len2 < array2.length){ + //如果数组1没有元素,并且2有元素 + list.add(array2[len2]); + len2++; + }else if(len2==array2.length && len1 < array1.length){ + //数组2没有元素,并且1有元素 + list.add(array1[len1]); + len1++; + }else{ + break; + } + } + //list转数组 + int[] result = new int[list.size()]; + Iterator it = list.iterator(); + int index = 0; + while(it.hasNext()){ + result[index++] = ((Integer)it.next()).intValue(); + } + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] resultArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, resultArray, 0, oldArray.length); + return resultArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } else { + int[] temp = new int[max]; + temp[0] = 1; + temp[1] = 1; + // 定义返回数组的长度变量 + int len = 2; + for (int i = 2; i < max; i++) { + int last = temp[i - 1] + temp[i - 2]; + if (last >= max) { + break; + } else { + temp[i] = last; + len = i + 1; + } + } + return Arrays.copyOf(temp, len); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + if (max < 3) { + return new int[0]; + } else { + int[] temp = new int[max]; + int count = 0; + // 从零开始遍历到max,如果有是素数就加入临时数组。 + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + temp[i - count] = i; + } else { + count++; + } + } + // max -1 -count是最后一个元素索引 + int len = max - count; + int[] resultArray = Arrays.copyOf(temp, len); + return resultArray; + } + } + + boolean isPrimes(int x) { + if (x <= 1) { + return false; + } else { + for (int i = 2; i < x; i++) { + if (x % i == 0) { + return false; + } + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 1) { + return new int[0]; + } else { + ArrayList array = new ArrayList(); + for (int i = 1; i <= max; i++) { + if (isPerfectNumber(i)) { + array.add(i); + } + } + int[] result = new int[array.size()]; + Iterator it = array.iterator(); + int index = 0; + while (it.hasNext()) { + result[index] = ((Integer) it.next()).intValue(); + index++; + } + return result; + } + } + + boolean isPerfectNumber(int x) { + if (x < 1) { + return false; + } else { + int count = 0; + for (int i = 1; i < x; i++) { + if (x % i == 0) { + count += i; + } + } + if (x == count) { + return true; + } + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + if(array == null){ + return null; + } + if(array.length ==0){ + return ""; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + if(i == array.length-1){ + sb.append(String.valueOf(array[i])); + break; + } + sb.append(String.valueOf(array[i])+seperator); + } + String result = sb.toString(); + return result; + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/LoginAction.java b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..613ffad09a --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package task0305.conderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/Struts.java b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/Struts.java new file mode 100644 index 0000000000..d522866c92 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/Struts.java @@ -0,0 +1,116 @@ +package task0305.conderising.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + + + //读取配置文件 + try { + Document doc =new SAXReader().read(new File("./src/com/coderising/litestruts/struts.xml")); + //获取XML中的action标签 + Element loginName = (Element)doc.selectSingleNode("//action[1]"); + Element logoutName = (Element)doc.selectSingleNode("//action[2]"); + //判断action的name属性内容 + if(actionName !=null && actionName.equals(loginName.attributeValue("name"))){ + //传入的actionName内容为login则进行login操作 + + //获取class路径 + String s = loginName.attributeValue("class"); + Class c = Class.forName(s); + Constructor con = c.getConstructor(); + //实例化LoginAction + LoginAction login =(LoginAction) con.newInstance(); + + //通过parameters获取name,password + String executeName = parameters.get("name"); + String executePassword = parameters.get("password"); + + //获取 setter 方法,并调用 + Method m = c.getMethod("setName", String.class); + m.invoke(login, executeName); + m =c.getMethod("setPassword", String.class); + m.invoke(login, executePassword); + + //调用LoginAction 的exectue 方法 exectueResult值为:帐号密码正确返回success,反之为fail + m = c.getMethod("execute"); + String exectueResult = (String)m.invoke(login); + + //获取 getMessage 方法 + m = c.getMethod("getMessage"); + String message =(String) m.invoke(login); + m = c.getMethod("getName"); + String name =(String) m.invoke(login); + m = c.getMethod("getPassword"); + String password =(String) m.invoke(login); + + //创建HashMap,将登录操作返回的3个变量放到Map + HashMap hm = new HashMap(); + hm.put("message", message); + hm.put("name", name); + hm.put("password", password); + + //创建View对象 + View view = new View(); + view.setParameters(hm); + + //读取struts.xml中的 配置 + List list =(List) doc.selectNodes("//action[@name = 'login']/result"); + for(Element e : list){ + String resultName = e.attributeValue("name"); + //exectue返回值比对XML下result 对应的值返回对应的jsp + if(resultName.equals(exectueResult)){ + //将对应的jsp 放到view对象中 + view.setJsp(e.getText()); + } + } + return view; + + }else if(actionName !=null && actionName.equals(logoutName.attributeValue("name"))){ + //actionName是logout则进行logout操作 + } + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/StrutsTest.java b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b21a3b5f9a --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +package task0305.conderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/View.java b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/View.java new file mode 100644 index 0000000000..0afc6e5caa --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0305/conderising/litestruts/View.java @@ -0,0 +1,23 @@ +package task0305.conderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/DownloadThread.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..2f102293cc --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/DownloadThread.java @@ -0,0 +1,59 @@ +package task0312.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; + +import task0312.coderising.download.api.Connection; +import task0312.coderising.download.api.DownloadListener; +import task0312.coderising.download.impl.ConnectionManagerImpl; + + + +public class DownloadThread extends Thread { + private static int finishCount =0; + String url; + String localPath; + int startPos; + int endPos; + private Object lock = new Object(); + DownloadListener listener; + + public DownloadThread(String url,String localPath,int startPos, int endPos, + DownloadListener listener) { + this.url =url; + this.startPos = startPos; + this.endPos = endPos; + this.localPath = localPath; + this.listener =listener; + } + public void run() { + RandomAccessFile ras = null; + Connection conn= null; + try { + ConnectionManagerImpl cm = new ConnectionManagerImpl(); + conn = cm.open(url); + byte[] download = conn.read(startPos, endPos); + ras = new RandomAccessFile(localPath, "rwd"); + ras.seek(startPos); + ras.write(download); + synchronized(lock){ + finishCount++; + if(finishCount == 6){ + listener.notifyFinished(); + } + } + }catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally { + if(ras!=null){ + try { + ras.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/FileDownloader.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..b3509c9642 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/FileDownloader.java @@ -0,0 +1,80 @@ +package task0312.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; + +import task0312.coderising.download.api.Connection; +import task0312.coderising.download.api.ConnectionManager; +import task0312.coderising.download.api.DownloadListener; +import task0312.coderising.download.impl.ConnectionManagerImpl; + + +public class FileDownloader { + Thread[] threadList = new Thread[6]; + String url; + DownloadListener listener; + ConnectionManager cm; + + public FileDownloader(String _url) { + this.url = _url; + } + + public void execute() { + RandomAccessFile raf = null; + try { + // 打开连接获取长度 + ConnectionManagerImpl cm = new ConnectionManagerImpl(); + Connection conn = cm.open(url); + int length = conn.getContentLength(); + + // 创建本地接收文件 + String localPath = url.substring(url.lastIndexOf('/') + 1); + raf = new RandomAccessFile(localPath, "rwd"); + raf.setLength(length); + raf.close(); + + int blockSize = length / threadList.length;// 每个线程下载的大小 + for (int threadNum = 0; threadNum < threadList.length; threadNum++) { + // 定义每个线程开始位置 + int threadStart = threadNum * blockSize; + // 定义每个线程结束位置 + int threadEnd = (threadNum + 1) * blockSize - 1; + // 定义最后线程结束位置为总长度-1 + if (threadNum == threadList.length - 1) { + threadEnd = length - 1; + } + String threadID = "Thread-" + (threadNum + 1); + threadList[threadNum] = new DownloadThread(url, localPath, threadStart, threadEnd, listener); + threadList[threadNum].start(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + // 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/FileDownloaderTest.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..546bd89efe --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/FileDownloaderTest.java @@ -0,0 +1,62 @@ +package task0312.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import task0312.coderising.download.api.ConnectionManager; +import task0312.coderising.download.api.DownloadListener; +import task0312.coderising.download.impl.ConnectionManagerImpl; + + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://img.ivsky.com/img/tupian/pre/201612/12/qingrenjie_meigui_liwu-004.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + //休眠5秒 + System.out.println("还没有下载完成,休眠五秒"); + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/Connection.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/Connection.java new file mode 100644 index 0000000000..43637a27a0 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package task0312.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/ConnectionException.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..f5e373fdf1 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/ConnectionException.java @@ -0,0 +1,7 @@ +package task0312.coderising.download.api; + +public class ConnectionException extends Exception { + public ConnectionException(String msg){ + super(msg); + } +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/ConnectionManager.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..1a3baecbc1 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package task0312.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/DownloadListener.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..60f26019d4 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package task0312.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/impl/ConnectionImpl.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..78085252d7 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,50 @@ +package task0312.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import task0312.coderising.download.api.Connection; + + + +public class ConnectionImpl implements Connection { + HttpURLConnection urlConnect; + public ConnectionImpl(HttpURLConnection urlConnect) { + this.urlConnect = urlConnect; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + byte[] result = new byte[endPos-startPos+1]; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + // 设置发出请求,指定下载部分 + urlConnect.setRequestProperty("Range", "bytes="+startPos+"-"+endPos); + int code = urlConnect.getResponseCode(); + if(code == 206){ + InputStream is = urlConnect.getInputStream(); + byte[] bys = new byte[1024]; + int len = 0; + while ((len = is.read(bys)) != -1) { + baos.write(bys, 0, len); + } + + result = baos.toByteArray(); + is.close(); + } + return result; + } + + @Override + public int getContentLength() { + return urlConnect.getContentLength(); + } + + @Override + public void close() { + + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coderising/download/impl/ConnectionManagerImpl.java b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..a4a38bd93a --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,33 @@ +package task0312.coderising.download.impl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import task0312.coderising.download.api.Connection; +import task0312.coderising.download.api.ConnectionManager; + + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) { + URL target; + Connection conn = null; + try { + target = new URL(url); + HttpURLConnection httpUrl = (HttpURLConnection) target.openConnection(); + httpUrl.setConnectTimeout(5000); + conn = new ConnectionImpl(httpUrl); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return conn; + } + +} diff --git a/group15/1513_121469918/HomeWork/src/task0312/coding/basic/linkedlist/LinkedList.java b/group15/1513_121469918/HomeWork/src/task0312/coding/basic/linkedlist/LinkedList.java new file mode 100644 index 0000000000..e7f8a75312 --- /dev/null +++ b/group15/1513_121469918/HomeWork/src/task0312/coding/basic/linkedlist/LinkedList.java @@ -0,0 +1,413 @@ +package task0312.coding.basic.linkedlist; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +import javax.management.RuntimeErrorException; + +import task0228.coding.basic.Iterator; +import task0228.coding.basic.List; + + + +public class LinkedList implements List { + private Node head; + private int size; + + public void add(Object o) { + // 判断头是否有数据 + if (head == null) { + head = new Node(o, null); + } else { + Node newNode = head; + while (newNode.next != null) { + newNode = newNode.next; + } + newNode.next = new Node(o, null); + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Node node = head; + if (index != 0) { + // 不是第一个索引值就找到索引值的前面一个节点 + for (int i = 1; i < index; i++) { + node = node.next; + } + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } else { + // 第一个索引值就将头节点指向它 + Node newNode = new Node(o, head); + head = newNode; + size++; + } + } + + public Object get(int index) { + indexCheck(index); + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Node getNode(int index) { + indexCheck(index); + Node node = head; + int i = 0; + while (i++ < index) { + node = node.next; + } + return node; + } + + public Object remove(int index) { + indexCheck(index); + Node node = head; + Node removeNode; + if (index == 0) { + // 删除第一个节点就把头节点指向原本的第二个节点 + removeNode = head; + head = head.next; + } else { + // 找到索引值的前一个节点 + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + // 前一个节点指针,指向被删除节点所指向的节点 + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + // 没有元素就抛异常 + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Object val = head.data; + head = head.next; + size--; + return val; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Node node = head; + while (node.next != null) { + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator { + private int poi = -1; + private LinkedList list; + + private MyIterator(LinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return (poi + 1) < list.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= list.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return list.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + poi--; + return val; + } + } + + private void indexCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (size == 0) { + return; + } + Node node = head; + Node node2 = new Node(head.data, null); + while (node.next != null) { + node = node.next; + node2 = new Node(node.data, node2); + } + head = node2; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + if (size == 0) { + return; + } + int i = size() / 2; + Node node = getNode(i); + head = node; + size -= i; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + indexCheck(i); + if (size == 0 || length <= 0) { + return; + } + if (length >= size) { + if (i == 0) { + head = null; + size = 0; + } else if (i == 1) { + head.next = null; + size = 1; + } else { + Node node = getNode(i - 1); + node.next = null; + size = i; + } + } else { + // lenth>0 &length101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + if (list == null) { + return new int[0]; + } + int[] result = new int[list.size()]; + Node node = head; + int len = 0; + int count = 0; + for (int i = 0; i < list.size(); i++) { + int index = (int) list.get(i); + if(index<0 || index>=size()){ + throw new NullPointerException("index:"+index); + } + while (count < index) { + node = node.next; + count++; + } + result[i] =(int)node.data; + len++; + + } + result = Arrays.copyOf(result, len); + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + for (int i = 0; i < size(); i++) { + for (int j = 0; j < list.size(); j++) { + if (get(i).equals(list.get(j))) { + remove(i--); + } + + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if (size == 0 || size == 1) { + return; + } + Node node = head; + while (node.next != null) { + if (node.data == node.next.data) { + node.next = node.next.next; + size--; + } else { + node = node.next; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + if (size == 0 || min >= max) { + return; + } + Node node = head; + int nodeValue = ((Integer) node.data).intValue(); + // 头开始元素值大于min + while (nodeValue > min && nodeValue < max) { + node = node.next; + size--; + if (node == null) { + head = null; + size = 0; + return; + } + nodeValue = ((Integer) node.data).intValue(); + } + head = node;// 当元素值大于等于max就跳出循环赋值给head + // 头开始元素值小于min + if (nodeValue < min) { + while (nodeValue < min) {// 遍历直到元素值大于min + node = node.next; + if (node.next == null) {// 最后元素值都比min小 + return; + } + nodeValue = ((Integer) node.data).intValue(); + } + if (node.next == null) { + return; + } + Node temp = new Node(null, node);// 大于min的不是最后元素则用temp.next记录当前位置 + node = node.next; + nodeValue = ((Integer) node.data).intValue(); + while (nodeValue < max) { + node = node.next; + size--; + if (node == null) { + temp.next.next = node; + return; + } + nodeValue = ((Integer) node.data).intValue(); + } + temp.next.next = node; + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + if (list == null) { + return list; + } + LinkedList result = new LinkedList(); + for (int i = 0; i < size(); i++) { + for (int j = 0; j < list.size(); j++) { + if (get(i).equals(list.get(j))) { + result.add(get(i)); + break; + } + } + } + return result; + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + for (int i = 0; i < size(); i++) { + if (i == size() - 1) { + sb.append(get(i)); + break; + } + sb.append(get(i)); + sb.append(","); + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/group15/1513_121469918/HomeWork01/.classpath b/group15/1513_121469918/HomeWork01/.classpath deleted file mode 100644 index fb565a588d..0000000000 --- a/group15/1513_121469918/HomeWork01/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/group15/1513_121469918/HomeWork01/.gitignore b/group15/1513_121469918/HomeWork01/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/group15/1513_121469918/HomeWork01/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/group15/1513_121469918/HomeWork01/.project b/group15/1513_121469918/HomeWork01/.project deleted file mode 100644 index 2865c24f37..0000000000 --- a/group15/1513_121469918/HomeWork01/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - HomeWork01 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index bcb09d4d64..0000000000 --- a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,8 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/coding/ArrayList.java=GBK -encoding//src/coding/BinaryTreeNode.java=GBK -encoding//src/coding/Iterator.java=GBK -encoding//src/coding/LinkedList.java=GBK -encoding//src/coding/List.java=GBK -encoding//src/coding/Queue.java=GBK -encoding//src/coding/Stack.java=GBK diff --git a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.jdt.core.prefs b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 7341ab1683..0000000000 --- a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group15/1513_121469918/HomeWork01/src/coding/ArrayList.java b/group15/1513_121469918/HomeWork01/src/coding/ArrayList.java deleted file mode 100644 index f85c800084..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/ArrayList.java +++ /dev/null @@ -1,118 +0,0 @@ -package coding; - -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - int len = size + 1; - // жlistijǷ - if (len > elementData.length) { - // - Object[] newElemDate = new Object[elementData.length + 1]; - // ƾԪص - System.arraycopy(elementData, 0, newElemDate, 0, elementData.length); - elementData = newElemDate; - } - elementData[size] = o; - size++; - } - - public void add(int index, Object o) { - // ǷԽ - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - // Ԫصĩβֱӵadd - if (index == size) { - add(o); - } else { - // - Object[] newElemData = new Object[elementData.length + 1]; - // indexǰԪص - System.arraycopy(elementData, 0, newElemData, 0, index); - newElemData[index] = o; - // index ԺԪص - System.arraycopy(elementData, index, newElemData, index + 1, size - index); - - elementData = newElemData; - size++; - } - } - - public Object get(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - return elementData[index]; - } - - public Object remove(int index) { - if (index >= size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - Object removeElement = elementData[index]; - //һԪصֵҪ - if(index != (size-1)){ - // - Object[] newElemData = new Object[elementData.length]; - // indexǰԪص - System.arraycopy(elementData, 0, newElemData, 0, index); - // index ԺԪص - System.arraycopy(elementData, index+1, newElemData, index, size - index -1); - } - //һԪصֱֵӼlist - size--; - return removeElement; - } - - public int size() { - return size; - } - - public Iterator iterator() { - return new MyIterator(this); - } - - private class MyIterator implements Iterator { - private int poi = -1; - private ArrayList array = null; - - private MyIterator(ArrayList array) { - this.array = array; - } - - @Override - public boolean hasNext() { - return (poi + 1) < array.size; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - poi++; - if (poi >= array.size) { - poi--; - throw new IndexOutOfBoundsException(); - } - - return array.get(poi); - } - - @Override - public Object remove() { - // TODO Auto-generated method stub - if (poi < 0) { - throw new NoSuchElementException(); - } - Object val = array.remove(poi); - poi--; - return val; - } - - } -} diff --git a/group15/1513_121469918/HomeWork01/src/coding/BinaryTreeNode.java b/group15/1513_121469918/HomeWork01/src/coding/BinaryTreeNode.java deleted file mode 100644 index 8e40fa1d90..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/BinaryTreeNode.java +++ /dev/null @@ -1,53 +0,0 @@ -package coding; - -public class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o) { - // жϵǰڵԪ - if (data == null) { - setData(o); - } else { - Integer i = (Integer) o; - // ǰڵжҽڵ - if (i.compareTo((Integer) data) == -1) { - if(right == null) - right = new BinaryTreeNode(); - return right.insert(i); - } else if (i.compareTo((Integer) data) == 1) { - if(left == null) - left = new BinaryTreeNode(); - return left.insert(i); - } - return null; - } - return null; - } - -} diff --git a/group15/1513_121469918/HomeWork01/src/coding/Iterator.java b/group15/1513_121469918/HomeWork01/src/coding/Iterator.java deleted file mode 100644 index 26ca2a672a..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package coding; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - public Object remove(); -} diff --git a/group15/1513_121469918/HomeWork01/src/coding/LinkedList.java b/group15/1513_121469918/HomeWork01/src/coding/LinkedList.java deleted file mode 100644 index 5d15f141f7..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/LinkedList.java +++ /dev/null @@ -1,170 +0,0 @@ -package coding; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - private Node head; - private int size; - - public void add(Object o) { - // жͷǷ - if (head == null) { - head = new Node(o, null); - } else { - Node newNode = head; - while (newNode.next != null) { - newNode = newNode.next; - } - newNode.next = new Node(o, null); - } - size++; - } - - public void add(int index, Object o) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - Node node = head; - if (index != 0) { - // ǵһֵҵֵǰһڵ - for (int i = 1; i < index; i++) { - node = node.next; - } - Node newNode = new Node(o, node.next); - node.next = newNode; - size++; - } else { - // һֵͽͷڵָ - Node newNode = new Node(o, head); - head = newNode; - size++; - } - } - - public Object get(int index) { - indexCheck(index); - Node node = head; - for (int i = 1; i <= index; i++) { - node = node.next; - } - return node.data; - } - - public Object remove(int index) { - indexCheck(index); - - Node node = head; - Node removeNode; - if (index == 0) { - //ɾһڵͰͷڵָԭĵڶڵ - removeNode = head; - head = head.next; - } else { - //ҵֵǰһڵ - for (int i = 1; i < index; i++) { - node = node.next; - } - removeNode = node.next; - //ǰһڵָ룬ָɾڵָĽڵ - node.next = removeNode.next; - } - size--; - return removeNode.data; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - Node newNode = new Node(o, head.next); - head.next = newNode; - size++; - } - - public void addLast(Object o) { - add(o); - } - - public Object removeFirst() { - //ûԪؾ쳣 - if (size <= 0) { - throw new IndexOutOfBoundsException("size:" + size); - } - Object val = head.data; - head = head.next; - size--; - return val; - } - - public Object removeLast() { - if (size <= 0) { - throw new IndexOutOfBoundsException("size:" + size); - } - Node node = head; - while (node.next != null) { - node = node.next; - } - Object val = node.data; - node = null; - size--; - return val; - } - - public Iterator iterator() { - return new MyIterator(this); - } - - private class MyIterator implements Iterator{ - private int poi = -1; - private LinkedList list ; - private MyIterator(LinkedList list) { - this.list= list; - } - @Override - public boolean hasNext() { - // TODO Auto-generated method stub - return (poi + 1) < list.size; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - poi++; - if (poi >= list.size) { - poi--; - throw new IndexOutOfBoundsException(); - } - - return list.get(poi); - } - - @Override - public Object remove() { - // TODO Auto-generated method stub - if (poi < 0) { - throw new NoSuchElementException(); - } - Object val = list.removeLast(); - poi--; - return val; - } - - } - - private void indexCheck(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } -} diff --git a/group15/1513_121469918/HomeWork01/src/coding/List.java b/group15/1513_121469918/HomeWork01/src/coding/List.java deleted file mode 100644 index 8b85bc5b37..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package coding; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/group15/1513_121469918/HomeWork01/src/coding/Queue.java b/group15/1513_121469918/HomeWork01/src/coding/Queue.java deleted file mode 100644 index f4b6faaa8a..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/Queue.java +++ /dev/null @@ -1,35 +0,0 @@ -package coding; - -import java.util.NoSuchElementException; - -public class Queue { - private int size; - private LinkedList list = new LinkedList(); - - public void enQueue(Object o){ - list.addLast(o);; - size++; - } - - public Object deQueue(){ - if(size<=0){ - throw new NoSuchElementException(); - } - Object val = list.removeFirst(); - size--; - return val; - } - - public boolean isEmpty(){ - boolean flag = false; - if(size >= 0){ - flag = true; - } - return flag; - } - - public int size(){ - return size; - } - -} diff --git a/group15/1513_121469918/HomeWork01/src/coding/Stack.java b/group15/1513_121469918/HomeWork01/src/coding/Stack.java deleted file mode 100644 index 742a6c4e40..0000000000 --- a/group15/1513_121469918/HomeWork01/src/coding/Stack.java +++ /dev/null @@ -1,41 +0,0 @@ -package coding; - -import java.util.NoSuchElementException; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size; - - public void push(Object o){ - elementData.add(o); - size++; - } - - public Object pop(){ - if(size<=0){ - throw new NoSuchElementException(); - } - int len = size-1; - Object val = elementData.remove(len); - size--; - return val; - } - - public Object peek(){ - if(size<=0){ - throw new NoSuchElementException(); - } - int len = size-1; - return elementData.get(len); - } - public boolean isEmpty(){ - boolean flag = false; - if(size >= 0){ - flag = true; - } - return flag; - } - public int size(){ - return size; - } -} diff --git a/group15/1513_121469918/HomeWork20170305/.classpath b/group15/1513_121469918/HomeWork20170305/.classpath deleted file mode 100644 index a851141e9a..0000000000 --- a/group15/1513_121469918/HomeWork20170305/.classpath +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/group15/1513_121469918/HomeWork20170305/.project b/group15/1513_121469918/HomeWork20170305/.project deleted file mode 100644 index 0e4fe1fc91..0000000000 --- a/group15/1513_121469918/HomeWork20170305/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - HomeWork20170305 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 67f156c482..0000000000 --- a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,9 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/com/coderising/array/ArrayUtil.java=UTF-8 -encoding//src/com/coding/basic/ArrayList.java=GBK -encoding//src/com/coding/basic/BinaryTreeNode.java=GBK -encoding//src/com/coding/basic/Iterator.java=GBK -encoding//src/com/coding/basic/LinkedList.java=GBK -encoding//src/com/coding/basic/List.java=GBK -encoding//src/com/coding/basic/Queue.java=GBK -encoding//src/com/coding/basic/Stack.java=GBK diff --git a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 7341ab1683..0000000000 --- a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml b/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml deleted file mode 100644 index a7cb57e188..0000000000 --- a/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java deleted file mode 100644 index 985ca47709..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,281 +0,0 @@ -package com.coderising.array; - - -import java.util.Arrays; -import java.util.TreeSet; -import com.coding.basic.ArrayList; -import com.coding.basic.Iterator; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = - * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * - * @param origin - * @return - */ - public void reverseArray(int[] origin) { - int len = origin.length; - for (int i = 0; i < len / 2; i++) { - int temp = origin[i]; - origin[i] = origin[len - 1 - i]; - origin[len - 1 - i] = temp; - } - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} - * - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray) { - int count = 0; - - // 创建一个临时数组装没有零的旧数组 - int[] temp = new int[oldArray.length - count]; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] == 0) { - // 如果值为0,统计、跳过不加入新数组 - count++; - continue; - } else { - temp[i - count] = oldArray[i]; - } - } - // 定义返回数组的长度 - int len = oldArray.length - count; - int[] resultArray = new int[len]; - System.arraycopy(temp, 0, resultArray, 0, len); - return resultArray; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = - * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2) { -/* int[] result = array1; - // 去除重复元素 - for (int i = 0; i < array2.length; i++) { - boolean sameVal = false; - for (int j = 0; j < array1.length; j++) { - if (array1[j] == array2[i]) { - sameVal = true; - break; - } - } - if(sameVal == false){ - result = grow(result, 1); - result[result.length-1] = array2[i]; - } - } - //冒泡排序 - for (int i = 0; i < result.length-1; i++) { - for (int j = i+1; j < result.length; j++) { - if(result[i]>result[j]){ - int temp = result[i]; - result[i] = result[j]; - result[j] =temp; - } - } - } - return result; -*/ - int len1=0,len2=0;//arr1长度len1 - ArrayList list = new ArrayList(); - for (int k=0;k < array1.length+array2.length; k++) { - //如果两个数组都还有元素 - if(len1array2[len2]){ - list.add(array2[len2]); - len2++; - }else{ - list.add(array1[len1]); - len1++; - len2++; - } - }else if(len1==array1.length && len2 < array2.length){ - //如果数组1没有元素,并且2有元素 - list.add(array2[len2]); - len2++; - }else if(len2==array2.length && len1 < array1.length){ - //数组2没有元素,并且1有元素 - list.add(array1[len1]); - len1++; - }else{ - break; - } - } - //list转数组 - int[] result = new int[list.size()]; - Iterator it = list.iterator(); - int index = 0; - while(it.hasNext()){ - result[index++] = ((Integer)it.next()).intValue(); - } - return result; - } - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param size - * @return - */ - public int[] grow(int[] oldArray, int size) { - int[] resultArray = new int[oldArray.length + size]; - System.arraycopy(oldArray, 0, resultArray, 0, oldArray.length); - return resultArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , - * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] - * - * @param max - * @return - */ - public int[] fibonacci(int max) { - if (max <= 1) { - return new int[0]; - } else { - int[] temp = new int[max]; - temp[0] = 1; - temp[1] = 1; - // 定义返回数组的长度变量 - int len = 2; - for (int i = 2; i < max; i++) { - int last = temp[i - 1] + temp[i - 2]; - if (last >= max) { - break; - } else { - temp[i] = last; - len = i + 1; - } - } - return Arrays.copyOf(temp, len); - } - } - - /** - * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * - * @param max - * @return - */ - public int[] getPrimes(int max) { - - if (max <= 1) { - return new int[0]; - } else { - // 创建临时数组 - int[] temp = new int[max]; - int count = 0; - // 从零开始遍历到max,如果有是素数就加入临时数组。 - for (int i = 0; i < max; i++) { - if (isPrimes(i)) { - temp[i - count] = i; - } else { - count++; - } - } - // max -1 -count是最后一个元素索引 - int len = max - count; - int[] resultArray = Arrays.copyOf(temp, len); - return resultArray; - } - } - - boolean isPrimes(int x) { - if (x <= 1) { - return false; - } else { - for (int i = 2; i < x; i++) { - if (x % i == 0) { - return false; - } - } - } - return true; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * - * @param max - * @return - */ - public int[] getPerfectNumbers(int max) { - if (max < 1) { - return new int[0]; - } else { - ArrayList array = new ArrayList(); - for (int i = 1; i <= max; i++) { - if (isPerfectNumber(i)) { - array.add(i); - } - } - int[] result = new int[array.size()]; - Iterator it = array.iterator(); - int index = 0; - while (it.hasNext()) { - result[index] = ((Integer) it.next()).intValue(); - index++; - } - return result; - } - } - - boolean isPerfectNumber(int x) { - if (x < 1) { - return false; - } else { - int count = 0; - for (int i = 1; i < x; i++) { - if (x % i == 0) { - count += i; - } - } - if (x == count) { - return true; - } - return false; - } - } - - /** - * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" - * - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator) { - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < array.length; i++) { - if(i == array.length-1){ - sb.append(String.valueOf(array[i])); - break; - } - sb.append(String.valueOf(array[i])+seperator); - } - String result = sb.toString(); - return result; - } - -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java deleted file mode 100644 index a0dd465d25..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.coderising.litestruts; - -import java.io.File; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.dom4j.Document; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - - - - //读取配置文件 - try { - Document doc =new SAXReader().read(new File("./src/com/coderising/litestruts/struts.xml")); - //获取XML中的action标签 - Element loginName = (Element)doc.selectSingleNode("//action[1]"); - Element logoutName = (Element)doc.selectSingleNode("//action[2]"); - //判断action的name属性内容 - if(actionName.equals(loginName.attributeValue("name"))){ - //传入的actionName内容为login则进行login操作 - - //获取class路径 - String s = loginName.attributeValue("class"); - Class c = Class.forName(s); - Constructor con = c.getConstructor(); - //实例化LoginAction - LoginAction login =(LoginAction) con.newInstance(); - - //通过parameters获取name,password - String executeName = parameters.get("name"); - String executePassword = parameters.get("password"); - - //获取 setter 方法,并调用 - Method m = c.getMethod("setName", String.class); - m.invoke(login, executeName); - m =c.getMethod("setPassword", String.class); - m.invoke(login, executePassword); - - //调用LoginAction 的exectue 方法 exectueResult值为:帐号密码正确返回success,反之为fail - m = c.getMethod("execute"); - String exectueResult = (String)m.invoke(login); - - //获取 getMessage 方法 - m = c.getMethod("getMessage"); - String message =(String) m.invoke(login); - m = c.getMethod("getName"); - String name =(String) m.invoke(login); - m = c.getMethod("getPassword"); - String password =(String) m.invoke(login); - - //创建HashMap,将登录操作返回的3个变量放到Map - HashMap hm = new HashMap(); - hm.put("message", message); - hm.put("name", name); - hm.put("password", password); - - //创建View对象 - View view = new View(); - view.setParameters(hm); - - //读取struts.xml中的 配置 - List list =(List) doc.selectNodes("//action[@name = 'login']/result"); - for(Element e : list){ - String resultName = e.attributeValue("name"); - //exectue返回值比对XML下result 对应的值返回对应的jsp - if(resultName.equals(exectueResult)){ - //将对应的jsp 放到view对象中 - view.setJsp(e.getText()); - } - } - return view; - - }else if(actionName.equals(logoutName.attributeValue("name"))){ - //actionName是logout则进行logout操作 - } - - } catch (Exception e) { - // TODO: handle exception - e.printStackTrace(); - } - - return null; - } - -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml deleted file mode 100644 index a7cb57e188..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 951b1ce0e9..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - int len = size + 1; - // жlistijǷ - if (len > elementData.length) { - // - Object[] newElemDate = new Object[elementData.length + 1]; - // ƾԪص - System.arraycopy(elementData, 0, newElemDate, 0, elementData.length); - elementData = newElemDate; - } - elementData[size] = o; - size++; - } - - public void add(int index, Object o) { - // ǷԽ - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - // Ԫصĩβֱӵadd - if (index == size) { - add(o); - } else { - // - Object[] newElemData = new Object[elementData.length + 1]; - // indexǰԪص - System.arraycopy(elementData, 0, newElemData, 0, index); - newElemData[index] = o; - // index ԺԪص - System.arraycopy(elementData, index, newElemData, index + 1, size - index); - - elementData = newElemData; - size++; - } - } - - public Object get(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - return elementData[index]; - } - - public Object remove(int index) { - if (index >= size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - Object removeElement = elementData[index]; - //һԪصֵҪ - if(index != (size-1)){ - // - Object[] newElemData = new Object[elementData.length]; - // indexǰԪص - System.arraycopy(elementData, 0, newElemData, 0, index); - // index ԺԪص - System.arraycopy(elementData, index+1, newElemData, index, size - index -1); - } - //һԪصֱֵӼlist - size--; - return removeElement; - } - - public int size() { - return size; - } - - public Iterator iterator() { - return new MyIterator(this); - } - - private class MyIterator implements Iterator { - private int poi = -1; - private ArrayList array = null; - - private MyIterator(ArrayList array) { - this.array = array; - } - - @Override - public boolean hasNext() { - return (poi + 1) < array.size; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - poi++; - if (poi >= array.size) { - poi--; - throw new IndexOutOfBoundsException(); - } - - return array.get(poi); - } - - @Override - public Object remove() { - // TODO Auto-generated method stub - if (poi < 0) { - throw new NoSuchElementException(); - } - Object val = array.remove(poi); - poi--; - return val; - } - - } -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index b3a1ee65b6..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o) { - // жϵǰڵԪ - if (data == null) { - setData(o); - } else { - Integer i = (Integer) o; - // ǰڵжҽڵ - if (i.compareTo((Integer) data) == -1) { - if(right == null) - right = new BinaryTreeNode(); - return right.insert(i); - } else if (i.compareTo((Integer) data) == 1) { - if(left == null) - left = new BinaryTreeNode(); - return left.insert(i); - } - return null; - } - return null; - } - -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java deleted file mode 100644 index f5cf74673d..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - public Object remove(); -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java deleted file mode 100644 index d8f759fdcb..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,170 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - private Node head; - private int size; - - public void add(Object o) { - // жͷǷ - if (head == null) { - head = new Node(o, null); - } else { - Node newNode = head; - while (newNode.next != null) { - newNode = newNode.next; - } - newNode.next = new Node(o, null); - } - size++; - } - - public void add(int index, Object o) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - Node node = head; - if (index != 0) { - // ǵһֵҵֵǰһڵ - for (int i = 1; i < index; i++) { - node = node.next; - } - Node newNode = new Node(o, node.next); - node.next = newNode; - size++; - } else { - // һֵͽͷڵָ - Node newNode = new Node(o, head); - head = newNode; - size++; - } - } - - public Object get(int index) { - indexCheck(index); - Node node = head; - for (int i = 1; i <= index; i++) { - node = node.next; - } - return node.data; - } - - public Object remove(int index) { - indexCheck(index); - - Node node = head; - Node removeNode; - if (index == 0) { - //ɾһڵͰͷڵָԭĵڶڵ - removeNode = head; - head = head.next; - } else { - //ҵֵǰһڵ - for (int i = 1; i < index; i++) { - node = node.next; - } - removeNode = node.next; - //ǰһڵָ룬ָɾڵָĽڵ - node.next = removeNode.next; - } - size--; - return removeNode.data; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - Node newNode = new Node(o, head.next); - head.next = newNode; - size++; - } - - public void addLast(Object o) { - add(o); - } - - public Object removeFirst() { - //ûԪؾ쳣 - if (size <= 0) { - throw new IndexOutOfBoundsException("size:" + size); - } - Object val = head.data; - head = head.next; - size--; - return val; - } - - public Object removeLast() { - if (size <= 0) { - throw new IndexOutOfBoundsException("size:" + size); - } - Node node = head; - while (node.next != null) { - node = node.next; - } - Object val = node.data; - node = null; - size--; - return val; - } - - public Iterator iterator() { - return new MyIterator(this); - } - - private class MyIterator implements Iterator{ - private int poi = -1; - private LinkedList list ; - private MyIterator(LinkedList list) { - this.list= list; - } - @Override - public boolean hasNext() { - // TODO Auto-generated method stub - return (poi + 1) < list.size; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - poi++; - if (poi >= list.size) { - poi--; - throw new IndexOutOfBoundsException(); - } - - return list.get(poi); - } - - @Override - public Object remove() { - // TODO Auto-generated method stub - if (poi < 0) { - throw new NoSuchElementException(); - } - Object val = list.removeLast(); - poi--; - return val; - } - - } - - private void indexCheck(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index:" + index + "size:" + size); - } - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java deleted file mode 100644 index ea25224bd2..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class Queue { - private int size; - private LinkedList list = new LinkedList(); - - public void enQueue(Object o){ - list.addLast(o);; - size++; - } - - public Object deQueue(){ - if(size<=0){ - throw new NoSuchElementException(); - } - Object val = list.removeFirst(); - size--; - return val; - } - - public boolean isEmpty(){ - boolean flag = false; - if(size >= 0){ - flag = true; - } - return flag; - } - - public int size(){ - return size; - } - -} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java deleted file mode 100644 index 9a28ee4d36..0000000000 --- a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size; - - public void push(Object o){ - elementData.add(o); - size++; - } - - public Object pop(){ - if(size<=0){ - throw new NoSuchElementException(); - } - int len = size-1; - Object val = elementData.remove(len); - size--; - return val; - } - - public Object peek(){ - if(size<=0){ - throw new NoSuchElementException(); - } - int len = size-1; - return elementData.get(len); - } - public boolean isEmpty(){ - boolean flag = false; - if(size >= 0){ - flag = true; - } - return flag; - } - public int size(){ - return size; - } -} diff --git a/group15/1515_337959725/.classpath b/group15/1515_337959725/.classpath index 6a4228528e..2d7497573f 100644 --- a/group15/1515_337959725/.classpath +++ b/group15/1515_337959725/.classpath @@ -1,7 +1,7 @@ - + diff --git a/group15/1515_337959725/.project b/group15/1515_337959725/.project index 6730933705..b6d8ce6204 100644 --- a/group15/1515_337959725/.project +++ b/group15/1515_337959725/.project @@ -1,6 +1,6 @@ - coding0305 + coding2017 diff --git a/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs b/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 060c5ee3d2..0000000000 --- a/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.5 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.5 diff --git a/group15/1515_337959725/src/com/coderising/download/DownloadThread.java b/group15/1515_337959725/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..9d24d7d020 --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,27 @@ +package com.coderising.download; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + try { + byte[] read = conn.read(startPos, endPos); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/group15/1515_337959725/src/com/coderising/download/FileDownloader.java b/group15/1515_337959725/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..b892cea185 --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,77 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // ʵĴ룬 ע⣺ Ҫö߳ʵ + // ӿ, Ҫд⼸ӿڵʵִ + // (1) ConnectionManager , ԴһӣͨConnectionԶȡеһΣstartPos, endPosָ + // (2) DownloadListener, Ƕ߳أ Ŀͻ˲֪ʲôʱҪʵֵ + // ̶ִ߳Ժ listenernotifiedFinished ͻ˾յ֪ͨ + // ʵ˼· + // 1. ҪConnectionManageropenӣ ȻͨConnection.getContentLengthļij + // 2. 3߳أ עÿ߳ҪȵConnectionManageropen + // Ȼread readжȡļĿʼλúͽλõIJ ֵbyte[] + // 3. byteд뵽ļ + // 4. е̶߳Ժ ҪlistenernotifiedFinished + + // Ĵʾ룬 Ҳ˵ֻһ̣߳ Ҫɶ̵߳ġ + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + int one=length/3; + + new DownloadThread(conn,0,one).start(); + new DownloadThread(conn,one+1,one*2+1).start(); + new DownloadThread(conn,(one+1)*2,length).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group15/1515_337959725/src/com/coderising/download/FileDownloaderTest.java b/group15/1515_337959725/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..e3c94fd679 --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/C:/Users/lyz/Desktop"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // ȴ߳سִ + while (!downloadFinished) { + try { + System.out.println("ûɣ"); + //5 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("ɣ"); + + + + } + +} diff --git a/group15/1515_337959725/src/com/coderising/download/api/Connection.java b/group15/1515_337959725/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..6a56145ddb --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/api/Connection.java @@ -0,0 +1,24 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * ʼͽλã ȡݣ ֵֽ + * @param startPos ʼλã 0ʼ + * @param endPos λ + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * õݵij + * @return + */ + public int getContentLength(); + + /** + * ر + */ + public void close(); +} + diff --git a/group15/1515_337959725/src/com/coderising/download/api/ConnectionException.java b/group15/1515_337959725/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group15/1515_337959725/src/com/coderising/download/api/ConnectionManager.java b/group15/1515_337959725/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..2fcad26a6b --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,11 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * һurl , һ + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} + diff --git a/group15/1515_337959725/src/com/coderising/download/api/DownloadListener.java b/group15/1515_337959725/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1515_337959725/src/com/coderising/download/impl/ConnectionImpl.java b/group15/1515_337959725/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..bf59420685 --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,48 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URL url; + + + public ConnectionImpl(URL url) { + this.url = url; + } + + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + byte[] b=new byte[1024*1024*10]; + InputStream is = url.openConnection().getInputStream(); + is.skip(startPos); + while((is.read(b,0, endPos-startPos))!=-1); + return b; + } + + @Override + public int getContentLength() { + int length=0; + try { + byte[] b=new byte[1024*1024*10]; + InputStream is = url.openConnection().getInputStream(); + while((length = is.read(b, 0, 1024*1024*10))!=-1); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return length; + } + + @Override + public void close() { + + + } + +} diff --git a/group15/1515_337959725/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group15/1515_337959725/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..89ae0edb72 --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,31 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + URL urll = null; + try { + urll=new URL(url); +// URLConnection urlConnection = urll.openConnection(); + + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return new ConnectionImpl(urll); + } + +} diff --git a/group15/1515_337959725/src/com/coding/basic/SingleLinkedList.java b/group15/1515_337959725/src/com/coding/basic/SingleLinkedList.java new file mode 100644 index 0000000000..5f456856f7 --- /dev/null +++ b/group15/1515_337959725/src/com/coding/basic/SingleLinkedList.java @@ -0,0 +1,252 @@ +package com.coding.basic; + +public class SingleLinkedList{ + + //̬ڲʾĽڵ + private static class Node{ + public T date; // + Node next; //ָ + + public Node(T d){ + date = d; + next = null; + } + } + + private int theSize; + private Node head; + + public SingleLinkedList() + { + clear(); + } + // + public void clear(){ + theSize = 0; + head = null; + } + + //С + public int size(){ + return theSize; + } + + //ӽ + public void add(T x){ + Node newNode = new Node(x); + if(head == null){ + head = newNode ; + }else { + Node pNode = head; + while(pNode.next!=null){ + pNode = pNode.next; + } + pNode.next = newNode; + } + theSize++; + } + + //ڵ + public void add(int index ,T x){ + checkRange(index); + Node pNode = getNode(index); + Node newNode = new Node(x); + newNode.next = pNode.next; + pNode.next = newNode; + theSize++; + } + + //ͷڵ + public void addFirst(T x){ + Node newNode = new Node(x); + newNode.next = head; + head =newNode; + theSize++; + } + + //indexǷԽ + public void checkRange(int index){ + if (index<0 || index > size()) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size(); + } + + //ȡڵ + public T get(int index){ + Node pNode = getNode(index); + return pNode.date; + } + + //ȡڵ + public Node getNode(int index){ + checkRange(index); + Node pNode = head; + for(int i=0;i pNode = getNode(index); + T t=pNode.date; + Node temp = head; + for(int i=0;i7->10 , úΪ 10->7->3 + */ + public void reverse(){ + T t; + for(int i=0;i node1 = getNode(i); + Node node2 = getNode(theSize-1-i); + node1.date = node2.date; + node2.date=t; + } + } + + /** + * ɾһǰ벿 + * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + + */ + public void removeFirstHalf(){ + int count=theSize/2; + for(int i=0;i101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * صĽӦ[101,301,401,601] + * @param list + */ + public int[] getElements(SingleLinkedList list){ + int a; + int length=0; + int[] b=new int[theSize]; + for(int i=0;imin||a c=new SingleLinkedList(); + for(int i=0;i parameters) { * */ SAXReader reader = new SAXReader(); try { - Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); //re:创建一个java类来接收 Element root = document.getRootElement(); Iterator iter = root.elementIterator(); while(iter.hasNext()){ diff --git a/group15/1517_279137987/src/my/collection/linear/LinkedList.java b/group15/1517_279137987/src/my/collection/linear/LinkedList.java new file mode 100644 index 0000000000..1db8bd1053 --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linear/LinkedList.java @@ -0,0 +1,218 @@ +package my.collection.linear; + +public class LinkedList implements MyList { + + private Node head; + + private int size = 0; + + public void add(Object obj) { + add(this.size, obj); + } + + public void add(int index, Object obj) { + Node curNode = head; + Node addNode = new Node(obj); + if(index == 0){ + addNode.next = head; + head = addNode; + }else{ + for(int i=0; i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + for(int i=size; i>0; i--){ + this.add(get(i-1)); + } + this.removeFirstHalf(); + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + int removeCount = this.size()/2; + for(int i=0; i101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result = new int[list.size()]; + for(int i=0; i min && Integer.parseInt(get(i).toString()) < max){ + remove(i); + i--; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + LinkedList c = new LinkedList(); + for(int i=0; i 0) - throw new IndexOutOfBoundsException(); - } - - /** - * 增加数组容量 - */ - private void kuorong() { - elementData = Arrays.copyOf(elementData, size + 1); - } - - /** - * 添加数据 - * - * @param o - */ - public void add(Object o) { - //扩容 - kuorong(); - //添加数据 - elementData[size++] = o; - } - - /** - * 在指定索引添加数据 - * - * @param index - * @param o - */ - public void add(int index, Object o) { - //扩容 - kuorong(); - //移动数据 - System.arraycopy(elementData, index, elementData, index + 1, size - index); - //添加数据 - elementData[index] = o; - size++; - } - - /** - * 获取指定索引数据 - * - * @param index - * @return - */ - public Object get(int index) { - //检查是否越界 - checkLenght(index); - return elementData[index]; - } - - /** - * 移除指定索引数据 - * - * @param index - * @return - */ - public Object remove(int index) { - //检查是否越界 - checkLenght(index); - Object element = elementData[index]; - //计算移除该元素后,要前移的个数 - int movesize = size - index - 1; - //移动数据 - System.arraycopy(elementData, index + 1, elementData, index, movesize); - //删除末尾元素 - elementData[--size] = null; - return element; - } - - /** - * 返回数量 - * - * @return - */ - public int size() { - return size; - } - - /** - * 获取迭代器 - * - * @return - */ - public Iterator iterator() { - return new ArrayItr(); - } - - //迭代器实现类部类 - private class ArrayItr implements Iterator { - int cursor;//游标 - - @Override - public boolean hasNext() { - return cursor != size; - } - - @Override - public Object next() { - int i = cursor; - if (i > size) throw new NoSuchElementException(); - Object[] newElementData = ArrayList.this.elementData; - if (i > newElementData.length) throw new IndexOutOfBoundsException(); - cursor = i + 1; - return newElementData[i]; - } - } - - /** - * 重写toString 方便打印 - * - * @return - */ - @Override - public String toString() { - Object[] s = Arrays.copyOf(elementData, size); - return Arrays.toString(s); - } -} diff --git a/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index 34d76db083..0000000000 --- a/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.coding.basic; - -/** - * 实现二叉树 - * left总比父节点小 - * right总比父节点大 - */ -public class BinaryTreeNode { - private Node root; - private int size = 0; - - /** - * 插入数据 - * @param data - */ - public void insert(int data) { - final Node newNode = new Node(data); - if (root == null) {//根节点为空 直接插入数据到根节点 - root = newNode; - } else { - Node current = root; - while (true) {//循环判断 - Node parent = current; - if (data < current.data) {//比父节点小 就是left - current = current.left; - //直到left节点不存在 - if (current == null) { - //插入数据 - parent.left = newNode; - return; - } - } else {//比父节点大 也就是right - current = current.right; - //直到right节点不存在 - if (current == null) { - //插入数据 - parent.right = newNode; - return; - } - } - } - } - size++; - } - - - /** - * 返回数量 - * @return - */ - public int size() { - return size; - } - - /** - * 重写toString 方便打印 - * - * @return - */ - @Override - public String toString() { - return "["+midTraverse(root)+"]"; - } - - /** - * 节点内部类 用于保存数据 - */ - private static class Node { - int data; - Node left; - Node right; - - Node(int data) { - this.data = data; - this.left = null; - this.right = null; - } - } - - //先序遍历 - private String preTraverse(Node node) { - if (node == null) - return ""; - else - return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); - } - //中序遍历 - private String midTraverse(Node node) { - if (node == null) - return ""; - else - return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); - } - //后序遍历 - private String posTraverse(Node node) { - if (node == null) - return ""; - else - return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; - } - - private String preJointComma(String str) { - return str == "" ? "" : "," + str; - } -} diff --git a/group15/1521_653895972/src/com/coding/basic/LinkedList.java b/group15/1521_653895972/src/com/coding/basic/LinkedList.java deleted file mode 100644 index f1f942590d..0000000000 --- a/group15/1521_653895972/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,271 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -/** - * Created by wanc on 2017/2/21. - * 实现单向链表集合 - */ -public class LinkedList implements List { - /** - * 首节点 - */ - private Node head; - /** - * 计数 - */ - private int size = 0; - - /** - * 检查是否越界 利用jdk源码的检测方法 - */ - private boolean isElementIndex(int index) { - return index >= 0 && index < size; - } - - /** - * JDK 源码检测方法 - * - * @param index - * @return - */ - private boolean isPositionIndex(int index) { - return index >= 0 && index <= size; - } - - /** - * JDK 源码 错误信息 - * - * @param index - * @return - */ - private String outOfBoundsMsg(int index) { - return "Index: " + index + ", Size: " + size; - } - - /** - * JDK 源码检测方法 - * - * @param index - * @return - */ - private void checkElementIndex(int index) { - if (!isElementIndex(index)) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - - /** - * JDK 源码检测方法 - * - * @param index - * @return - */ - private void checkPositionIndex(int index) { - if (!isPositionIndex(index)) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - - /** - * 获取对应下标的节点 - */ - Node node(int index) { - Node x = head; - for (int i = 0; i < index; i++) - x = x.next; - return x; - } - - /** - * 在末尾添加数据 - * - * @param o - */ - public void add(Object o) { - - if (head == null) - head = new Node(o, null); - else { - final Node lastNode = node(size - 1); - final Node newNode = new Node(o, null); - lastNode.next = newNode; - } - size++; - } - - /** - * 指定位置添加数据 - * - * @param index - * @param o - */ - public void add(int index, Object o) { - checkPositionIndex(index); - if (size == index) - add(o); - else { - final Node prevNode = node(index - 1); - final Node nextNode = prevNode.next; - final Node newNode = new Node(o, nextNode); - prevNode.next = newNode; - size++; - } - } - - /** - * 获取指定索引数据 - * - * @param index - * @return - */ - public Object get(int index) { - return node(index).data; - } - - /** - * 移除指定索引数据 - * - * @param index - * @return - */ - public Object remove(int index) { - checkElementIndex(index); - final Node prevNode = node(index - 1); - final Node x = prevNode.next; - if (index - 1 < 0) { - prevNode.next = null; - head = x; - } else { - final Node nextNode = x.next; - prevNode.next = nextNode; - x.next = null; - } - size--; - return x.data; - } - - /** - * 返回数量 - * - * @return - */ - public int size() { - return size; - } - - /** - * 在链首添加数据 - * - * @return - */ - public void addFirst(Object o) { - final Node h = head; - final Node newNode = new Node(o, h); - head = newNode; - size++; - } - - /** - * 在链尾添加数据 - * - * @return - */ - public void addLast(Object o) { - add(o); - } - - /** - * 移除链首数据 - * - * @return - */ - public Object removeFirst() { - final Node h = head; - if (h == null) - throw new NoSuchElementException(); - final Node newFirst = h.next; - h.next = null; - head = newFirst; - size--; - return h.data; - } - - /** - * 移除链尾数据 - * - * @return - */ - public Object removeLast() { - final Node prev = node(size - 1 - 1); - final Node l = prev.next; - prev.next = null; - l.next = null; - size--; - return l.data; - } - - /** - * 获取迭代器 - * - * @return - */ - public Iterator iterator() { - return new LinkedItr(); - } - - /** - * 迭代器实现内部类 - * - * @return - */ - private class LinkedItr implements Iterator { - int cursor;//游标 - - @Override - public boolean hasNext() { - return cursor != size; - } - - @Override - public Object next() { - int i = cursor; - if (i > size - 1) throw new NoSuchElementException(); - Node current = node(i); - if (current == null) throw new IndexOutOfBoundsException(); - cursor = i + 1; - return current.data; - } - } - - /** - * 节点内部类 用于保存数据 - */ - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - /** - * 重写toString 方便打印 - * - * @return - */ - @Override - public String toString() { - String result = "["; - for (int i = 0; i < size; i++) { - Node n = node(i); - if (i == 0) - result += n.data; - else - result += "," + n.data; - - } - - return result + "]"; - } -} diff --git a/group15/1521_653895972/src/com/coding/basic/Queue.java b/group15/1521_653895972/src/com/coding/basic/Queue.java deleted file mode 100644 index 4add2be9a4..0000000000 --- a/group15/1521_653895972/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.coding.basic; - -/** - * Created by wanc on 2017/2/21. - * 利用LinkedList 实现队列 - */ -public class Queue { - /** - * 利用LinkedList 保存数据 - */ - private LinkedList elementData = new LinkedList(); - - /** - * 入队 - * - * @param o - */ - public void enQueue(Object o) { - elementData.add(o); - } - - /** - * 出队 - * - * @return - */ - public Object deQueue() { - return elementData.removeFirst(); - } - - /** - * 是否为空 - * - * @return - */ - public boolean isEmpty() { - return elementData.size() == 0 ? true : false; - } - - /** - * 返回队列长度 - * - * @return - */ - public int size() { - return elementData.size(); - } - - /** - * 重写toString 方便打印 - * - * @return - */ - @Override - public String toString() { - return "Queue{" + - "elementData=" + elementData + - '}'; - } -} diff --git a/group15/1521_653895972/src/com/coding/basic/Stack.java b/group15/1521_653895972/src/com/coding/basic/Stack.java deleted file mode 100644 index 23c5ba6a7b..0000000000 --- a/group15/1521_653895972/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.coding.basic; -/** - * Created by wanc on 2017/2/21. - * 利用ArrayList 实现栈 - */ -public class Stack { - /** - * 利用ArrayList 保存数据 - */ - private ArrayList elementData = new ArrayList(); - - /** - * 入栈 - * @param o - */ - public void push(Object o) { - elementData.add(o); - } - - /** - * 出栈 - * @return - */ - public Object pop() { - elementData.remove(elementData.size()-1); - return null; - } - - /** - * 返回栈顶数据 - * @return - */ - public Object peek() { - return elementData.get(elementData.size()-1); - } - - /** - * 是否为空 - * @return - */ - public boolean isEmpty() { - return elementData.size()==0?true:false; - } - - /** - * 返回栈长度 - * @return - */ - public int size() { - return elementData.size(); - } - - /** - * 重写toString 方便打印 - * - * @return - */ - @Override - public String toString() { - return "Stack{" + - "elementData=" + elementData + - '}'; - } -} diff --git a/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java b/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java deleted file mode 100644 index 92c581254b..0000000000 --- a/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.coding.coderising.array; - -import org.junit.Test; - -import java.util.Arrays; - -/** - * Created by wanc on 2017/2/28. - */ -public class ArrayUtilTest { - - @Test - public void testReverseArray() throws Exception { - int[] arr = {7, 9, 30, 3}; - SimpleArrayUtil.reverseArray(arr); - System.out.println(Arrays.toString(arr)); - System.out.println("----------------------置换 end-----------------------------"); - } - - - @Test - public void testRemoveZero() throws Exception { - int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; - int newArr[] = SimpleArrayUtil.removeZero(oldArr); - System.out.println(Arrays.toString(newArr)); - System.out.println("----------------------去零 end-----------------------------"); - } - - @Test - public void testMerge() throws Exception { - int arr1[] = {3, 5, 7, 8}; - int arr2[] = {4, 5, 6, 7}; - int arr3[] = SimpleArrayUtil.merge(arr1, arr2); - System.out.println(Arrays.toString(arr3)); - System.out.println("----------------------merge end-----------------------------"); - } - - @Test - public void testGrow() throws Exception { - int arr1[] = {3, 5, 7, 8}; - int[] newArr = SimpleArrayUtil.grow(arr1, 3); - System.out.println(Arrays.toString(newArr)); - System.out.println("----------------------扩展 end-----------------------------"); - } - - @Test - public void testFibonacci() throws Exception { - int[] arr = SimpleArrayUtil.fibonacci(15); - System.out.println(Arrays.toString(arr)); - System.out.println("----------------------斐波那契 end-----------------------------"); - } - - @Test - public void testGetPrimes() throws Exception { - int[] arr = SimpleArrayUtil.getPrimes(23); - System.out.println(Arrays.toString(arr)); - System.out.println("----------------------素数 end-----------------------------"); - } - - @Test - public void testGetPerfectNumbers() throws Exception { - int[] newArr = SimpleArrayUtil.getPerfectNumbers(50); - System.out.println(Arrays.toString(newArr)); - System.out.println("----------------------完数 end-----------------------------"); - } - - @Test - public void testJoin() throws Exception { - int arr1[] = {3, 5, 7, 8}; - System.out.println(SimpleArrayUtil.join(arr1, "-")); - System.out.println("----------------------Join end-----------------------------"); - } -} \ No newline at end of file diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java deleted file mode 100644 index 273741bbfb..0000000000 --- a/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.coding.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * - * @author wanc - */ -public class LoginAction { - private String name; - private String password; - - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute() { - if ("test".equals(name) && "1234".equals(password)) { - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getMessage() { - return this.message; - } -} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java deleted file mode 100644 index ed20c588fe..0000000000 --- a/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.coding.coderising.litestruts; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - - -public class Struts { - - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - //解析xml - Map xmlInfo = parsersXml(actionName); - //获取类名 - String allName = (String) xmlInfo.get("className"); - try { - //加载类 - Class cls = Class.forName(allName); - //实例化 - Object object = cls.newInstance(); - for (String key : parameters.keySet()) { - //拼接set方法名 - String setName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); - Method setMethod = cls.getDeclaredMethod(setName, String.class); - //放射执行set方法 - setMethod.invoke(object, parameters.get(key)); - } - //执行execute方法 - Method exectue = cls.getDeclaredMethod("execute", null); - String reslut = (String) exectue.invoke(object, null); - //执行getMessage方法 - Method getMessage = cls.getDeclaredMethod("getMessage", null); - String message = (String) getMessage.invoke(object, null); - //获取xml配置想返回结果 - String jsp = (String) xmlInfo.get(reslut); - //组装view - Map map = new HashMap(); - map.put("message", message); - View view = new View(); - view.setJsp(jsp); - view.setParameters(map); - return view; - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return null; - } - - - private static Map parsersXml(String actionName) { - File file = new File(Struts.class.getResource("").getPath() + "/struts.xml"); - //1.获取DOM解析器工厂 - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - HashMap map = new HashMap<>(); - try { - //2.获取解析器 - DocumentBuilder builder = factory.newDocumentBuilder(); - //3.加载xml文档 - Document document = builder.parse(file); - //4.获取指定action的action集合 - NodeList actionlist = document.getElementsByTagName("action"); - //5.如果actionName重复 只去第一个 - for (int j = 0; j < actionlist.getLength(); j++) { - Element actionElement = (Element) actionlist.item(j); - if (actionElement.getAttribute("name") != null && actionElement.getAttribute("name").equalsIgnoreCase(actionName)) { - //6.获取 类全限定名 - String className = actionElement.getAttribute("class"); - map.put("className", className); - //7.获取 action子节点 - NodeList childList = actionElement.getChildNodes(); - int lenght = childList.getLength(); - for (int i = 0; i < lenght; i++) { - Node node = childList.item(i); - //判断为element节点 排除空格 换行 - if (node.getNodeType() == Node.ELEMENT_NODE) { - Element child = (Element) node; - switch (child.getAttribute("name")) { - case "success": - map.put("success", child.getTextContent()); - break; - case "fail": - map.put("fail", child.getTextContent()); - break; - case "error": - map.put("error", child.getTextContent()); - break; - } - } - } - break; - } - - } - - } catch (ParserConfigurationException e) { - e.printStackTrace(); - } catch (SAXException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return map; - } -} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java deleted file mode 100644 index 8699cc3eca..0000000000 --- a/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coding.coderising.litestruts; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java deleted file mode 100644 index 2c909058cb..0000000000 --- a/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coding.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - - private Map parameters; - - public String getJsp() { - return jsp; - } - - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - - public Map getParameters() { - return parameters; - } - - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml b/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml deleted file mode 100644 index bec462fdf5..0000000000 --- a/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/group15/1521_653895972/src/task1/basic/WArrayList.java b/group15/1521_653895972/src/task1/basic/WArrayList.java new file mode 100644 index 0000000000..d1ba8c5d63 --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WArrayList.java @@ -0,0 +1,165 @@ +package task1.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.Objects; + +/** + * Created by wanc on 2017/2/21. + * 实现ArrayList + */ +public class WArrayList implements WList { + /** + * 实例化空数组 不用每次都new + */ + private static final Object[] Empty_elementData = {}; + /** + * 计数 + */ + private int size = 0; + /** + * 数据存放 + */ + private Object[] elementData = new Object[100]; + + public WArrayList() { + this.elementData = Empty_elementData; + } + + public WArrayList(Object[] c) { + size = c.length; + this.elementData = c; + } + + /** + * 检查是否越界 + */ + private void checkLenght(int index) { + if (index - size > 0) + throw new IndexOutOfBoundsException(); + } + + /** + * 增加数组容量 + */ + private void kuorong() { + elementData = Arrays.copyOf(elementData, size + 1); + } + + /** + * 添加数据 + * + * @param o + */ + public void add(Object o) { + //扩容 + kuorong(); + //添加数据 + elementData[size++] = o; + } + + /** + * 在指定索引添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + //扩容 + kuorong(); + //移动数据 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + //添加数据 + elementData[index] = o; + size++; + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + //检查是否越界 + checkLenght(index); + return elementData[index]; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + //检查是否越界 + checkLenght(index); + Object element = elementData[index]; + //计算移除该元素后,要前移的个数 + int movesize = size - index - 1; + //移动数据 + System.arraycopy(elementData, index + 1, elementData, index, movesize); + //删除末尾元素 + elementData[--size] = null; + return element; + } + + /** + * 返回数量 + * + * @return + */ + public int size() { + return size; + } + + public boolean contaions(Object o) { + if (size==0)return false; + for (int i = 0; i < size; i++) { + if (Objects.equals(o, elementData[i])) + return true; + } + return false; + } + + /** + * 获取迭代器 + * + * @return + */ + public WIterator iterator() { + return new ArrayItr(); + } + + //迭代器实现类部类 + private class ArrayItr implements WIterator { + int cursor;//游标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i > size) throw new NoSuchElementException(); + Object[] newElementData = WArrayList.this.elementData; + if (i > newElementData.length) throw new IndexOutOfBoundsException(); + cursor = i + 1; + return newElementData[i]; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + Object[] s = Arrays.copyOf(elementData, size); + return Arrays.toString(s); + } +} diff --git a/group15/1521_653895972/src/task1/basic/WBinaryTreeNode.java b/group15/1521_653895972/src/task1/basic/WBinaryTreeNode.java new file mode 100644 index 0000000000..207ccb7828 --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WBinaryTreeNode.java @@ -0,0 +1,105 @@ +package task1.basic; + +/** + * 实现二叉树 + * left总比父节点小 + * right总比父节点大 + */ +public class WBinaryTreeNode { + private Node root; + private int size = 0; + + /** + * 插入数据 + * @param data + */ + public void insert(int data) { + final Node newNode = new Node(data); + if (root == null) {//根节点为空 直接插入数据到根节点 + root = newNode; + } else { + Node current = root; + while (true) {//循环判断 + Node parent = current; + if (data < current.data) {//比父节点小 就是left + current = current.left; + //直到left节点不存在 + if (current == null) { + //插入数据 + parent.left = newNode; + return; + } + } else {//比父节点大 也就是right + current = current.right; + //直到right节点不存在 + if (current == null) { + //插入数据 + parent.right = newNode; + return; + } + } + } + } + size++; + } + + + /** + * 返回数量 + * @return + */ + public int size() { + return size; + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "["+midTraverse(root)+"]"; + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + int data; + Node left; + Node right; + + Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } + + //先序遍历 + private String preTraverse(Node node) { + if (node == null) + return ""; + else + return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); + } + //中序遍历 + private String midTraverse(Node node) { + if (node == null) + return ""; + else + return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); + } + //后序遍历 + private String posTraverse(Node node) { + if (node == null) + return ""; + else + return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; + } + + private String preJointComma(String str) { + return str == "" ? "" : "," + str; + } +} diff --git a/group15/1521_653895972/src/task1/basic/WIterator.java b/group15/1521_653895972/src/task1/basic/WIterator.java new file mode 100644 index 0000000000..0f6b64a99f --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WIterator.java @@ -0,0 +1,7 @@ +package task1.basic; + +public interface WIterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group15/1521_653895972/src/task1/basic/WLinkedList.java b/group15/1521_653895972/src/task1/basic/WLinkedList.java new file mode 100644 index 0000000000..9afe3add71 --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WLinkedList.java @@ -0,0 +1,423 @@ +package task1.basic; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; +import java.util.Objects; + +/** + * Created by wanc on 2017/2/21. + * 实现单向链表集合 + */ +public class WLinkedList implements WList { + /** + * 首节点 + */ + private Node head; + /** + * 计数 + */ + private int size = 0; + + /** + * 检查是否越界 利用jdk源码的检测方法 + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + /** + * JDK 源码 错误信息 + * + * @param index + * @return + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 获取对应下标的节点 + */ + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + /** + * 在末尾添加数据 + * + * @param o + */ + public void add(Object o) { + + if (head == null) + head = new Node(o, null); + else { + final Node lastNode = node(size - 1); + final Node newNode = new Node(o, null); + lastNode.next = newNode; + } + size++; + } + + /** + * 指定位置添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + checkPositionIndex(index); + if (size == index) + add(o); + else { + final Node prevNode = node(index - 1); + final Node nextNode = prevNode.next; + final Node newNode = new Node(o, nextNode); + prevNode.next = newNode; + size++; + } + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + return node(index).data; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + checkElementIndex(index); + final Node prevNode = node(index - 1); + final Node x = prevNode.next; + if (index - 1 < 0) { + prevNode.next = null; + head = x; + } else { + final Node nextNode = x.next; + prevNode.next = nextNode; + x.next = null; + } + size--; + return x.data; + } + public Object remove(Object element){ + Node x=head; + for (int i=0;i (size - 1)) throw new NoSuchElementException(); + Node current = node(i); + if (current == null) throw new IndexOutOfBoundsException(); + delCursor = i; + cursor = i + 1; +// System.out.println("i="+i+"-"+current.data); + return current.data; + } + + @Override + public void remove() { + if (delCursor < 0) { + throw new IllegalStateException(); + } + try { + WLinkedList.this.remove(delCursor); + if (cursor > 0) + cursor--; + delCursor = -1; + } catch (IndexOutOfBoundsException e) { + throw new ConcurrentModificationException(); + } + + } + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + String result = "["; + for (int i = 0; i < size; i++) { + Node n = node(i); + if (i == 0) + result += n.data; + else + result += "," + n.data; + + } + + return result + "]"; + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (head == null) return; + Node[] nodes = new Node[size]; + Node x = head; + for (int i = 0; i < size; i++) { + nodes[i] = x; + x = x.next; + } + + head = nodes[nodes.length - 1]; + Node tmp = head; + for (int j = nodes.length - 2; j >= 0; j--) { + Node c = nodes[j]; + tmp.next = c; + tmp = c; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int len = size / 2; + remove(0, len); + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + checkElementIndex(i); + checkElementIndex(i + length - 1); + if (0 == length) return; + int a = i - 1; + Node p = node(a);//前一个 + Node f = p.next;//删除第一个 + Node l = node(i + length - 1);//删除最后一个 + Node h = l.next;//后一个 + //去掉引用 等待GC回收 + Node tmp = f; + while (tmp != l) { + Node n = tmp.next; + tmp.next = null; + tmp = n; + } + l.next = null; + + if (0 == i) + head = h; + else + p.next = h; + size -= length; + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(WLinkedList list) { + if (list == null) return null; + int[] arr = new int[list.size]; + WIterator itr = list.iterator(); + int i = 0; + while (itr.hasNext()) { + arr[i] = (int) node((int) itr.next()).data; + i++; + } + return arr; + } + + interface ListWIterator extends WIterator { + void remove(); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(WLinkedList list) { + if (list != null && list.size > 0) { + WIterator itr = list.iterator(); + while (itr.hasNext()) { + ListWIterator sourItr = listIterator(); + Object value = itr.next(); + while (sourItr.hasNext()) { + Object souValue = sourItr.next(); +// System.out.println(value+"-"+souValue); + if (value.equals(souValue)) { +// System.out.println(value+"-"+sourItr.next()); + sourItr.remove(); +// System.out.println("remove"); + } + } +// System.out.println("---------------------------------"); + } + } + } + + +} diff --git a/group15/1521_653895972/src/task1/basic/WList.java b/group15/1521_653895972/src/task1/basic/WList.java new file mode 100644 index 0000000000..f7534934d2 --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WList.java @@ -0,0 +1,9 @@ +package task1.basic; + +public interface WList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group15/1521_653895972/src/task1/basic/WQueue.java b/group15/1521_653895972/src/task1/basic/WQueue.java new file mode 100644 index 0000000000..af3fdb8302 --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WQueue.java @@ -0,0 +1,60 @@ +package task1.basic; + +/** + * Created by wanc on 2017/2/21. + * 利用LinkedList 实现队列 + */ +public class WQueue { + /** + * 利用LinkedList 保存数据 + */ + private WLinkedList elementData = new WLinkedList(); + + /** + * 入队 + * + * @param o + */ + public void enQueue(Object o) { + elementData.add(o); + } + + /** + * 出队 + * + * @return + */ + public Object deQueue() { + return elementData.removeFirst(); + } + + /** + * 是否为空 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + /** + * 返回队列长度 + * + * @return + */ + public int size() { + return elementData.size(); + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "Queue{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/src/task1/basic/WStack.java b/group15/1521_653895972/src/task1/basic/WStack.java new file mode 100644 index 0000000000..a6bb09ed77 --- /dev/null +++ b/group15/1521_653895972/src/task1/basic/WStack.java @@ -0,0 +1,64 @@ +package task1.basic; +/** + * Created by wanc on 2017/2/21. + * 利用ArrayList 实现栈 + */ +public class WStack { + /** + * 利用ArrayList 保存数据 + */ + private WArrayList elementData = new WArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o) { + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop() { + elementData.remove(elementData.size()-1); + return null; + } + + /** + * 返回栈顶数据 + * @return + */ + public Object peek() { + return elementData.get(elementData.size()-1); + } + + /** + * 是否为空 + * @return + */ + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + /** + * 返回栈长度 + * @return + */ + public int size() { + return elementData.size(); + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "Stack{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BasicTest.java b/group15/1521_653895972/src/task1/test/BasicTest.java similarity index 92% rename from group15/1521_653895972/src/com/coding/basic/BasicTest.java rename to group15/1521_653895972/src/task1/test/BasicTest.java index a181087104..f5143acc7f 100644 --- a/group15/1521_653895972/src/com/coding/basic/BasicTest.java +++ b/group15/1521_653895972/src/task1/test/BasicTest.java @@ -1,7 +1,8 @@ -package com.coding.basic; +package task1.test; import org.junit.Assert; import org.junit.Test; +import task1.basic.*; /** * Created by wanc on 2017/2/21. @@ -11,16 +12,16 @@ public class BasicTest { @Test public void test() { //测试 - testArrayList(); +// testArrayList(); testLinkedList(); - testBinaryTreeNode(); - testStack(); - testQueue(); +// testBinaryTreeNode(); +// testStack(); +// testQueue(); } public void testQueue(){ - Queue queue = new Queue(); + WQueue queue = new WQueue(); queue.enQueue("S"); queue.enQueue("Y"); queue.enQueue(5); @@ -32,7 +33,7 @@ public void testQueue(){ System.out.println(queue); } public void testStack(){ - Stack stack = new Stack(); + WStack stack = new WStack(); stack.push("S"); stack.push("Y"); stack.push(5); @@ -46,7 +47,7 @@ public void testStack(){ public void testBinaryTreeNode(){ System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); System.out.println("new 一个实例"); - BinaryTreeNode root = new BinaryTreeNode(); + WBinaryTreeNode root = new WBinaryTreeNode(); root.insert(5); root.insert(6); root.insert(9); @@ -61,7 +62,7 @@ public void testLinkedList() { System.out.println("-------------------LinkedList 测试开始-------------------"); System.out.println("new 一个实例"); - LinkedList list = new LinkedList(); + WLinkedList list = new WLinkedList(); System.out.println("添加元素----A"); list.add("A"); @@ -125,7 +126,7 @@ public void testLinkedList() { System.out.println(); System.out.println("迭代器输出:"); - Iterator i = list.iterator(); + WIterator i = list.iterator(); while (i.hasNext()){ System.out.print(i.next()+" "); } @@ -139,7 +140,7 @@ public void testArrayList() { System.out.println("-------------------ArrayList 测试开始-------------------"); System.out.println("new 一个实例"); - ArrayList list = new ArrayList(); + WArrayList list = new WArrayList(); System.out.println("添加元素 A"); list.add("A"); @@ -174,7 +175,7 @@ public void testArrayList() { System.out.println("输出:"+list); System.out.println("数量:"+list.size()); - Iterator i = list.iterator(); + WIterator i = list.iterator(); System.out.print("迭代器输出:"); while (i.hasNext()){ System.out.print(i.next()+" "); diff --git a/group15/1521_653895972/src/task1/test/WLinkedListTest.java b/group15/1521_653895972/src/task1/test/WLinkedListTest.java new file mode 100644 index 0000000000..33fddb06e3 --- /dev/null +++ b/group15/1521_653895972/src/task1/test/WLinkedListTest.java @@ -0,0 +1,98 @@ +package task1.test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import task3.basic.WLinkedList; + +import java.util.Arrays; + +/** + * Created by wanc on 2017/3/7. + * 3月5日 布置的数据结构作业测试 + */ +public class WLinkedListTest { + WLinkedList list; + + @Before + public void setUp() throws Exception { + list = new WLinkedList(); + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + list.add(701); + list.add(301); + System.out.println(list); + } + + @After + public void tearDown() throws Exception { + + + } + + + @Test + public void testReverse() throws Exception { + list.reverse(); + System.out.println(list); + } + + @Test + public void testRemoveFirstHalf() throws Exception { + list.removeFirstHalf(); + System.out.println(list); + } + + @Test + public void testRemove() throws Exception { + list.remove(3,4); + System.out.println(list); + } + + @Test + public void testGetElements() throws Exception { + WLinkedList lst = new WLinkedList(); + lst.add(1); + lst.add(3); + lst.add(4); + lst.add(6); + int[] elements = list.getElements(lst); + System.out.println(Arrays.toString(elements)); + } + + @Test + public void testSubtract() throws Exception { + WLinkedList lst = new WLinkedList(); + lst.add(101); + lst.add(301); + lst.add(401); + lst.add(601); + list.subtract(lst); + System.out.println(list); + } + + @Test + public void testRemoveDuplicateValues() throws Exception { + list.add(301); + list.add(401); + System.out.println(list); + list.removeDuplicateValues(); + System.out.println(list); + } + + @Test + public void testRemoveRange() throws Exception { + + } + + @Test + public void testIntersection() throws Exception { + + } +} \ No newline at end of file diff --git a/group15/1521_653895972/src/task2/array/ArrayUtilTest.java b/group15/1521_653895972/src/task2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..8b14cf6516 --- /dev/null +++ b/group15/1521_653895972/src/task2/array/ArrayUtilTest.java @@ -0,0 +1,73 @@ +package task2.array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by wanc on 2017/2/28. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() throws Exception { + int[] arr = {7, 9, 30, 3}; + SimpleArrayUtil.reverseArray(arr); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------置换 end-----------------------------"); + } + + + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int newArr[] = SimpleArrayUtil.removeZero(oldArr); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------去零 end-----------------------------"); + } + + @Test + public void testMerge() throws Exception { + int arr1[] = {3, 5, 7, 8}; + int arr2[] = {4, 5, 6, 7}; + int arr3[] = SimpleArrayUtil.merge(arr1, arr2); + System.out.println(Arrays.toString(arr3)); + System.out.println("----------------------merge end-----------------------------"); + } + + @Test + public void testGrow() throws Exception { + int arr1[] = {3, 5, 7, 8}; + int[] newArr = SimpleArrayUtil.grow(arr1, 3); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------扩展 end-----------------------------"); + } + + @Test + public void testFibonacci() throws Exception { + int[] arr = SimpleArrayUtil.fibonacci(15); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------斐波那契 end-----------------------------"); + } + + @Test + public void testGetPrimes() throws Exception { + int[] arr = SimpleArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------素数 end-----------------------------"); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] newArr = SimpleArrayUtil.getPerfectNumbers(50); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------完数 end-----------------------------"); + } + + @Test + public void testJoin() throws Exception { + int arr1[] = {3, 5, 7, 8}; + System.out.println(SimpleArrayUtil.join(arr1, "-")); + System.out.println("----------------------Join end-----------------------------"); + } +} \ No newline at end of file diff --git a/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java b/group15/1521_653895972/src/task2/array/SimpleArrayUtil.java similarity index 99% rename from group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java rename to group15/1521_653895972/src/task2/array/SimpleArrayUtil.java index 0aa491fb16..fa1b17e7ed 100644 --- a/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java +++ b/group15/1521_653895972/src/task2/array/SimpleArrayUtil.java @@ -1,4 +1,4 @@ -package com.coding.coderising.array; +package task2.array; public class SimpleArrayUtil { diff --git a/group15/1521_653895972/src/task2/litestruts/LoginAction.java b/group15/1521_653895972/src/task2/litestruts/LoginAction.java new file mode 100644 index 0000000000..3f7baf7842 --- /dev/null +++ b/group15/1521_653895972/src/task2/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package task2.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author wanc + */ +public class LoginAction { + private String name; + private String password; + + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group15/1521_653895972/src/task2/litestruts/Struts.java b/group15/1521_653895972/src/task2/litestruts/Struts.java new file mode 100644 index 0000000000..87c9d2a9ca --- /dev/null +++ b/group15/1521_653895972/src/task2/litestruts/Struts.java @@ -0,0 +1,145 @@ +package task2.litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + //解析xml + Map xmlInfo = parsersXml(actionName); + //获取类名 + String allName = (String) xmlInfo.get("className"); + try { + //加载类 + Class cls = Class.forName(allName); + //实例化 + Object object = cls.newInstance(); + for (String key : parameters.keySet()) { + //拼接set方法名 + String setName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Method setMethod = cls.getDeclaredMethod(setName, String.class); + //放射执行set方法 + setMethod.invoke(object, parameters.get(key)); + } + //执行execute方法 + Method exectue = cls.getDeclaredMethod("execute", null); + String reslut = (String) exectue.invoke(object, null); + //执行getMessage方法 + Method getMessage = cls.getDeclaredMethod("getMessage", null); + String message = (String) getMessage.invoke(object, null); + //获取xml配置想返回结果 + String jsp = (String) xmlInfo.get(reslut); + //组装view + Map map = new HashMap(); + map.put("message", message); + View view = new View(); + view.setJsp(jsp); + view.setParameters(map); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + + private static Map parsersXml(String actionName) { + File file = new File(Struts.class.getResource("").getPath() + "/struts.xml"); + //1.获取DOM解析器工厂 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + HashMap map = new HashMap<>(); + try { + //2.获取解析器 + DocumentBuilder builder = factory.newDocumentBuilder(); + //3.加载xml文档 + Document document = builder.parse(file); + //4.获取指定action的action集合 + NodeList actionlist = document.getElementsByTagName("action"); + //5.如果actionName重复 只去第一个 + for (int j = 0; j < actionlist.getLength(); j++) { + Element actionElement = (Element) actionlist.item(j); + if (actionElement.getAttribute("name") != null && actionElement.getAttribute("name").equalsIgnoreCase(actionName)) { + //6.获取 类全限定名 + String className = actionElement.getAttribute("class"); + map.put("className", className); + //7.获取 action子节点 + NodeList childList = actionElement.getChildNodes(); + int lenght = childList.getLength(); + for (int i = 0; i < lenght; i++) { + Node node = childList.item(i); + //判断为element节点 排除空格 换行 + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element child = (Element) node; + switch (child.getAttribute("name")) { + case "success": + map.put("success", child.getTextContent()); + break; + case "fail": + map.put("fail", child.getTextContent()); + break; + case "error": + map.put("error", child.getTextContent()); + break; + } + } + } + break; + } + + } + + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return map; + } +} diff --git a/group15/1521_653895972/src/task2/litestruts/View.java b/group15/1521_653895972/src/task2/litestruts/View.java new file mode 100644 index 0000000000..7663182dcc --- /dev/null +++ b/group15/1521_653895972/src/task2/litestruts/View.java @@ -0,0 +1,27 @@ +package task2.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group15/1521_653895972/src/task2/test/StrutsTest.java b/group15/1521_653895972/src/task2/test/StrutsTest.java new file mode 100644 index 0000000000..4bd28b08d4 --- /dev/null +++ b/group15/1521_653895972/src/task2/test/StrutsTest.java @@ -0,0 +1,45 @@ +package task2.test; + +import task2.litestruts.Struts; +import task2.litestruts.View; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1521_653895972/src/task3/basic/WLinkedList.java b/group15/1521_653895972/src/task3/basic/WLinkedList.java new file mode 100644 index 0000000000..eff5fba7b9 --- /dev/null +++ b/group15/1521_653895972/src/task3/basic/WLinkedList.java @@ -0,0 +1,534 @@ +package task3.basic; + +import task1.basic.WIterator; +import task1.basic.WList; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; +import java.util.Objects; + +/** + * Created by wanc on 2017/2/21. + * 实现单向链表集合 + */ +public class WLinkedList implements WList { + /** + * 首节点 + */ + private Node head; + /** + * 计数 + */ + private int size = 0; + + /** + * 检查是否越界 利用jdk源码的检测方法 + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + /** + * JDK 源码 错误信息 + * + * @param index + * @return + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 获取对应下标的节点 + */ + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + /** + * 在末尾添加数据 + * + * @param o + */ + public void add(Object o) { + + if (head == null) + head = new Node(o, null); + else { + final Node lastNode = node(size - 1); + final Node newNode = new Node(o, null); + lastNode.next = newNode; + } + size++; + } + + /** + * 指定位置添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + checkPositionIndex(index); + if (size == index) + add(o); + else { + final Node prevNode = node(index - 1); + final Node nextNode = prevNode.next; + final Node newNode = new Node(o, nextNode); + prevNode.next = newNode; + size++; + } + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + return node(index).data; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + checkElementIndex(index); + final Node prevNode = node(index - 1); + final Node x = prevNode.next; + if (index - 1 < 0) { + prevNode.next = null; + head = x; + } else { + final Node nextNode = x.next; + prevNode.next = nextNode; + x.next = null; + } + size--; + return x.data; + } + + public Object remove(Object element) { + Node x = head; + for (int i = 0; i < size; i++) { + if (Objects.equals(x.data, element)) + x = x.next; + } + return null; + } + + /** + * 返回数量 + * + * @return + */ + public int size() { + return size; + } + + /** + * 在链首添加数据 + * + * @return + */ + public void addFirst(Object o) { + final Node h = head; + final Node newNode = new Node(o, h); + head = newNode; + size++; + } + + /** + * 在链尾添加数据 + * + * @return + */ + public void addLast(Object o) { + add(o); + } + + /** + * 移除链首数据 + * + * @return + */ + public Object removeFirst() { + final Node h = head; + if (h == null) + throw new NoSuchElementException(); + final Node newFirst = h.next; + h.next = null; + head = newFirst; + size--; + return h.data; + } + + /** + * 移除链尾数据 + * + * @return + */ + public Object removeLast() { + final Node prev = node(size - 1 - 1); + final Node l = prev.next; + prev.next = null; + l.next = null; + size--; + return l.data; + } + + /** + * 获取迭代器 + * + * @return + */ + public WIterator iterator() { + return new LinkedItr(); + } + + public ListWIterator listIterator() { + return new LinkedItr(); + } + + /** + * 迭代器实现内部类 + * + * @return + */ + private class LinkedItr implements ListWIterator { + int cursor = 0;//游标 + int delCursor = -1; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i > (size - 1)) throw new NoSuchElementException(); + Node current = node(i); + if (current == null) throw new IndexOutOfBoundsException(); + delCursor = i; + cursor = i + 1; +// System.out.println("i="+i+"-"+current.data); + return current.data; + } + + @Override + public void remove() { + if (delCursor < 0) { + throw new IllegalStateException(); + } + try { + WLinkedList.this.remove(delCursor); + if (cursor > 0) + cursor--; + delCursor = -1; + } catch (IndexOutOfBoundsException e) { + throw new ConcurrentModificationException(); + } + + } + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + String elementStr = ""; + Node p = head; + while (p != null) { + elementStr += p.data + ","; + p = p.next; + } + + return "WLinkedList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (head == null) return; + Node[] nodes = new Node[size]; + Node x = head; + for (int i = 0; i < size; i++) { + nodes[i] = x; + x = x.next; + } + + head = nodes[nodes.length - 1]; + Node tmp = head; + for (int j = nodes.length - 2; j >= 0; j--) { + Node c = nodes[j]; + tmp.next = c; + tmp = c; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int len = size / 2; + remove(0, len); + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + checkElementIndex(i); + checkElementIndex(i + length - 1); + if (0 == length) return; + int a = i - 1; + Node p = node(a);//前一个 + Node f = p.next;//删除第一个 + Node l = node(i + length - 1);//删除最后一个 + Node h = l.next;//后一个 + //去掉引用 等待GC回收 + Node tmp = f; + while (tmp != l) { + Node n = tmp.next; + tmp.next = null; + tmp = n; + } + l.next = null; + + if (0 == i) + head = h; + else + p.next = h; + size -= length; + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(WLinkedList list) { + if (list == null) return null; + int[] arr = new int[list.size]; + WIterator itr = list.iterator(); + int i = 0; + while (itr.hasNext()) { + arr[i] = (int) node((int) itr.next()).data; + i++; + } + return arr; + } + + interface ListWIterator extends WIterator { + void remove(); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(WLinkedList list) { + if (list != null && list.size > 0) { + WIterator itr = list.iterator(); + while (itr.hasNext()) { + ListWIterator sourItr = listIterator(); + Object value = itr.next(); + while (sourItr.hasNext()) { + Object souValue = sourItr.next(); +// System.out.println(value+"-"+souValue); + if (value.equals(souValue)) { +// System.out.println(value+"-"+sourItr.next()); + sourItr.remove(); +// System.out.println("remove"); + } + } +// System.out.println("---------------------------------"); + } + } + } + + public Object[] toArray() { + Object[] newObj = new Object[size]; + if (head == null) return new Object[]{}; + Node x = head; + for (int i = 0; i < size; i++) { + newObj[i] = x.data; + x = x.next; + } + return newObj; + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if (head == null) return; + Node n1 = head; + Node n2 = head.next; + while (n1 != null && n2 != null) { + if (Objects.equals(n1.data, n2.data)) { + n2 = n2.next; + n1.next = n2; + size--; + } else { + n1 = n2; + n2 = n2.next; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Node x = head; + boolean mingetflag = false; + boolean maxgetflag = false; + Node minEndNode = null; + Node maxStartNode = null; + while (x.next != null) { + if ((int) x.data <= min && !mingetflag) { + minEndNode = x; + }else { + mingetflag = true; + } + if ((int) x.data >= max && !maxgetflag) { + maxStartNode = x; + maxgetflag = true; + } + if (maxgetflag && mingetflag) + break; + x = x.next; + } + System.out.println(minEndNode.data + "-" + maxStartNode.data+"-"+maxStartNode.next.data); + if (minEndNode != null && maxStartNode != null) { + clear(minEndNode, maxStartNode); + } + if (minEndNode == null && maxStartNode != null) { + clear(null, maxStartNode); + } + if (minEndNode != null && maxStartNode == null) { + clear(minEndNode, null); + } + } + + private void clear(Node start, Node end) { + if (start == null) + start = head; + Node x = start.next; + while (x != null) { + if (end != null && Objects.equals(x.data, end.data)) { + break; + } + Node next = x.next; + x.data = null; + x.next = null; + x = next; + size--; + } + start.next = end; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public WLinkedList intersection(WLinkedList list) { + WLinkedList result = new WLinkedList(); + Node n1=this.head,n2=list.head; + while (n1!=null&&n2!=null){ + if (Objects.equals(n1.data,n2.data)){ + result.add(n1.data); + n1=n1.next; + n2=n2.next; + }else if ((int)n1.data>(int)n2.data){ + n2=n2.next; + }else { + n1=n1.next; + } + } + return result; + } + +} diff --git a/group15/1521_653895972/src/task3/download/DownloadThread.java b/group15/1521_653895972/src/task3/download/DownloadThread.java new file mode 100644 index 0000000000..28c541c75e --- /dev/null +++ b/group15/1521_653895972/src/task3/download/DownloadThread.java @@ -0,0 +1,38 @@ +package task3.download; + +import task3.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread{ + Connection conn; + int startPos; + int endPos; + String targetPath; + + public DownloadThread(Connection conn, int startPos, int endPos){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + public DownloadThread( Connection conn, int startPos, int endPos, String targetPath) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.targetPath = targetPath; + } + + public void run(){ + try { + byte[] rs = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile(targetPath, "rw"); + raf.seek(startPos); + raf.write(rs, 0, rs.length); + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1521_653895972/src/task3/download/FileDownloader.java b/group15/1521_653895972/src/task3/download/FileDownloader.java new file mode 100644 index 0000000000..a75186ade6 --- /dev/null +++ b/group15/1521_653895972/src/task3/download/FileDownloader.java @@ -0,0 +1,94 @@ +package task3.download; + +import task3.download.api.Connection; +import task3.download.api.ConnectionManager; +import task3.download.api.DownloadListener; + +import java.util.ArrayList; +import java.util.List; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + int startPos=0,endPos=0; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + String targetPath = "G:\\wanc\\图片\\targetfile." + url.substring(url.lastIndexOf(".") + 1); + List list = new ArrayList<>(); + int size = 3; + for (int i = 0; i < size; i++) { + conn = cm.open(this.url); + startPos = i * (length / size); + endPos = (i == size - 1) ? length - 1 : (i + 1) * (length / size) - 1; + DownloadThread thread = new DownloadThread(conn, startPos, endPos, targetPath); + list.add(thread); + thread.start(); + } + + // 调用线程的join方法,保证所有的线程都结束后再发出结束通知 + for (int i = 0; i < list.size(); i++) { + try { + list.get(i).join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + listener.notifyFinished(); + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group15/1521_653895972/src/task3/download/api/Connection.java b/group15/1521_653895972/src/task3/download/api/Connection.java new file mode 100644 index 0000000000..e9ce626831 --- /dev/null +++ b/group15/1521_653895972/src/task3/download/api/Connection.java @@ -0,0 +1,23 @@ +package task3.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group15/1521_653895972/src/task3/download/api/ConnectionException.java b/group15/1521_653895972/src/task3/download/api/ConnectionException.java new file mode 100644 index 0000000000..a9c2c5ef83 --- /dev/null +++ b/group15/1521_653895972/src/task3/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package task3.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group15/1521_653895972/src/task3/download/api/ConnectionManager.java b/group15/1521_653895972/src/task3/download/api/ConnectionManager.java new file mode 100644 index 0000000000..bb83d56a62 --- /dev/null +++ b/group15/1521_653895972/src/task3/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package task3.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException, Exception; +} diff --git a/group15/1521_653895972/src/task3/download/api/DownloadListener.java b/group15/1521_653895972/src/task3/download/api/DownloadListener.java new file mode 100644 index 0000000000..6d0cb69e7c --- /dev/null +++ b/group15/1521_653895972/src/task3/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package task3.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group15/1521_653895972/src/task3/download/impl/ConnectionImpl.java b/group15/1521_653895972/src/task3/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..c38ff387ee --- /dev/null +++ b/group15/1521_653895972/src/task3/download/impl/ConnectionImpl.java @@ -0,0 +1,55 @@ +package task3.download.impl; + +import task3.download.api.Connection; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionImpl implements Connection { + private HttpURLConnection conn; + + public ConnectionImpl(String urlString) { + URL targetUrl = null; + try { + targetUrl = new URL(urlString); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + try { + conn = (HttpURLConnection) targetUrl.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + //设置读取的文件块 + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream is = conn.getInputStream(); + ByteArrayOutputStream out =new ByteArrayOutputStream(); + int len=0; + byte[] buff = new byte[1024]; + while ((len = is.read(buff)) != -1) { + out.write(buff,0,len); + } + out.close(); + is.close(); + return out.toByteArray(); + } + + @Override + public int getContentLength() { + return conn.getContentLength(); + } + + @Override + public void close() { + conn.disconnect(); + } + +} diff --git a/group15/1521_653895972/src/task3/download/impl/ConnectionManagerImpl.java b/group15/1521_653895972/src/task3/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..a488f89561 --- /dev/null +++ b/group15/1521_653895972/src/task3/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,13 @@ +package task3.download.impl; + +import task3.download.api.Connection; +import task3.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws Exception { + return new ConnectionImpl(url); + } + +} diff --git a/group15/1521_653895972/src/task3/test/FileDownloaderTest.java b/group15/1521_653895972/src/task3/test/FileDownloaderTest.java new file mode 100644 index 0000000000..c37d9e318e --- /dev/null +++ b/group15/1521_653895972/src/task3/test/FileDownloaderTest.java @@ -0,0 +1,60 @@ +package task3.test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import task3.download.FileDownloader; +import task3.download.api.ConnectionManager; +import task3.download.api.DownloadListener; +import task3.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://img.bizhi.sogou.com/images/1680x1050/2014/04/24/590270.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group15/1521_653895972/src/task3/test/WLinkedListTest.java b/group15/1521_653895972/src/task3/test/WLinkedListTest.java new file mode 100644 index 0000000000..4f1959823e --- /dev/null +++ b/group15/1521_653895972/src/task3/test/WLinkedListTest.java @@ -0,0 +1,72 @@ +package task3.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import task3.basic.WLinkedList; + +/** + * Created by wanc on 2017/3/13. + */ +public class WLinkedListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testRemoveDuplicateValues() throws Exception { + WLinkedList wll = new WLinkedList(); + wll.add(11); + wll.add(12); + wll.add(12); + wll.add(13); + wll.add(14); + wll.add(14); + wll.add(15); + wll.removeDuplicateValues(); + Assert.assertArrayEquals(new Object[]{11,12,13,14,15},wll.toArray()); + } + + @Test + public void testRemoveRange() throws Exception { + WLinkedList wll = new WLinkedList(); + wll.add(11); + wll.add(12); + wll.add(13); + wll.add(14); + wll.add(15); + wll.add(16); + wll.add(17); + wll.removeRange(12,16); +// wll.removeRange2(12,16); + Assert.assertArrayEquals(new Object[]{11,12,16,17},wll.toArray()); + } + + @Test + public void testIntersection() throws Exception { + WLinkedList wll = new WLinkedList(); + wll.add(11); + wll.add(12); + wll.add(13); + wll.add(14); + wll.add(15); + wll.add(16); + wll.add(17); + WLinkedList wll2 = new WLinkedList(); + wll2.add(8); + wll2.add(10); + wll2.add(12); + wll2.add(14); + wll2.add(16); + wll2.add(18); + WLinkedList wll3 =wll.intersection(wll2); + Assert.assertArrayEquals(new Object[]{12,14,16},wll3.toArray()); + } +} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/.classpath b/group16/1012075117/DataStructure219/.classpath deleted file mode 100644 index fceb4801b5..0000000000 --- a/group16/1012075117/DataStructure219/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/group16/1012075117/DataStructure219/.project b/group16/1012075117/DataStructure219/.project deleted file mode 100644 index 567baae65f..0000000000 --- a/group16/1012075117/DataStructure219/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - DataStructure219 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs b/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 3a21537071..0000000000 --- a/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java deleted file mode 100644 index a1d46a21d8..0000000000 --- a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.stackwei.DataStructure; - -/** - * - * @author stackwei -2017.2.25 - * - */ -public class ArrayList implements List { - - private int flag = -1; - private static final int DEFAULT_CAPACITY = 1; - private Object[] elementData = new Object[DEFAULT_CAPACITY]; - - @Override - public void add(Object element) { - // 当要添加数据的位置已经超过数组长度时,增长数组长度 - if (size() + 1 == elementData.length) { - grow(); - } - elementData[flag + 1] = element; - flag++; - } - - @Override - public void add(int index, Object element) { - if (index < 0 || index > getFlag() + 1) { - System.out.println("在--" + index + "--添加的--" + element + "--无效,因为越界了!"); - return; - } - // 数组长度永远比已存数据大一个。 - if (size() + 1 == elementData.length) { - grow(); - } - elementData[index] = element; - if (index > getFlag()) { - flag++; - } - } - - @Override - public Object get(int index) { - if (index < 0 || index > getFlag()) { - System.out.print("在--" + index + "--的get无效,因为越界了!"); - return null; - } - return elementData[index]; - } - - @Override - public Object remove(int index) { - if (index < 0 || index > getFlag()) { - System.out.println("在--" + index + "--的remove无效,因为越界了!"); - return null; - } - Object oldValue = elementData[index]; - elementData[index] = null; - // 将删除处后面的数据往前移一格。 - Object[] data2 = new Object[elementData.length - 1]; - System.arraycopy(elementData, 0, data2, 0, getFlag()); - elementData = data2; - flag--; - return oldValue; - } - - @Override - public int size() { - return getFlag() + 1; - } - - public int getFlag() { - return flag; - } - - private void grow() { - Object[] data2 = new Object[elementData.length + 1]; - System.arraycopy(elementData, 0, data2, 0, getFlag() + 2);// 最后一个参数是需要复制的数据的数量。 - elementData = data2; - } - - /** - * 测试用例 - * - * @param args - */ - public static void main(String[] args) { - ArrayList al = new ArrayList(); - al.add(0, 99); - al.add(1, 100); - System.out.println(al.get(1)); - al.remove(1); - System.out.println(al.get(1)); - System.out.println(al.size()); - } -} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java deleted file mode 100644 index a1c728f0a1..0000000000 --- a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java +++ /dev/null @@ -1,194 +0,0 @@ -package com.stackwei.DataStructure; - -/** - * - * @author stackwei -2017.2.25 - * - */ -public class LinkedList implements List { - - private Node head = null; - private Node last = null; - private int size = 0; - - private static class Node { - Object item; - Node prev; - Node next; - - public Node(Node prev, Object item, Node next) { - this.prev = prev; - this.item = item; - this.next = next; - } - } - - @Override - public void add(Object element) { - addLast(element); - } - - @Override - public void add(int index, Object element) { - if (index < 0 || index > size) { - System.out.println("操作无效,越界了"); - return; - } - if (index == 0) { - addFirst(element); - return; - } - if (index == size) { - addLast(element); - return; - } - Node indexNode = node(index); - Node newNode = new Node(indexNode.prev, element, indexNode); - indexNode.prev.next = newNode; - indexNode.prev = newNode; - size++; - } - - @Override - public Object get(int index) { - if (index < 0 || index >= size) { - System.out.println("查询无效,越界了"); - return null; - } - if (index == 0) { - return head.item; - } - return node(index).item; - } - - @Override - public Object remove(int index) { - if (index < 0 || index > size) { - System.out.println("是空的,无法删除"); - return null; - } - if (index == 0) { - return removeFirst(); - } - if (index == size - 1) { - return removeLast(); - } - Node x = node(index); - final Object element = x.item; - final Node next = x.next; - final Node prev = x.prev; - - if (prev == null) { - head = next; - } else { - prev.next = next; - x.prev = null; - } - - if (next == null) { - last = prev; - } else { - next.prev = prev; - x.next = null; - } - - x.item = null; - size--; - return element; - } - - @Override - public int size() { - return size; - } - - private void addFirst(Object element) { - final Node f = head; - Node newNode = new Node(null, element, f); - head = newNode; - if (f == null) - last = newNode; - else - f.prev = newNode; - size++; - } - - public void addLast(Object element) { - if (head == null) { - addFirst(element); - } else { - Node newNode = new Node(last, element, null); - last.next = newNode; - last = newNode; - size++; - } - } - - public Object removeFirst() { - if (head == null) { - System.out.println("是空的,无法删除"); - return null; - } else { - Node x = head; - Node next = head.next; - Object element = x.item; - x.item = null; - x.next = null; - head = next; - if (next == null) - last = null; - else - x.prev = null; - size--; - return element; - } - } - - public Object removeLast() { - if (last == null) { - System.out.println("是空的,无法删除"); - return null; - } else { - final Node l = last; - final Object element = l.item; - final Node p = l.prev; - l.item = null; - l.prev = null; - last = p; - if (p == null) - head = null; - else - p.next = null; - size--; - return element; - } - } - - Node node(int index) { - if (index < (size >> 1)) { - Node x = head; - for (int i = 0; i < index; i++) - x = x.next; - return x; - } else { - Node x = last; - for (int i = size - 1; i > index; i--) - x = x.prev; - return x; - } - } - - /** - * 测试用例 - * - * @param args - */ - public static void main(String[] args) { - LinkedList ll = new LinkedList(); - ll.add(0, "xxx"); - ll.add(1, 111); - System.out.println(ll.size()); - System.out.println(ll.get(2)); - - } -} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java deleted file mode 100644 index 5226796141..0000000000 --- a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.stackwei.DataStructure; - -public interface List { - public void add(Object o); - - public void add(int index, Object o); - - public Object get(int index); - - public Object remove(int index); - - public int size(); -} diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java deleted file mode 100644 index 4a227495e9..0000000000 --- a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.stackwei.DataStructure; - -/** - * - * @author stackwei -2017.2.25 - * - */ -public class Queue { - - private LinkedList ll = new LinkedList(); - - /** - * 在队尾添加数据 - * @param element - */ - public void enQueue(Object element) { - ll.addLast(element); - } - - /** - * 删除队头数据 - * @return - */ - public Object deQueue() { - return ll.removeFirst(); - } - - /** - * 队列是否为空 - * @return - */ - public boolean isEmpty() { - if (ll.size() > 0) { - return false; - } - return true; - } - - /** - * 测试用例 - * @param args - */ - public static void main(String[] args) { - Queue q = new Queue(); - q.enQueue(97); - q.enQueue(98); - q.enQueue(99); - System.out.println(q.isEmpty()); - System.out.println(q.deQueue()); - } - -} diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java deleted file mode 100644 index 1b047ffafd..0000000000 --- a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.stackwei.DataStructure; - -/** - * - * @author stackwei -2017.2.25 - * - */ -public class Stack { - - private ArrayList al = new ArrayList(); - - /** - * 进栈 - * @param item - */ - public void push(Object item) { - al.add(item); - } - - /** - * 出栈 - * @return - */ - public Object pop() { - return al.remove(al.getFlag()); - } - - /** - * 获取栈顶元素 - * @return - */ - public Object peek() { - return al.get(al.getFlag()); - } - - /** - * 栈是否为空 - * @return - */ - public boolean isEmpty() { - if (al.getFlag() >= 0) { - return false; - } - return true; - } - - /** - * 测试用例 - * @param args - */ - public static void main(String[] args) { - Stack s = new Stack(); - s.push(98); - s.push(99); - s.pop(); - System.out.println(s.peek()); - } - -} diff --git a/group16/1012075117/src/com/coderising/download/DownloadThread.java b/group16/1012075117/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..d89e878fb0 --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,49 @@ +package com.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class DownloadThread extends Thread { + + ConnectionManager cm; + Connection conn; + DownloadListener downloadListener; + int startPos; + int endPos; + String fileName; + String url; + + public DownloadThread(String url, int startPos, int endPos, String fileName, DownloadListener downloadListener) { + this.url = url; + this.startPos = startPos; + this.endPos = endPos; + this.fileName = fileName; + this.downloadListener = downloadListener; + } + + public void run() { + cm = new ConnectionManagerImpl(); + byte[] b = null; + RandomAccessFile randomAF = null; + try { + conn = cm.open(url); + b = conn.read(startPos, endPos); + randomAF = new RandomAccessFile(fileName, "rw"); + randomAF.seek(startPos); + randomAF.write(b); + randomAF.close(); + downloadListener.notifyFinished(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ConnectionException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/download/FileDownloader.java b/group16/1012075117/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..7ff9ee2b29 --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,116 @@ +package com.coderising.download; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +/** + * 实现多线程下载 - 第三次作业 + * @author stackwei + * @date 2017/4/3 + * @status ok + */ +public class FileDownloader { + + String url; + DownloadListener listener; + ConnectionManager cm; + AtomicInteger atomicInteger; + + public FileDownloader(String _url) { + this.url = _url; + atomicInteger = new AtomicInteger(); + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. + // 需要调用ConnectionManager的open方法打开连接,然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载,注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法,read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + String fileName = conn.getFileName(); + + int p2 = 0; + int p4 = 0; + if (length % 3 == 0) { + p2 = length / 3; + p4 = 2 * p2; + } else { + if((length - 1) % 3 == 0) { + p2 = (length - 1) / 3; + p4 = 2 * p2; + } else { + if((length - 2) % 3 == 0) { + p2 = (length - 2) / 3; + p4 = 2 * p2; + } + } + } + + atomicInteger.getAndIncrement(); + atomicInteger.getAndIncrement(); + atomicInteger.getAndIncrement(); + + new DownloadThread(url, 0, p2 , fileName, new DownloadListener() { + @Override + public void notifyFinished() { + if (atomicInteger.decrementAndGet() == 0) + FileDownloader.this.listener.notifyFinished(); + }; + }).start(); + + new DownloadThread(url, p2 + 1, p4, fileName, new DownloadListener() { + @Override + public void notifyFinished() { + if (atomicInteger.decrementAndGet() == 0) + FileDownloader.this.listener.notifyFinished(); + }; + }).start(); + + new DownloadThread(url, p4 + 1, length - 1, fileName, new DownloadListener() { + @Override + public void notifyFinished() { + if (atomicInteger.decrementAndGet() == 0) + FileDownloader.this.listener.notifyFinished(); + }; + }).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/download/FileDownloaderTest.java b/group16/1012075117/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..6445bdf48d --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,40 @@ +package com.coderising.download; + +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Test + public void testDownload() { + String url = "http://img3.cache.netease.com/photo/0038/2017-03-31/CGT4JVHJ5S400038.jpg"; + FileDownloader downloader = new FileDownloader(url); + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/download/api/Connection.java b/group16/1012075117/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..fb340f390d --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/api/Connection.java @@ -0,0 +1,29 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); + + /** + * 获取文件名 + * @return + */ + public String getFileName(); +} diff --git a/group16/1012075117/src/com/coderising/download/api/ConnectionException.java b/group16/1012075117/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/1012075117/src/com/coderising/download/api/ConnectionManager.java b/group16/1012075117/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group16/1012075117/src/com/coderising/download/api/DownloadListener.java b/group16/1012075117/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/1012075117/src/com/coderising/download/impl/ConnectionImpl.java b/group16/1012075117/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..35dea6afdb --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,65 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection { + private HttpURLConnection httpURLConnection; + private InputStream inputstream; + String url; + + public ConnectionImpl(HttpURLConnection httpURLConnection, String url) { + this.httpURLConnection = httpURLConnection; + this.url = url; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + inputstream = httpURLConnection.getInputStream(); + int total = endPos - startPos + 1; + inputstream.skip(startPos); + byte[] bytes = new byte[total]; + int len = 0; + int hasRead = 0; + while ((len = inputstream.read(bytes, hasRead, total - hasRead)) > 0) { + hasRead = hasRead + len; + } + return bytes; + } + + @Override + public int getContentLength() { + return httpURLConnection.getContentLength(); + } + + @Override + public void close() { + try { + if (inputstream != null) + inputstream.close(); + httpURLConnection.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public String getFileName() { + int index; + String fileName = "src/com/coderising/download/"; + String temp = httpURLConnection.getHeaderField("Content-Disposition"); + if (temp != null) { + index = temp.indexOf("="); + fileName += temp.substring(index + 2, temp.length() - 1); + return fileName; + } else { + index = url.lastIndexOf("/"); + fileName += url.substring(index + 1); + return fileName; + } + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group16/1012075117/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..6b576f0946 --- /dev/null +++ b/group16/1012075117/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,38 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + URL netURL = null; + URLConnection urlConnection = null; + HttpURLConnection httpURLConnection = null; + ConnectionImpl connectionImpl = null; + + try { + netURL = new URL(url); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + try { + urlConnection = netURL.openConnection(); + httpURLConnection = (HttpURLConnection) urlConnection; + httpURLConnection.connect(); + connectionImpl = new ConnectionImpl(httpURLConnection, url); + } catch (IOException e) { + e.printStackTrace(); + } + return connectionImpl; + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/litestruts/LoginAction.java b/group16/1012075117/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..641e224b67 --- /dev/null +++ b/group16/1012075117/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +public class LoginAction { + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/litestruts/Struts.java b/group16/1012075117/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ca1fa4892e --- /dev/null +++ b/group16/1012075117/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,182 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * 读取配置文件 struts.xml - 第二次作业 + * @author stackwei + * @date 2017/3/20 + * @status ok + */ +public class Struts { + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + * + */ + + int flag = 0; + String className; + String executeResult; + String jsp; + Element resultElement; + List actionList = new ArrayList<>(); + Map classNameMap = new HashMap(); + Map messagesMap = new HashMap(); + View view = new View(); + + actionList = getRootElement("src/com/litestruts/struts.xml");// 获取所有节点 + classNameMap = getClassName(actionList, actionName, classNameMap);// 获取action的类名并放到Map中 + + className = (String) classNameMap.get("className"); + messagesMap = getResult(className, parameters);// messages包含了,调用execute()后的返回值result,和所有getter方法的值和属性 + + executeResult = (String) messagesMap.get("result"); + messagesMap.remove("result"); + flag = (int) classNameMap.get("flag"); + resultElement = actionList.get(flag); + jsp = getJSP(executeResult, resultElement);// 获取到里的jsp + + view.setJsp(jsp); + view.setParameters(messagesMap); + + return view; + } + + /** + * 获取所有节点 + * + * @param fileName + * @return + */ + private static List getRootElement(String fileName) { + File inputXml = new File(fileName); + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(inputXml); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + List al = new ArrayList(); + for (Iterator i = root.elementIterator(); i.hasNext();) { // 获取所有action节点 + Element action = (Element) i.next(); + al.add(action); + } + return al; + } + + /** + * 根据给定的actionName,获取对应的class名字 + * + * @param al + * @param actionName + * @param map + * @return + */ + private static Map getClassName(List al, String actionName, Map map) { + String className = null; + for(int i=0;i getResult(String className, Map parameters) throws Exception { + Class actionClass = null; + Constructor constructor = null; + Object object = null; + Method method = null; + Map map = new HashMap(); + try { + actionClass = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + try { + constructor = actionClass.getConstructor(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + object = constructor.newInstance(); + Set keySet = parameters.keySet(); + // 据parameters中的数据,调用对象的setter方法 + for (String key : keySet) { + if (key.equals("name")) { + method = actionClass.getMethod("setName", String.class); + method.invoke(object, parameters.get(key)); + } + if (key.equals("password")) { + method = actionClass.getMethod("setPassword", String.class); + method.invoke(object, parameters.get(key)); + } + } + // 通过反射调用对象的execute 方法,并获得返回值,例如"success" + method = actionClass.getMethod("execute"); + String result = (String) method.invoke(object); + map.put("result", result); + + //找到对象的所有getter方法,把值和属性形成一个HashMap + Method getName = actionClass.getMethod("getName"); + Method getPassword = actionClass.getMethod("getPassword"); + Method getMessage = actionClass.getMethod("getMessage"); + map.put("name", getName.invoke(object)); + map.put("password", getPassword.invoke(object)); + map.put("message", getMessage.invoke(object)); + + return map; + } + + private static String getJSP(String result, Element actionElement) { + String jsp = null; + for (Iterator i = actionElement.elementIterator(); i.hasNext();) { // 获取所有action子节点result + Element resultElement = (Element) i.next(); + if(resultElement.attribute("name").getValue().equals(result)) { + jsp = resultElement.getTextTrim(); + } + } + return jsp; + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/litestruts/StrutsTest.java b/group16/1012075117/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..4e1ced3ee5 --- /dev/null +++ b/group16/1012075117/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 密码和预设的不一致 + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coderising/litestruts/View.java b/group16/1012075117/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..564b4127e1 --- /dev/null +++ b/group16/1012075117/src/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coding/basic/ArrayList.java b/group16/1012075117/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..6a83a34b41 --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/ArrayList.java @@ -0,0 +1,95 @@ +package com.coding.basic; + +/** + * 实现 ArrayList - 第一次作业 + * @author stackwei + * @date 2017/2/25 + * @status ok + */ +public class ArrayList implements List { + + private int flag = -1; + private static final int DEFAULT_CAPACITY = 1; + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + @Override + public void add(Object element) { + // 当要添加数据的位置已经超过数组长度时,增长数组长度 + if (size() + 1 == elementData.length) { + grow(); + } + elementData[flag + 1] = element; + flag++; + } + + @Override + public void add(int index, Object element) { + if (index < 0 || index > getFlag() + 1) { + System.out.println("在--" + index + "--添加的--" + element + "--无效,因为越界了!"); + return; + } + // 数组长度永远比已存数据大一个。 + if (size() + 1 == elementData.length) { + grow(); + } + elementData[index] = element; + if (index > getFlag()) { + flag++; + } + } + + @Override + public Object get(int index) { + if (index < 0 || index > getFlag()) { + System.out.print("在--" + index + "--的get无效,因为越界了!"); + return null; + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > getFlag()) { + System.out.println("在--" + index + "--的remove无效,因为越界了!"); + return null; + } + Object oldValue = elementData[index]; + elementData[index] = null; + // 将删除处后面的数据往前移一格。 + Object[] data2 = new Object[elementData.length - 1]; + System.arraycopy(elementData, 0, data2, 0, getFlag()); + elementData = data2; + flag--; + return oldValue; + } + + @Override + public int size() { + return getFlag() + 1; + } + + public int getFlag() { + return flag; + } + + private void grow() { + Object[] data2 = new Object[elementData.length + 1]; + System.arraycopy(elementData, 0, data2, 0, getFlag() + 2);// 最后一个参数是需要复制的数据的数量。 + elementData = data2; + } + + /** + * 测试用例 + * + * @param args + */ + public static void main(String[] args) { + ArrayList al = new ArrayList(); + al.add(0, 99); + al.add(1, 100); + System.out.println(al.get(1)); + al.remove(1); + System.out.println(al.get(1)); + System.out.println(al.size()); + } +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coding/basic/Iterator.java b/group16/1012075117/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1012075117/src/com/coding/basic/LinkedList.java b/group16/1012075117/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..fd0214bd1a --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/LinkedList.java @@ -0,0 +1,195 @@ +package com.coding.basic; + +/** + * 实现 LinkedList - 第一次作业 + * @author stackwei + * @date 2017/2/25 + * @status ok + */ +public class LinkedList implements List { + + private Node head = null; + private Node last = null; + private int size = 0; + + private static class Node { + Object item; + Node prev; + Node next; + + public Node(Node prev, Object item, Node next) { + this.prev = prev; + this.item = item; + this.next = next; + } + } + + @Override + public void add(Object element) { + addLast(element); + } + + @Override + public void add(int index, Object element) { + if (index < 0 || index > size) { + System.out.println("操作无效,越界了"); + return; + } + if (index == 0) { + addFirst(element); + return; + } + if (index == size) { + addLast(element); + return; + } + Node indexNode = node(index); + Node newNode = new Node(indexNode.prev, element, indexNode); + indexNode.prev.next = newNode; + indexNode.prev = newNode; + size++; + } + + @Override + public Object get(int index) { + if (index < 0 || index >= size) { + System.out.println("查询无效,越界了"); + return null; + } + if (index == 0) { + return head.item; + } + return node(index).item; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > size) { + System.out.println("是空的,无法删除"); + return null; + } + if (index == 0) { + return removeFirst(); + } + if (index == size - 1) { + return removeLast(); + } + Node x = node(index); + final Object element = x.item; + final Node next = x.next; + final Node prev = x.prev; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + x.next = null; + } + + x.item = null; + size--; + return element; + } + + @Override + public int size() { + return size; + } + + private void addFirst(Object element) { + final Node f = head; + Node newNode = new Node(null, element, f); + head = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + public void addLast(Object element) { + if (head == null) { + addFirst(element); + } else { + Node newNode = new Node(last, element, null); + last.next = newNode; + last = newNode; + size++; + } + } + + public Object removeFirst() { + if (head == null) { + System.out.println("是空的,无法删除"); + return null; + } else { + Node x = head; + Node next = head.next; + Object element = x.item; + x.item = null; + x.next = null; + head = next; + if (next == null) + last = null; + else + x.prev = null; + size--; + return element; + } + } + + public Object removeLast() { + if (last == null) { + System.out.println("是空的,无法删除"); + return null; + } else { + final Node l = last; + final Object element = l.item; + final Node p = l.prev; + l.item = null; + l.prev = null; + last = p; + if (p == null) + head = null; + else + p.next = null; + size--; + return element; + } + } + + Node node(int index) { + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + /** + * 测试用例 + * + * @param args + */ + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.add(0, "xxx"); + ll.add(1, 111); + System.out.println(ll.size()); + System.out.println(ll.get(2)); + + } +} \ No newline at end of file diff --git a/group16/1012075117/src/com/coding/basic/List.java b/group16/1012075117/src/com/coding/basic/List.java new file mode 100644 index 0000000000..03fa879b2e --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group16/1012075117/src/com/coding/basic/Queue.java b/group16/1012075117/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..41cd854e34 --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/Queue.java @@ -0,0 +1,53 @@ +package com.coding.basic; + +/** + * 实现 Queue - 第一次作业 + * @author stackwei + * @date 2017/2/25 + * @status ok + */ +public class Queue { + + private LinkedList ll = new LinkedList(); + + /** + * 在队尾添加数据 + * @param element + */ + public void enQueue(Object element) { + ll.addLast(element); + } + + /** + * 删除队头数据 + * @return + */ + public Object deQueue() { + return ll.removeFirst(); + } + + /** + * 队列是否为空 + * @return + */ + public boolean isEmpty() { + if (ll.size() > 0) { + return false; + } + return true; + } + + /** + * 测试用例 + * @param args + */ + public static void main(String[] args) { + Queue q = new Queue(); + q.enQueue(97); + q.enQueue(98); + q.enQueue(99); + System.out.println(q.isEmpty()); + System.out.println(q.deQueue()); + } + +} diff --git a/group16/1012075117/src/com/coding/basic/Stack.java b/group16/1012075117/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..34d4692113 --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/Stack.java @@ -0,0 +1,60 @@ +package com.coding.basic; + +/** + * 实现 Stack - 第一次作业 + * @author stackwei + * @date 2017/2/25 + * @status ok + */ +public class Stack { + + private ArrayList al = new ArrayList(); + + /** + * 进栈 + * @param item + */ + public void push(Object item) { + al.add(item); + } + + /** + * 出栈 + * @return + */ + public Object pop() { + return al.remove(al.getFlag()); + } + + /** + * 获取栈顶元素 + * @return + */ + public Object peek() { + return al.get(al.getFlag()); + } + + /** + * 栈是否为空 + * @return + */ + public boolean isEmpty() { + if (al.getFlag() >= 0) { + return false; + } + return true; + } + + /** + * 测试用例 + * @param args + */ + public static void main(String[] args) { + Stack s = new Stack(); + s.push(98); + s.push(99); + s.pop(); + System.out.println(s.peek()); + } + +} diff --git a/group16/1012075117/src/com/coding/basic/array/ArrayUtil.java b/group16/1012075117/src/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..f75a0be1eb --- /dev/null +++ b/group16/1012075117/src/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,263 @@ +package com.coding.basic.array; + +import java.util.Arrays; + +/** + * 数组工具类-第二次作业 + * @author stackwei + * @date 2017/3/20 + * @status ok + */ +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int length; + int[] temp; + + length = origin.length; + temp = new int[length]; + for (int i = 0; i < length; i++) { + temp[length - i - 1] = origin[i]; + } + for (int i = 0; i < length; i++) { + origin[i] = temp[i]; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int flag = 0; + int j = 0; + int length; + length = oldArray.length; + int[] newArray; + + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0) { + flag++; + } + } + newArray = new int[flag]; + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int[] temp; + int[] array3; + int flag = 0; + int repeat = 0; + boolean boolea = true; + int length1 = array1.length; + int length2 = array2.length; + temp = new int[length1 + length2]; + + // 先把a1添加到temp + for (int i = 0; i < length1; i++) { + temp[i] = array1[i]; + } + // 把a2中不重复的添加到temp + for (int i = 0; i < length2; i++) { + for (int j = 0; j < length1; j++) { + if (temp[j] == array2[i]) { + boolea = false; + repeat++; + } + } + if (boolea) { + temp[length1 + flag] = array2[i]; + flag++; + } + boolea = true; + } + // 有重复就new一个数组长度减去重复的长度的a3,排序并返回 + if (repeat != 0) { + array3 = new int[length1 + length2 - repeat]; + for (int i = 0; i < (temp.length - repeat); i++) { + array3[i] = temp[i]; + } + Arrays.sort(array3); + return array3; + } + // 无重复就排序并返回 + Arrays.sort(temp); + return temp; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] temp; + int length; + + length = oldArray.length; + temp = new int[length + size]; + for (int i = 0; i < length; i++) { + temp[i] = oldArray[i]; + } + oldArray = null; + oldArray = temp; + + return oldArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int[] temp = new int[2]; + int[] array; + int f1 = 1; + int f2 = 1; + int length = 2; + + if (max <= 1) { + return null; + } + + temp[0] = f1; + temp[1] = f2; + if (max == 2) { + return temp; + } + + for (int i = 2; temp[i - 1] < max; i++) { + if ((f1 + f2) >= max) + break; + if (i + 1 > temp.length) { + temp = new ArrayUtil().grow(temp, 1); + } + temp[i] = f1 + f2; + f1 = temp[i - 1]; + f2 = temp[i]; + length++; + } + array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = temp[i]; + } + + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] temp = new int[1]; + boolean flag = true; + int i = 0; + + if (max < 3) + return null; + + for (int j = 2; j < max; j++) { + for (int k = 2; k <= Math.sqrt(j); k++) { + if (j % k == 0) { + flag = false; + break; + } + } + if (flag) { + if (i + 1 > temp.length) + temp = new ArrayUtil().grow(temp, 1); + temp[i] = j; + i++; + } + flag = true; + } + return temp; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] temp = new int[1]; + int i = 0; + + if (max < 6) + return null; + + for (int j = 1; j < max; j++) { + int total = 0; + for (int k = 1; k < j / 2 + 1; k++) { + if (j % k == 0) + total += k; + } + if (total == j) { + if (i + 1 > temp.length) + temp = new ArrayUtil().grow(temp, 1); + temp[i] = j; + i++; + } + } + return temp; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + if(array == null || array.length ==0 || seperator == null) + return null; + + StringBuilder sb = new StringBuilder(); + int length = array.length; + for(int i=0;i7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 从当前链表中取出那些listB所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在listB中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } + +} \ No newline at end of file diff --git a/group16/2562124714/.idea/misc.xml b/group16/2562124714/.idea/misc.xml index e97ef03f44..05483570e0 100644 --- a/group16/2562124714/.idea/misc.xml +++ b/group16/2562124714/.idea/misc.xml @@ -1,22 +1,6 @@ - + - - - - - 1.7 - - - - - - - \ No newline at end of file diff --git a/group16/2562124714/.idea/workspace.xml b/group16/2562124714/.idea/workspace.xml index d357c0f9a1..bba44e297b 100644 --- a/group16/2562124714/.idea/workspace.xml +++ b/group16/2562124714/.idea/workspace.xml @@ -13,49 +13,60 @@ + + + + + - - - - - - - - - - - + - - - - + + + + + + + + + + + + - + - - - + + + + + - - + + - - + + @@ -63,46 +74,78 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -130,8 +173,6 @@ + + + + true + DEFINITION_ORDER + @@ -166,6 +216,7 @@ + @@ -209,20 +260,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + @@ -410,6 +521,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -468,6 +717,15 @@ + + + + + + + + + + + + + + + @@ -529,6 +793,22 @@ + + + + + + + + + + + + + + + + @@ -557,6 +837,8 @@ @@ -568,40 +850,56 @@ + + + + + + + + + + + - + - - + - + + - + + + + @@ -616,66 +914,269 @@ + + + + + + + + + + + + + - - + + - - - - + + + + + + - + + + + + + + + + + + + + + + + + + + + + - + - - + + + + + - + - - + + - - + + + + - + - - + + - + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -683,7 +1184,6 @@ - @@ -699,82 +1199,182 @@ - - - - - + + + + + + + - + - + + + - + - + + + - - + + + + + + + - - + + + + + + + - + - - - - + + + + + + + + + + + + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + \ No newline at end of file diff --git a/group16/2562124714/src/Test/StrutsTest.java b/group16/2562124714/src/Test/StrutsTest.java new file mode 100644 index 0000000000..663c9dba3b --- /dev/null +++ b/group16/2562124714/src/Test/StrutsTest.java @@ -0,0 +1,43 @@ +package Test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + com.coderising.litestruts.View view = com.coderising.litestruts.Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + com.coderising.litestruts.View view = com.coderising.litestruts.Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/2562124714/src/Test/TestRunner.java b/group16/2562124714/src/Test/TestRunner.java index 2bf465f832..963fb955d3 100644 --- a/group16/2562124714/src/Test/TestRunner.java +++ b/group16/2562124714/src/Test/TestRunner.java @@ -10,7 +10,7 @@ */ public class TestRunner { public static void main(String[] args) { - org.junit.runner.Result result = JUnitCore.runClasses(BinaryTreeNodeTest.class); + org.junit.runner.Result result = JUnitCore.runClasses(StrutsTest.class); for (Failure failure:result.getFailures()) { System.out.println(failure.toString()); } diff --git a/group16/2562124714/src/com/coderising/action/LoginAction.java b/group16/2562124714/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..8f1ea73abb --- /dev/null +++ b/group16/2562124714/src/com/coderising/action/LoginAction.java @@ -0,0 +1,41 @@ +package com.coderising.action; + +/** + * Created by zhangwj on 2017/3/9. + */ +public class LoginAction { + private String Name; + private String Password; + private String Message; + + public void setName(String name) + { + this.Name = name; + } + public void setPassword(String pass) + { + this.Password = pass; + } + + public String exectue() + { + if (this.Name == "test" && this.Password == "1234") + { + this.Message = "login successful"; + return "success"; + } + else + { + this.Message = "login failed,please check your user/pwd"; + return "fail"; + } + } + + public String getMessage() + { + return this.Message; + } + + + +} diff --git a/group16/2562124714/src/com/coderising/array/ArrayUtil.java b/group16/2562124714/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..4b496a41f7 --- /dev/null +++ b/group16/2562124714/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,292 @@ +package com.coderising.array; + +import com.*; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if (1 == origin.length || 0 == origin.length) + { + return; + } + + int temp = 0; + for (int i = 0; i < origin.length / 2; i++) + { + temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public Integer[] removeZero(int[] oldArray){ + com.coding.basic.ArrayList blist = new com.coding.basic.ArrayList(); + + //int j = 0; + + for(int i = 0; i < oldArray.length; i++) + { + if (0 != oldArray[i]) + { + blist.add(oldArray[i]); + } + } + + Object[] newArray = blist.ToArray(); + + return (Integer[])newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public Integer[] merge(int[] array1, int[] array2){ + com.coding.basic.ArrayList blist = new com.coding.basic.ArrayList(); + int i = 0; + + for (i = 0; i < array1.length; i++) + { + blist.add(array1[0]); + } + + for(i = 0; i < array2.length; i++) + { + for (int j = 0; j < blist.size(); j ++) + { + if (array2[i] >= (int)blist.get(j + 1)) + { + if (array2[i] == (int)blist.get(j + 1)) + { + break; + } + //已经到最后了 + if (j == blist.size() - 1) + { + if (array2[i] == (int)blist.get(j + 1)) + { + break; + } + else + { + blist.add(j + 1, array2[i]); + break; + } + } + else + { + if (array2[i] <= (int)blist.get(j + 2)) + { + if (array2[i] == (int)blist.get(j + 2)) + { + break; + } + else + { + blist.add(j + 1, array2[i]); + break; + } + } + } + + } + else + { + if (j == 0) + { + blist.add(j + 1, array2[i]); + break; + } + else + { + continue; + } + } + } + } + + return (Integer[]) blist.ToArray(); + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] NewArray = new int[oldArray.length + size]; + + for(int i = 0; i < NewArray.length; i++) + { + if (i < oldArray.length) { + NewArray[i] = oldArray[i]; + } + else + { + NewArray[i] = 0; + } + } + + return NewArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public Integer[] fibonacci(int max){ + com.coding.basic.ArrayList result = new com.coding.basic.ArrayList(); + int i = 0; + int TempMax = 0; + + + while (true) + { + TempMax = CaculateFibonacci(i++); + if (TempMax <= max) + { + result.add(TempMax); + continue; + } + else + { + break; + } + } + + return (Integer[])result.ToArray(); + } + + public int CaculateFibonacci(int i) + { + if (1 == i) + return 1; + else if (2 == i) + return 1; + else + return CaculateFibonacci(i - 1) + CaculateFibonacci(i - 2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public Integer[] getPrimes(int max){ + com.coding.basic.ArrayList result = new com.coding.basic.ArrayList(); + + + + for(int i = 2; i < max; i ++) + { + if(CaculatePrimes(i)) + { + result.add(i); + } + } + + return (Integer[])result.ToArray(); + } + + //计算素数函数 算法好像不高明啊! + public boolean CaculatePrimes(int Num) + { + for (int i = 2; i < Math.sqrt(Num); i++) + { + if (Num % i == 0) + { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public Integer[] getPerfectNumbers(int max){ + com.coding.basic.ArrayList result = new com.coding.basic.ArrayList(); + + for (int i = 6; i < max; i++) + { + if (IsPerfectNumber(i)) + { + result.add(i); + } + } + return (Integer[])result.ToArray(); + } + + //计算所有的因子之和 算法并不高明啊! + public boolean IsPerfectNumber(int Num) + { + int temp = 0; + for (int i = 1; i < Num; i++) + { + if (Num % i == 0) + { + temp += i; + } + } + if (temp == Num) + { + return true; + } + else + { + return false; + } + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + + for (int i = 0; i < array.length - 1; i++) + { + result += Integer.toString(array[i])+ seperator; + } + + result += Integer.toString(array[array.length]); + + + return result; + } + + +} diff --git a/group16/2562124714/src/com/coderising/download/DownloadThread.java b/group16/2562124714/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..b03ab73e24 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,37 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CountDownLatch latch; + RandomAccessFile ResultFile; + + public DownloadThread( Connection conn, int startPos, int endPos, CountDownLatch latchArg, RandomAccessFile fileArg){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.latch = latchArg; + this.ResultFile = fileArg; + } + public void run(){ + try { + byte []b = this.conn.read(this.startPos, this.endPos); + System.out.println(b.toString()); + ResultFile.seek(startPos); + ResultFile.write(b, 0, endPos - startPos); + } catch (IOException e) { + e.printStackTrace(); + } + this.latch.countDown(); //下载完成就lockdown + } +} diff --git a/group16/2562124714/src/com/coderising/download/FileDownloader.java b/group16/2562124714/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..62a630867d --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,128 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + //写入文件 + //得到文件名 + String fileName = "E:\\zhuomian\\java课程\\testFile.jpg"; + //根据文件大小及文件名,创建一个同样大小,同样文件名的文件 + File file = new File(fileName); + RandomAccessFile raf = null; + try { + raf = new RandomAccessFile(file, "rw"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + Connection conn1 = null; + try { + CountDownLatch countdownlatch = new CountDownLatch(3); + + conn1 = cm.open(this.url); + Connection conn4 = cm.open(this.url); + int length = conn4.getContentLength(); + try { + raf.setLength(length); //设置文件长度 一系列的占位符 + } catch (IOException e) { + e.printStackTrace(); + } + new DownloadThread(conn1, 0, length / 3 - 1, countdownlatch, raf).start(); + Connection conn2 = cm.open(this.url); + new DownloadThread(conn2, length / 3, (length / 3) *2 - 1, countdownlatch, raf).start(); + Connection conn3 = cm.open(this.url); + new DownloadThread(conn3, (length / 3) *2 , length - 1, countdownlatch, raf).start(); + + + try { + countdownlatch.await(); + this.listener.notifyFinished(); + conn4.close(); + conn1.close(); + conn2.close(); + conn3.close(); + try { + if (raf != null) { + raf.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + //this.listener.notifyFinished(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn1 != null){ + conn1.close(); + } + if (raf != null) + { + try { + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group16/2562124714/src/com/coderising/download/FileDownloaderTest.java b/group16/2562124714/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..52d6495465 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://upload.qianlong.com/2017/0310/1489104335573.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group16/2562124714/src/com/coderising/download/api/Connection.java b/group16/2562124714/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group16/2562124714/src/com/coderising/download/api/ConnectionException.java b/group16/2562124714/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/2562124714/src/com/coderising/download/api/ConnectionManager.java b/group16/2562124714/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..f657345633 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + Connection open(String url) throws ConnectionException; +} diff --git a/group16/2562124714/src/com/coderising/download/api/DownloadListener.java b/group16/2562124714/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/2562124714/src/com/coderising/download/impl/ConnectionImpl.java b/group16/2562124714/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..8b4bee0010 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,55 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.ProtocolException; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + private HttpURLConnection connection; + @Override + public byte[] read(int startPos, int endPos) throws IOException { + this.connection.setRequestMethod("GET"); + this.connection.setReadTimeout(5000); + this.connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream inputstream = this.connection.getInputStream(); + byte[]b = new byte[endPos - startPos + 10]; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + System.out.println("开始下载"+startPos+"-" + endPos +"---"); + int length; + while(-1 != (length = inputstream.read(b))) { + bos.write(b, 0 ,length); + } + + + return bos.toByteArray(); + } + + @Override + public int getContentLength() { + int fileSize = this.connection.getContentLength(); + + System.out.println("文件大小为:"+fileSize); + + return fileSize; + } + + @Override + public void close() { + this.connection.disconnect(); + + + } + + public ConnectionImpl(URLConnection urlconnection) + { + this.connection = (HttpURLConnection)urlconnection; + } + +} diff --git a/group16/2562124714/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group16/2562124714/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..b7b8e02de5 --- /dev/null +++ b/group16/2562124714/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,35 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String desiredUrl) throws ConnectionException { + URL url = null; + + try + { + //create the HttpURLConnection + url = new URL(desiredUrl); + URLConnection connection = url.openConnection(); + //connection.connect(); + ConnectionImpl connectionimpl = new ConnectionImpl(connection); + return connectionimpl; + } + catch (Exception e) + { + e.printStackTrace(); + return null; + } + + //return null; + } + +} diff --git a/group16/2562124714/src/com/coderising/litestruts/Struts.java b/group16/2562124714/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..2bc79f1199 --- /dev/null +++ b/group16/2562124714/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,128 @@ +package com.coderising.litestruts; + +import com.sun.org.apache.regexp.internal.RE; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.helpers.DefaultHandler; + +import javax.print.Doc; +import org.w3c.dom.Document; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + //0. SAX Parser is faster and uses less memory than DOM parser. Dom解析功能强大,可增删改查,操作时会将xml文档以文档对象的方式读取到内存中,因此适用于小文档 + //Sax解析是从头到尾逐行逐个元素读取内容,修改较为不便,但适用于只读的大文档 + + long lasting = System.currentTimeMillis(); + + try { + File f = new File("C:\\Users\\zhangwj\\Desktop\\java课程\\coding\\coding2017-1\\group16\\2562124714\\src\\com\\coderising\\litestruts\\struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(f); + NodeList nl = doc.getElementsByTagName("struts"); + for (int i = 0; i < nl.getLength(); i++) { + + Node node = doc.getElementsByTagName("action").item(i); //get action node + //System.out.print("action name is " + doc.getElementsByTagName("action").item(i).getFirstChild().getNodeValue()); + Element e = (Element) node; + System.out.printf("attribute of name is "+ e.getAttribute("name") + " actionName Need is" + actionName); + if (e.getAttribute("name").toString().equals(actionName)) { + //1 获取相应的class 设置用户名和密码 + // System.out.print("action name is " + e.getAttribute("name") + " action class is " + e.getAttribute("class")); + Class ActionClass = Class.forName(e.getAttribute("class")); + //强制类型转换 + Object Action = ActionClass.newInstance(); + for (Map.Entry entry : parameters.entrySet() + ) { + if (entry.getKey() == "name") { + //设置姓名 + //2 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method fun_setName = ActionClass.getDeclaredMethod("setName", String.class); + fun_setName.invoke(Action, entry.getValue()); + + } else if (entry.getKey() == "password") { + //设置密码 + Method fun_setName = ActionClass.getDeclaredMethod("setPassword", String.class); + fun_setName.invoke(Action, entry.getValue()); + } else { + continue; + } + } + + Method ExecuteMethod = ActionClass.getDeclaredMethod("exectue"); + //2 调用execute方法 + String ss = "11"; + Object ExecuteResultValue = ExecuteMethod.invoke(Action); + + //3通过反射找到对象的所有getter方法(例如 getMessage), + //通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + //放到View对象的parameters + Method Getter_Method = ActionClass.getDeclaredMethod("getMessage"); + Object message = Getter_Method.invoke(Action); + Map messageMap = new HashMap(); + messageMap.put("message", (String) message); + com.coderising.litestruts.View view = new com.coderising.litestruts.View(); + view.setParameters(messageMap); + + //4 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + //放到View对象的jsp字段中。 + //首先获取result节点 + NodeList ResultNL = ((Element) node).getElementsByTagName("result"); + for (int j = 0; j < ResultNL.getLength(); j++ + ) { + Node node1 = ResultNL.item(j); + Element e1 = (Element) node1; + System.out.println("name is " + e1.getAttribute("name") + "return Value is" + (String) ExecuteResultValue); + if (e1.getAttribute("name").toString().equals((String) ExecuteResultValue)) { + view.setJsp(node1.getFirstChild().getNodeValue()); + } + } + + return view; + + + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + /* + + 0. 读取配置文件struts.xml + + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group16/2562124714/src/com/coderising/litestruts/View.java b/group16/2562124714/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/2562124714/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/2562124714/src/com/coding/basic/ArrayList.java b/group16/2562124714/src/com/coding/basic/ArrayList.java index f1d5a9fdd9..acdfadd83d 100644 --- a/group16/2562124714/src/com/coding/basic/ArrayList.java +++ b/group16/2562124714/src/com/coding/basic/ArrayList.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.Objects; + public class ArrayList implements List { private int size = 0; @@ -96,5 +98,22 @@ public int size(){ public Iterator iterator(){ return null; } + + public Object[] ToArray() + { + Object [] Array = new Object[this.size]; + if(this.size == 0) + { + return new Object[0]; + } + + //使用System.arraycopy()来复制数组是更优的办法 zwj 20170309 + for (int i = 0 ; i < this.size; i ++) + { + Array[i] = this.elementData[i]; + } + + return Array; + } } diff --git a/group16/2816977791/thirdExercise/src/DownloadThread.java b/group16/2816977791/thirdExercise/src/DownloadThread.java new file mode 100644 index 0000000000..025fcff47b --- /dev/null +++ b/group16/2816977791/thirdExercise/src/DownloadThread.java @@ -0,0 +1,34 @@ +import api.Connection; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread { + + Connection conn; + int startPos; + int endPos; + + private CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos, CyclicBarrier barrier) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.barrier = barrier; + } + + public void run() { + try { + byte[] buffer = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile("/Users/nvarchar/example.jpg", "rw"); + raf.seek(startPos); + raf.write(buffer); + raf.close(); + barrier.await(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group16/2816977791/thirdExercise/src/FileDownloader.java b/group16/2816977791/thirdExercise/src/FileDownloader.java new file mode 100644 index 0000000000..5cf6b374d6 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/FileDownloader.java @@ -0,0 +1,74 @@ +import api.Connection; +import api.ConnectionException; +import api.ConnectionManager; +import api.DownloadListener; + +import java.util.concurrent.CyclicBarrier; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int THREAD_NUM = 10; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() { + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = null; + try { + //(1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + conn = cm.open(this.url); + + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + int length = conn.getContentLength(); + + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + int start = 0; + int endPos = 0; + for (int i = 0; i < THREAD_NUM; i++) { + endPos = start + length / THREAD_NUM; + System.out.println(start + "=====" + endPos); + new DownloadThread(conn, start, endPos > (length - 1) ? length - 1 : endPos, barrier).start(); + start = endPos + 1; + } + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group16/2816977791/thirdExercise/src/FileDownloaderTest.java b/group16/2816977791/thirdExercise/src/FileDownloaderTest.java new file mode 100644 index 0000000000..250fe011b7 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/FileDownloaderTest.java @@ -0,0 +1,57 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import api.ConnectionManager; +import api.DownloadListener; +import impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://cdn.pixabay.com/photo/2017/03/31/15/34/sunset-2191645_1280.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + } + +} diff --git a/group16/2816977791/thirdExercise/src/api/Connection.java b/group16/2816977791/thirdExercise/src/api/Connection.java new file mode 100644 index 0000000000..828332de3b --- /dev/null +++ b/group16/2816977791/thirdExercise/src/api/Connection.java @@ -0,0 +1,23 @@ +package api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group16/2816977791/thirdExercise/src/api/ConnectionException.java b/group16/2816977791/thirdExercise/src/api/ConnectionException.java new file mode 100644 index 0000000000..60a2043e44 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/api/ConnectionException.java @@ -0,0 +1,5 @@ +package api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/2816977791/thirdExercise/src/api/ConnectionManager.java b/group16/2816977791/thirdExercise/src/api/ConnectionManager.java new file mode 100644 index 0000000000..efb2f2ab6b --- /dev/null +++ b/group16/2816977791/thirdExercise/src/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group16/2816977791/thirdExercise/src/api/DownloadListener.java b/group16/2816977791/thirdExercise/src/api/DownloadListener.java new file mode 100644 index 0000000000..b8ab21d462 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/api/DownloadListener.java @@ -0,0 +1,5 @@ +package api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/2816977791/thirdExercise/src/basic/Iterator.java b/group16/2816977791/thirdExercise/src/basic/Iterator.java new file mode 100644 index 0000000000..9570e2792d --- /dev/null +++ b/group16/2816977791/thirdExercise/src/basic/Iterator.java @@ -0,0 +1,11 @@ +package basic; + +/** + * @author nvarchar + * date 2017/3/27 + */ +public interface Iterator { + boolean hasNext(); + + Object next(); +} diff --git a/group16/2816977791/thirdExercise/src/basic/LinkedList.java b/group16/2816977791/thirdExercise/src/basic/LinkedList.java new file mode 100644 index 0000000000..742ed954ba --- /dev/null +++ b/group16/2816977791/thirdExercise/src/basic/LinkedList.java @@ -0,0 +1,378 @@ +package basic; + +import java.util.NoSuchElementException; + +/** + * @author nvarchar + * date 2017/3/27 + */ +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + addLast(o); + } else if (index == 0) { + addFirst(o); + } else { + Node node = node(index - 1); + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } + } + + public Object get(int index) { + checkPositionIndex(index); + return node(index).data; + } + + public Object remove(int index) { + checkPositionIndex(index); + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } else { + Node newNode = node(index); + Node prevNode = node(index - 1); + prevNode.next = newNode.next; + size--; + return newNode.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node first = head; + Node newNode = new Node(o, first); + head = newNode; + if (first == null) { + tail = newNode; + } + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + Node last = tail; + tail = newNode; + if (last == null) { + head = newNode; + } else { + last.next = newNode; + } + size++; + } + + public Object removeFirst() { + Node first = head; + if (first == null) { + throw new NoSuchElementException(); + } else { + Node next = first.next; + if (next == null) { + head = null; + tail = null; + } else { + head = next; + } + size--; + return first.data; + } + } + + public Object removeLast() { + Node last = tail; + if (last == null) { + throw new NoSuchElementException(); + } else { + if (size == 1) { + head = null; + tail = null; + } else { + tail = node(size - 2); + tail.next = null; + } + size--; + return last.data; + } + } + + public Iterator iterator() { + return new Iterator() { + private int nextIndex; + private Node node; + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } else { + nextIndex++; + if (node == null) { + node = head; + return node.data; + } else { + node = node.next; + return node.data; + } + } + } + }; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException(); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private Node node(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Iterator iterator = iterator(); + LinkedList list = new LinkedList(); + while (iterator.hasNext()) { + list.addFirst(iterator.next()); + } + this.head = list.head; + this.tail = list.tail; + this.size = list.size; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int count = size / 2; + if (count == 0) { + return; + } + Node newNode = node(count); + head = newNode; + size = size - count; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + checkPositionIndex(i); + checkPositionIndex(i + length); + for (int j = i; j < i + length; j++) { + remove(j); + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + int[] result = new int[list.size]; + int i = 0; + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + int position = (int) iterator.next(); + if (position >= 0 && position < size) { + int number = (int) get(position); + result[i++] = number; + } + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + LinkedList result = new LinkedList(); + Iterator iterator = iterator(); + Iterator iteratorB = list.iterator(); + while (iterator.hasNext() && iteratorB.hasNext()) { + int number1 = (int) iterator.next(); + int number2 = (int) iteratorB.next(); + while (number1 < number2) { + if (!iterator.hasNext()) { + break; + } + result.add(number1); + number1 = (int) iterator.next(); + } + while (number1 > number2) { + if (!iteratorB.hasNext()) { + break; + } + number2 = (int) iteratorB.next(); + } + } + while (iterator.hasNext()){ + result.add(iterator.next()); + } + head = result.head; + tail = result.tail; + size = result.size; + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + int prev; + int after; + LinkedList result = new LinkedList(); + Iterator iterator = iterator(); + if (iterator.hasNext()) { + prev = (int) iterator.next(); + result.add(prev); + } else { + return; + } + if (iterator.hasNext()) { + after = (int) iterator.next(); + } else { + return; + } + if (prev != after){ + result.add(after); + } + + + while (iterator.hasNext()) { + prev = after; + after = (int) iterator.next(); + if (prev != after) { + result.add(after); + } + } + + head = result.head; + tail = result.tail; + size = result.size; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Iterator iterator = iterator(); + LinkedList result = new LinkedList(); + while (iterator.hasNext()) { + int number = (int) iterator.next(); + if (number <= min || number >= max) { + result.add(number); + } + } + head = result.head; + tail = result.tail; + size = result.size; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList result = new LinkedList(); + Iterator iterator = iterator(); + Iterator iteratorB = list.iterator(); + while (iterator.hasNext() && iteratorB.hasNext()) { + int number1 = (int) iterator.next(); + int number2 = (int) iteratorB.next(); + while (number1 < number2) { + if (!iterator.hasNext()) { + break; + } + number1 = (int) iterator.next(); + } + while (number1 > number2) { + if (!iteratorB.hasNext()) { + break; + } + number2 = (int) iteratorB.next(); + } + if (number1 == number2) { + result.add(number1); + } + } + return result; + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); +// list.addLast(3); +// list.addLast(7); +// list.addLast(10); +// list.reverse(); +// System.out.println(); +// list.addLast(2); +// list.addLast(5); +// list.addLast(7); +// list.addLast(8); +// list.addLast(10); +// list.removeFirstHalf(); +// System.out.println(); + } +} diff --git a/group16/2816977791/thirdExercise/src/basic/LinkedListTest.java b/group16/2816977791/thirdExercise/src/basic/LinkedListTest.java new file mode 100644 index 0000000000..2d4667f822 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/basic/LinkedListTest.java @@ -0,0 +1,147 @@ +package basic; + +import org.junit.Test; + +/** + * @author nvarchar + * date 2017/3/28 + */ +public class LinkedListTest { + + @Test + public void reverse() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(3); + list.addLast(7); + list.addLast(10); + list.reverse(); + System.out.println(); + } + + @Test + public void removeFirstHalf() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(2); + list.addLast(5); + list.addLast(7); + list.addLast(8); + list.addLast(10); + list.removeFirstHalf(); + System.out.println(); + } + + @Test + public void remove() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(2); + list.addLast(5); + list.addLast(7); + list.addLast(8); + list.addLast(10); + list.remove(1, 2); + System.out.println(); + } + + @Test + public void getElements() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(11); + list.addLast(101); + list.addLast(201); + list.addLast(301); + list.addLast(401); + list.addLast(501); + list.addLast(601); + list.addLast(701); + + LinkedList listB = new LinkedList(); + listB.addLast(1); + listB.addLast(3); + listB.addLast(4); + listB.addLast(6); + list.getElements(listB); + + System.out.println(); + } + + @Test + public void subtract() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(11); + list.addLast(101); + list.addLast(201); + list.addLast(301); + list.addLast(401); + list.addLast(501); + list.addLast(601); + list.addLast(701); + + LinkedList listB = new LinkedList(); + listB.addLast(11); + listB.addLast(301); + listB.addLast(401); + listB.addLast(601); + list.subtract(listB); + + System.out.println(); + } + + @Test + public void removeDuplicateValues() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(11); + list.addLast(101); + list.addLast(101); + list.addLast(101); + list.addLast(101); + list.addLast(201); + list.addLast(301); + list.addLast(301); + list.addLast(401); + list.addLast(401); + list.addLast(501); + list.addLast(601); + list.addLast(601); + list.addLast(701); + list.removeDuplicateValues(); + System.out.println(); + } + + @Test + public void removeRange() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(11); + list.addLast(101); + list.addLast(201); + list.addLast(301); + list.addLast(401); + list.addLast(501); + list.addLast(601); + list.addLast(701); + list.removeRange(200, 500); + System.out.println(); + } + + @Test + public void intersection() throws Exception { + LinkedList list = new LinkedList(); + list.addLast(11); + list.addLast(101); + list.addLast(201); + list.addLast(301); + list.addLast(401); + list.addLast(501); + list.addLast(601); + list.addLast(701); + + LinkedList listB = new LinkedList(); + listB.addLast(11); + listB.addLast(301); + listB.addLast(401); + listB.addLast(601); + listB.addLast(901); + list.intersection(listB); + System.out.println(); + } + +} \ No newline at end of file diff --git a/group16/2816977791/thirdExercise/src/basic/List.java b/group16/2816977791/thirdExercise/src/basic/List.java new file mode 100644 index 0000000000..828053574c --- /dev/null +++ b/group16/2816977791/thirdExercise/src/basic/List.java @@ -0,0 +1,17 @@ +package basic; + +/** + * @author nvarchar + * date 2017/3/27 + */ +public interface List { + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group16/2816977791/thirdExercise/src/impl/ConnectionImpl.java b/group16/2816977791/thirdExercise/src/impl/ConnectionImpl.java new file mode 100644 index 0000000000..699c5c6139 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/impl/ConnectionImpl.java @@ -0,0 +1,64 @@ +package impl; + +import api.Connection; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionImpl implements Connection { + + URL url; + + public ConnectionImpl(String urlString) { + try { + url = new URL(urlString); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + ByteArrayOutputStream baos = null; + try { + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream in = conn.getInputStream(); + baos = new ByteArrayOutputStream(); + int len = 0; + byte[] buffer = new byte[1024]; + while ((len = in.read(buffer)) != -1) { + baos.write(buffer, 0, len); + } + in.close(); + baos.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) url.openConnection(); + return conn.getContentLength(); + } catch (IOException e) { + return -1; + } finally { + conn.disconnect(); + } + } + + @Override + public void close() { + + } + +} diff --git a/group16/2816977791/thirdExercise/src/impl/ConnectionManagerImpl.java b/group16/2816977791/thirdExercise/src/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ff364efd92 --- /dev/null +++ b/group16/2816977791/thirdExercise/src/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package impl; + +import api.Connection; +import api.ConnectionException; +import api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection connection = new ConnectionImpl(url); + return connection; + } + +} diff --git a/group16/313001956/.classpath b/group16/313001956/.classpath index b42037dde2..249d4729ec 100644 --- a/group16/313001956/.classpath +++ b/group16/313001956/.classpath @@ -8,5 +8,6 @@ + diff --git a/group16/313001956/src/com/coderising/array/ArrayUtil.java b/group16/313001956/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..158b1bc6df --- /dev/null +++ b/group16/313001956/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,202 @@ + +package com.coderising.array; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; + +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * һa , Ըֵû 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] a = + * [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int size = origin.length; + if (size == 0) { + return; + } + int semi = size / 2; + int temp; + for (int i = 0; i < semi; i++) { + temp = origin[i]; + origin[i] = origin[size - 1 - i]; + origin[size - 1 - i] = temp; + } + } + + /** + * µһ飺 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * ҪֵΪ0ȥΪ0ֵһµ飬ɵΪ {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + ArrayList arrayList = new ArrayList(); + int size = oldArray.length; + for (int i = 0; i < size; i++) { + if (oldArray[i] != 0) + arrayList.add(oldArray[i]); + } + + return arrayListToArray(arrayList); + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + ArrayList arraylist = new ArrayList(); + int size1 = array1.length; + int size2 = array2.length; + int j = 0; + for (int i = 0; i < size1; i++) { + if (j >= size2) + arraylist.add(array1[i]); + else { + for (; j < size2; j++) { + if (array1[i] < array2[j]) { + arraylist.add(array1[i]); + break; + } else if (array1[i] == array2[j]) { + arraylist.add(array2[j]); + j++; + break; + } else { + arraylist.add(array2[j]); + } + } + } + } + return arrayListToArray(arraylist); + } + + private int[] arrayListToArray(ArrayList arraylist) { + int newSize = arraylist.size(); + int[] newArray = new int[newSize]; + for (int i = 0; i < newSize; i++) + newArray[i] = Integer.parseInt(arraylist.get(i).toString()); + return newArray; + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int newsize = oldArray.length + size; + int[] newArray = new int[newsize]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ 磬 max = 15 , + * 򷵻صӦΪ [11235813] max = 1, 򷵻ؿ [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int array[] = null; + ArrayList arraylist = new ArrayList(); + arraylist.add(1); + arraylist.add(1); + if (max == 1) + return null; + int temp = 1; + for (int i = 1; (temp = Integer.parseInt(arraylist.get(i).toString()) + + Integer.parseInt(arraylist.get(i - 1).toString())) <= max; i++) { + + arraylist.add(temp); + } + + return arrayListToArray(arraylist); + } + + /** + * Сڸֵmax max = 23, صΪ[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList al = new ArrayList(); + if (max == 1) { + return null; + } else if (max == 2) { + al.add(2); + } else { + for (int i = 2; i < max; i++) { + for (int j = 2; j <= Math.sqrt(max); j++) { + if (i % j == 0) + break; + } + al.add(i); + } + } + return arrayListToArray(al); + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 һֵmax һ飬 Сmax + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList al = new ArrayList(); + int num = 0; + for (int i = 1; i < max; i++) { + num = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) + num += j; + } + if (num == i) + al.add(i); + } + return arrayListToArray(al); + } + + /** + * seperator array array= [3,8,9], seperator = "-" 򷵻ֵΪ"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String s = ""; + int lenth = array.length; + for (int i = 0; i < lenth; i++) { + if (i == 0) + s += i; + else { + s += seperator + i; + } + } + return s; + } + +} diff --git a/group16/313001956/src/com/coderising/download/DownloadThread.java b/group16/313001956/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..0653f71d80 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,57 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; +import com.coding.basic.ArrayList; + +public class DownloadThread extends Thread { + + Connection conn; + Integer startPos; + Integer endPos; + DownloadListener listener; + File file; + int threadNum; + ArrayList threadDone; + + public DownloadThread(Connection conn, int startPos, int endPos, DownloadListener listener, File file, + Integer threadNum, ArrayList threadDone) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.listener = listener; + this.file = file; + this.threadNum = threadNum; + this.threadDone = threadDone; + // run(); + } + + @Override + public synchronized void run() { + try { + byte[] bt = conn.read(startPos, endPos, file); + + threadDone.add(1); + + if (conn != null) { + conn.close(); + } + if (threadDone.size() == threadNum) { + + listener.notifyFinished(); + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } +} diff --git a/group16/313001956/src/com/coderising/download/FileDownloader.java b/group16/313001956/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..6903506b6b --- /dev/null +++ b/group16/313001956/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,97 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.util.List; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coding.basic.ArrayList; + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() { + // ʵĴ룬 ע⣺ Ҫö߳ʵ + // ӿ, Ҫд⼸ӿڵʵִ + // (1) ConnectionManager , ԴһӣͨConnectionԶȡеһΣstartPos, + // endPosָ + // (2) DownloadListener, Ƕ߳أ Ŀͻ˲֪ʲôʱҪʵֵ + // ̶ִ߳Ժ listenernotifiedFinished ͻ˾յ֪ͨ + // ʵ˼· + // 1. ҪConnectionManageropenӣ + // ȻͨConnection.getContentLengthļij + // 2. 3߳أ עÿ߳ҪȵConnectionManageropen + // Ȼread readжȡļĿʼλúͽλõIJ ֵbyte[] + // 3. byteд뵽ļ + // 4. е̶߳Ժ ҪlistenernotifiedFinished + + // Ĵʾ룬 Ҳ˵ֻһ̣߳ Ҫɶ̵߳ġ + Connection conn = null; + try { + Integer threadNum = 3; + //Integer threadDone = 0; + ArrayList threadDone=new ArrayList(); + conn = cm.open(this.url); + if (conn.getConn().getResponseCode() == 200) { + int length = conn.getContentLength(); + int size = (length % threadNum == 0 ? length / threadNum : length / threadNum + 1); + + String filename = url.substring(url.lastIndexOf('/')); + String filePath = "C:\\Users\\Administrator\\Desktop\\" + filename; + File file = new File(filePath); + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf.setLength(length); + raf.close(); + + for (int i = 0; i < threadNum; i++) { + Connection connThread = cm.open(this.url); + new DownloadThread(connThread, i * size, (i + 1) * size - 1, listener, file, threadNum, + threadDone).start(); + } + } + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + +} diff --git a/group16/313001956/src/com/coderising/download/FileDownloaderTest.java b/group16/313001956/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..cca82ea5da --- /dev/null +++ b/group16/313001956/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + //String url = "http://10.10.1.65:1024/wxl.jpg"; + String url = "http://10.10.1.65:1024/java.pdf"; + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // ȴ߳سִ + while (!downloadFinished) { + try { + System.out.println("ûɣ"); + //5 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("ɣ"); + + + + } + +} diff --git a/group16/313001956/src/com/coderising/download/api/Connection.java b/group16/313001956/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..3a3edf5835 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/Connection.java @@ -0,0 +1,43 @@ +package com.coderising.download.api; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.HttpURLConnection ; + +public interface Connection { + URL fileurl = null; + HttpURLConnection conn = null; + InputStream inStream = null; + + /** + * ʼͽλã ȡݣ ֵֽ + * + * @param startPos + * ʼλã 0ʼ + * @param endPos + * λ + * @return + */ + public byte[] read(int startPos, int endPos,File file) throws IOException; + + /** + * õݵij + * + * @return + */ + public int getContentLength(); + + /** + * ر + */ + public void close(); + + public void setConn(HttpURLConnection conn); + public void setFileurl(URL fileurl); + public HttpURLConnection getConn(); + public URL getFileurl(URL fileurl) ; + public void setinStream(InputStream inStream); + public InputStream getinStream(); +} diff --git a/group16/313001956/src/com/coderising/download/api/ConnectionException.java b/group16/313001956/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/313001956/src/com/coderising/download/api/ConnectionManager.java b/group16/313001956/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group16/313001956/src/com/coderising/download/api/DownloadListener.java b/group16/313001956/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java b/group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..94fd2c9b67 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,80 @@ +package com.coderising.download.impl; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.URL; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection { + URL fileurl = null; + HttpURLConnection uRLconn = null; + InputStream inStream = null; + + @Override + public byte[] read(int startPos, int endPos, File file) throws IOException { + uRLconn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + if (inStream == null) + inStream = uRLconn.getInputStream(); + int size = endPos - startPos + 1; + + byte[] bt = new byte[size]; + + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + raf.seek(startPos); + int lenth=0; + //lenth = inStream.read(bt,0,size); + while ((lenth = inStream.read(bt,0,size)) != -1) + raf.write(bt, 0, lenth); + raf.close(); + + return bt; + + } + + @Override + public int getContentLength() { + int fileSize = uRLconn.getContentLength(); + return fileSize; + } + + @Override + public void close() { + if (inStream != null) + try { + inStream.close(); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setConn(HttpURLConnection uRLconn) { + this.uRLconn = uRLconn; + } + + public HttpURLConnection getConn() { + return this.uRLconn; + } + + public void setFileurl(URL fileurl) { + this.fileurl = fileurl; + } + + public URL getFileurl(URL fileurl) { + return this.fileurl; + } + + public void setinStream(InputStream inStream) { + this.inStream = inStream; + } + + public InputStream getinStream() { + return this.inStream; + } +} diff --git a/group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ff96aaa595 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,35 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + try { + URL fileurl=new URL(url); + HttpURLConnection uRlconn = (HttpURLConnection)fileurl.openConnection(); + //ӵ + uRlconn.setRequestMethod("GET"); + uRlconn.setReadTimeout(5000); + Connection conn = new ConnectionImpl(); + conn.setFileurl(fileurl); + + conn.setConn(uRlconn); + return conn; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group16/313001956/src/com/coderising/jvm/loader/ClassFileLoader.java b/group16/313001956/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..2b7607a09e --- /dev/null +++ b/group16/313001956/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,56 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + static final int BUFF_SIZE=1024; + + public byte[] readBinaryCode(String className) { + byte[] barray = new byte[BUFF_SIZE]; + try { + + String pathname = clzPaths.get(0) + "\\" + className.replace('.', '\\')+".class"; + File file = new File(pathname); + InputStream in = new FileInputStream(file); + int byteread = 0; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while ((byteread = in.read(barray)) != -1) { + baos.write(barray, 0, byteread); + } + return baos.toByteArray(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + int clzsize = clzPaths.size(); + String str = ""; + if (clzsize > 0) { + for (int i = 0; i < clzsize; i++) { + str += clzPaths.get(i); + if (i < clzsize - 1) { + str += ";"; + } + } + } + return str; + } + +} diff --git a/group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..6edfd64a18 --- /dev/null +++ b/group16/313001956/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,91 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + //static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path1 = "D:\\Java2017\\GitHub\\coding2017\\group16\\313001956\\build\\classes"; + static String path2 = "C:\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // ע⣺ֽܺJVM汾йϵ Կõൽж + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i parameters) { + + /* + * + * 0. ȡļstruts.xml + * + * 1. actionNameҵӦclass LoginAction, ͨʵ + * parametersеݣösetter parametersе ("name"="test" , + * "password"="1234") , ǾӦõ setNamesetPassword + * + * 2. ͨöexectue ÷ֵ"success" + * + * 3. ͨҵgetter getMessage, ͨã ֵγһHashMap , + * {"message": "¼ɹ"} , ŵViewparameters + * + * 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + * ŵViewjspֶС + * + */ + View view = new View(); + Map map = new HashMap(); + view.setParameters(map); + try { + + SAXReader reader = new SAXReader(); + String dir = System.getProperty("user.dir"); + + Document document = reader.read(new File(dir + "/src/com/coderising/litestruts/struts.xml")); + Element struts = document.getRootElement(); + java.util.List list_action = struts.elements("action"); + + Element item = null; + for (int i = 0; i < list_action.size(); i++) { + item = list_action.get(i); + String nm = item.attributeValue("name"); + if (actionName.equals(nm)) { + break; + } + } + String str_class = item.attributeValue("class"); + // String real_class=dir+"/"+str_class.replace('.', '/'); + // Class cl = Class.forName( dir.replace('\\', + // '.')+".src."+str_class); + Class cl = Class.forName(str_class); + Object instance = cl.newInstance(); + + String dNmae = parameters.get("name"); + String dpassword = parameters.get("password"); + Method mName = cl.getMethod("setName", String.class); + Method mPassword = cl.getMethod("setPassword", String.class); + mName.invoke(instance, dNmae); + mPassword.invoke(instance, dpassword); + + Method mExectue = cl.getMethod("execute"); + Object result = mExectue.invoke(instance); + + Method[] methods = cl.getMethods(); + for (Method method : methods) { + if (isGetter(method)) { + String mGettername = method.getName().substring(3); + Object mResult = method.invoke(instance); + view.getParameters().put(mGettername.toLowerCase(), mResult); + } + } + + java.util.List resulList = item.elements(); + for (Element el : resulList) { + if (result.toString().equals(el.attributeValue("name"))) { + view.setJsp(el.getTextTrim()); + break; + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + return view; + } + + // жǷgetter + public static boolean isGetter(Method method) { + if (!method.getName().startsWith("get")) + return false; + if (method.getParameterTypes().length != 0) + return false; + if (void.class.equals(method.getReturnType())) + return false; + return true; + } + +} diff --git a/group16/313001956/src/com/coderising/litestruts/StrutsTest.java b/group16/313001956/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9e98836f5f --- /dev/null +++ b/group16/313001956/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //ԤIJһ + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/313001956/src/com/coderising/litestruts/View.java b/group16/313001956/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f1e7fcfa19 --- /dev/null +++ b/group16/313001956/src/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index 3bec144013..03d2547c30 100644 Binary files a/group16/313001956/src/com/coding/basic/ArrayList.java and b/group16/313001956/src/com/coding/basic/ArrayList.java differ diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java index de886c9084..483ca7ac44 100644 Binary files a/group16/313001956/src/com/coding/basic/LinkedList.java and b/group16/313001956/src/com/coding/basic/LinkedList.java differ diff --git a/group16/313001956/src/com/coding/basic/LinkedListTest.java b/group16/313001956/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2acd774160 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +public class LinkedListTest { + + @Test + public final void testReverse() { + + LinkedList list=new LinkedList(); + list.add(3); + list.add(7); + list.add(10); + + LinkedList testlist=new LinkedList(); + testlist.add(10); + testlist.add(7); + testlist.add(3); + + list.reverse(list); + Assert.assertEquals(list.size(), testlist.size()); + Assert.assertEquals(list.get(0), testlist.get(0)); + Assert.assertEquals(list.get(1), testlist.get(1)); + Assert.assertEquals(list.get(2), testlist.get(2)); + } + +} diff --git a/group16/313001956/src/com/coding/basic/linklist/LRUPageFrame.java b/group16/313001956/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..e689ae03ce --- /dev/null +++ b/group16/313001956/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,135 @@ +package com.coding.basic.linklist; + +/** + * ˫ʵLRU㷨 + * + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private Node first;// ͷ + private Node last;// β + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + first = null; + last = null; + } + + /** + * ȡж + * + * @param key + * @return + */ + public void access(int pageNum) { + Node node = new Node(); + node.prev = null; + node.pageNum = pageNum; + + if (first == null) { + node.next = null; + first = node; + return; + } + if(judgeEqual(node, pageNum)){ + return; + } + + node.next = first; + first.prev = node; + first = node; + + if (last == null) { + judgeFull(); + } else { + Node temp = last.prev; + + last.prev = null; + last.next = null; + last.pageNum = 0; + + temp.next = null; + last = temp; + } + } + + private boolean judgeEqual(Node node, int pageNum) { + if (first.pageNum == pageNum) { + return true; + } + Node nd = first; + while (nd != null) { + if (nd.pageNum == pageNum) { + if (nd.next != null) { + nd.prev.next = nd.next; + nd.next.prev = nd.prev; + nd.prev = null; + nd.next = first; + first = nd; + } else { + if (last != null) { + last = nd.prev; + } + nd.prev.next = null; + + nd.prev = null; + nd.next = first; + first.prev=nd; + first = nd; + } + + return true; + } + nd = nd.next; + } + return false; + } + + // жǷ˲last + private void judgeFull() { + int count = 0; + Node node = first; + while (node != null) { + count++; + if (count == this.capacity) { + last = node; + return; + } + node = node.next; + } + + if (count >= this.capacity) { + + } + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..d1e58e2405 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,35 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(0); + Assert.assertEquals("0,4,3", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,4", frame.toString()); + } + +} diff --git a/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java index 5fd5f1efba..2c1fca67d4 100644 --- a/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java +++ b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java @@ -26,16 +26,21 @@ public static void reverseArray(int[] origin){ */ public static int[] removeZero(int[] oldArray){ - for(int i = 0;i < oldArray.length ;i++){ + int zeroCount = 0; + for(int i = 0;i < oldArray.length; i++){ if(oldArray[i] == 0){ - int[] a = {}; - System.arraycopy(oldArray, 0, a, 0, i); - System.arraycopy(oldArray, 0, a, i, oldArray.length); - oldArray = a; - removeZero(oldArray); + zeroCount++; } } - return oldArray; + int[] newArr = new int[oldArray.length - zeroCount]; + int index = 0; + for(int i = 0;i < oldArray.length; i++){ + if(oldArray[i] != 0){ + newArr[index] = oldArray[i]; + index++; + } + } + return newArr; } /** @@ -47,7 +52,45 @@ public static int[] removeZero(int[] oldArray){ */ public static int[] merge(int[] array1, int[] array2){ - return null; + //先对数组进行去重,记录重复的索引,后将两个数组合并,再进行排序 + int[] repeatedNum = new int[array1.length + array2.length]; + int repeatedCount = 0; + for(int i = 0;i < array1.length; i++){ + for(int j = 0;j < array2.length; j++){ + if(array1[i] == array2[j]){ + repeatedNum[repeatedCount] = array1[i]; + repeatedCount++; + } + } + } + int [] combineArr = new int[array1.length + array2.length - repeatedCount]; + for(int i = 0;i < array1.length; i++){ + combineArr[i] = array1[i]; + } + for(int i = 0;i < array2.length; i++){ + int index = array1.length -1; + boolean same = false; + for(int j = 0;j < repeatedNum.length; j++){ + if(array2[i] == repeatedNum[j]){ + same = true; + } + } + if(!same){ + index += 1; + combineArr[index] = array2[i]; + } + } + //冒泡排序 + for(int i = 0;i < combineArr.length;i++){ + for(int j = i + 1;j < combineArr.length;j++){ + if(combineArr[i] > combineArr[j]){ + int x = combineArr[i]; + combineArr[i] = combineArr[j]; + combineArr[j] = x; + } + } + } + return combineArr; } /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size @@ -59,7 +102,11 @@ public static int[] merge(int[] array1, int[] array2){ * @return */ public static int[] grow(int [] oldArray, int size){ - return null; + int[] newArr = new int[oldArray.length + size]; + for(int i = 0;i < oldArray.length; i++){ + newArr[i] = oldArray[i]; + } + return newArr; } /** @@ -70,7 +117,31 @@ public static int[] grow(int [] oldArray, int size){ * @return */ public static int[] fibonacci(int max){ - return null; + if(max == 1){ + return null; + }else{ + int length = 0; + int dataBefore = 0; + int dataAfter = 1; + while(dataAfter < max){ + int date = dataAfter; + dataAfter = dataAfter + dataBefore; + dataBefore = date; + length++; + } + int index = 0; + int[] result = new int[length]; + dataBefore = 0; + dataAfter = 1; + while(dataAfter < max){ + result[index] = dataAfter; + int date = dataAfter; + dataAfter = dataAfter + dataBefore; + dataBefore = date; + index ++; + } + return result; + } } /** @@ -80,6 +151,12 @@ public static int[] fibonacci(int max){ * @return */ public static int[] getPrimes(int max){ + int i = 1; + int length = 0; + while(i < max){ + i++; + int search = 1; + } return null; } @@ -102,7 +179,14 @@ public static int[] getPerfectNumbers(int max){ * @return */ public static String join(int[] array, String seperator){ - return null; + StringBuilder sb = new StringBuilder(); + for(int i=0 ;i < array.length; i++){ + sb.append(String.valueOf(array[i])); + if(i != array.length - 1){ + sb.append(seperator); + } + } + return sb.toString(); } public static void main(String[] args) { @@ -111,9 +195,29 @@ public static void main(String[] args) { for (int i : a) { System.out.print(i+","); }*/ - int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; - removeZero(oldArr); - for (int i : oldArr) { + /*int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; + int[] newArr = removeZero(oldArr); + for (int i : newArr) { + System.out.print(i+","); + }*/ + /*int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] merge = merge(a1,a2); + for (int i : merge) { + System.out.print(i+","); + }*/ + /*int[] oldArray = {2,3,6}; + int size = 3; + int[] newArr = grow(oldArray, size); + for (int i : newArr) { + System.out.print(i+","); + }*/ + /*int[] array= {3,8,9}; + String seperator = "-"; + String join = join(array, seperator); + System.out.println(join);*/ + int[] fibonacci = fibonacci(15); + for (int i : fibonacci) { System.out.print(i+","); } } diff --git a/group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..e5ddb476a6 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/DownloadThread.java b/group16/420355244/Homework3/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/FileDownloader.java b/group16/420355244/Homework3/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/FileDownloaderTest.java b/group16/420355244/Homework3/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/api/Connection.java b/group16/420355244/Homework3/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionManager.java b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java b/group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionImpl.java b/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group16/420355244/Homework3/src/com/coderising/litestruts/LoginAction.java b/group16/420355244/Homework3/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/420355244/Homework3/src/com/coderising/litestruts/Struts.java b/group16/420355244/Homework3/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group16/420355244/Homework3/src/com/coderising/litestruts/StrutsTest.java b/group16/420355244/Homework3/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/420355244/Homework3/src/com/coderising/litestruts/View.java b/group16/420355244/Homework3/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/ArrayList.java b/group16/420355244/Homework3/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/Iterator.java b/group16/420355244/Homework3/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/LinkedList.java b/group16/420355244/Homework3/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..4fdb03db8a --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + + + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/List.java b/group16/420355244/Homework3/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/Queue.java b/group16/420355244/Homework3/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/Stack.java b/group16/420355244/Homework3/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group16/502059278/src/cn/mark/work0219/MyLinkedList.java b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java index 896d7bcb79..22d0027941 100644 --- a/group16/502059278/src/cn/mark/work0219/MyLinkedList.java +++ b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java @@ -7,23 +7,25 @@ public class MyLinkedList implements MyList{ private Node head; + private Node last; private int size;//集合的长度 + + public MyLinkedList(){ + this.head = new Node(null); + } /** * 添加元素 */ @Override public boolean add(Object o) { - //为空判断 - if ( o == null ){ - System.out.println("不允许null的元素插入!"); - return false; - } - if(head == null){ - head = new Node(); - head.data = o; - }else{ - + if (this.last == null){ + this.last = new Node(o); + this.last.pre = this.head; + this.last.next = this.last; + } else { + Node oldLast = this.last; + this.last = new Node(o); } return false; @@ -49,13 +51,18 @@ public Object remove(int index) { @Override public int size() { - // TODO Auto-generated method stub - return 0; + return this.size; } private static class Node{ Object data; + Node pre; Node next; + + + Node(Object data){ + this.data = data; + } } diff --git a/group16/502059278/src/cn/mark/work0312/MutiDownload.java b/group16/502059278/src/cn/mark/work0312/MutiDownload.java new file mode 100644 index 0000000000..116d81267b --- /dev/null +++ b/group16/502059278/src/cn/mark/work0312/MutiDownload.java @@ -0,0 +1,163 @@ +package cn.mark.work0312; + +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * 多线程下载 + */ +public class MutiDownload { + /*线程数*/ + private static final int THREAD_COUNT = 5; + /*下载资源*/ + private static final String DOWNLOAD_URL = "http://cn.bing.com/az/hprichbg/rb/PlungeDiving_ZH-CN11143756334_1920x1080.jpg"; + /*下载位置*/ + private static final String FILE_NAME = "D:/down.jpg"; + + public static void main(String[] args) { + //文件大小 + long fileSize; + HttpURLConnection connection = null; + try{ + //打开一个链接 + connection = (HttpURLConnection) new URL(DOWNLOAD_URL).openConnection(); + //设置请求方式 + connection.setRequestMethod("GET"); + //连接超时 + connection.setConnectTimeout(8000); + //读取超时 + connection.setReadTimeout(8000); + + if ( connection.getResponseCode() == 200 ){//请求成功返回200 + //文件大小 + fileSize = connection.getContentLength(); + //每个线程要读取的块 + long eachSize = fileSize / THREAD_COUNT; + + //打开一个RandomAccessFile文件,打开方式为读写(rw) + RandomAccessFile raf = new RandomAccessFile(FILE_NAME,"rw"); + //setLength是先在存储设备占用一块空间,防止下载到一半空间不足 + raf.setLength(fileSize); + raf.close(); + + /*创建线程开始下载*/ + for ( int i =0; i size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(size, o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator{ + private LinkedList list; + private int position; + + public LinkedListIterator(LinkedList list) { + this.list=list; + } + + @Override + public boolean hasNext() { + if (position+1>size()){ + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + + } + + @Override + public String toString(){ + for (int i = 0; i 7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Stack stack=new Stack<>(); + while (size()>0) { + stack.add(remove(0)); + } + + while (!stack.isEmpty()) { + this.add(stack.pop()); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + for (int i = 0; i < size()/2; i++) { + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + checkPositionIndex(i); + checkPositionIndex(i+length-1); + + for (int j = 0; j < length; j++) { + remove(i); + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result=new int[list.size]; + + for (int i = 0; i < list.size; i++) { + result[i]=(int)get((Integer)list.get(i)); + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * @param list + */ + + public void subtract(LinkedList list){ + int k=0; + for (int i = size()-1; i >=0; i--) { + + for (int j = k; j < list.size(); j++) { + if (get(i).equals(list.get(j))) { + remove(i); + k=j; + break; + } + } + + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + for (int i = size()-2; i >=0; i--) { + if (get(i).equals(get(i+1))) { + remove(i); + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int start=-1; + int end=-1; + for (int i = 0; i < size(); i++) { + if ((int)get(i)>min) { + start=i; + break; + } + } + for (int i = size()-1; i >=0; i--) { + if ((int)get(i)101->201->301->401->501->601->701 + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + LinkedList listB=new LinkedList(); + listB.add(1); + listB.add(3); + listB.add(4); + listB.add(6); + + System.out.println(Arrays.toString(list.getElements(listB))); + } + + //removeRange + @Test + public void subtract(){ + LinkedList list=new LinkedList(); + // 11->101->201->301->401->501->601->701 + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + LinkedList listB=new LinkedList(); + listB.add(201); + listB.add(601); + listB.add(401); + + list.subtract(listB); + list.toString(); + } + + @Test + public void removeDuplicateValues(){ + LinkedList list=new LinkedList(); + // 11->101->201->301->401->501->601->701 + list.add(11); + list.add(101); + list.add(301); + list.add(301); + list.add(401); + list.add(401); + list.add(601); + list.add(701); + + list.removeDuplicateValues(); + list.toString(); + } + + //intersection + @Test + public void removeRange(){ + LinkedList list=new LinkedList(); + // 11->101->201->301->401->501->601->701 + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + list.removeRange(800,900); + list.toString(); + } + + @Test + public void intersection(){ + LinkedList list=new LinkedList(); + // 11->101->201->301->401->501->601->701 + list.add(11); + list.add(101); + list.add(201); + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + + LinkedList listB=new LinkedList(); + listB.add(22); + listB.add(201); + listB.add(401); + listB.add(601); + listB.add(801); + + list.intersection(listB).toString(); + } +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java index 6a94ded0a9..c0cce5af0a 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java @@ -4,8 +4,6 @@ public class LinkedList implements List { private Node head; private int size = 0; - private Iterator iterator = new LinkedListIterator(); - public void add(Object o) { Node newNode = new Node(o, null); @@ -98,8 +96,8 @@ public Object removeLast() { return remove(size - 1); } - public Iterator iterator() { - return iterator; + public LinkedListIterator iterator() { + return new LinkedListIterator(head); } private void checkIndex(int index) { @@ -114,6 +112,22 @@ private void checkForAdd(int index) { } } + + @Override + public String toString() { + Iterator iterator = iterator(); + StringBuilder builder = new StringBuilder("["); + while ((iterator.hasNext())) { + builder.append(iterator.next()).append(','); + } + if (size() > 0) { + builder.deleteCharAt(builder.length() - 1); + } + return builder + .append(']') + .toString(); + } + /** * 把该链表逆置 * 例如链表为 3->7->10 , 逆置后变为 10->7->3 @@ -122,17 +136,18 @@ public void reverse() { if (size == 0) { return; } - Node[] nodes = new Node[size]; + Object[] datas = new Object[size]; int i = 0; // 迭代链表的数据生成数组 + Iterator iterator = iterator(); while (iterator.hasNext()) { - nodes[i++] = (Node) iterator.next(); + datas[i++] = iterator.next(); } // 遍历数组越生成新的 链表 - Node newHead = nodes[--i]; - Node next = newHead.next; + Node newHead = new Node(datas[--i], null); + Node next = newHead; for (int j = --i; j >= 0; j--) { - next.next = nodes[j]; + next.next = new Node(datas[j], null); next = next.next; } @@ -146,9 +161,23 @@ public void reverse() { * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 */ public void removeFirstHalf() { + removeFirstSize(size >> 1); + } + public void removeFirstSize(int firstSize) { + firstSize = firstSize > size() ? size() : firstSize; + LinkedListIterator iterator = iterator(); + int i = 1; + while (i++ <= firstSize) { + iterator.nextNode(); + } + if (size > 0) { + head = iterator.nextNode(); + size = size() - firstSize; + } } + /** * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 * @@ -156,6 +185,26 @@ public void removeFirstHalf() { * @param length */ public void remove(int i, int length) { + if (i == 0) { + removeFirstSize(length); + return; + } + if (i >= size || length == 0) { + return; + } + + int lastLenth = size - i; + length = length <= lastLenth ? length : lastLenth; + Node pre = node(i - 1); + int j = 0; + + Node next = pre; + while (j++ < length) { + next = next.next; + } + pre.next = next.next; + size = size - length; + } @@ -169,9 +218,44 @@ public void remove(int i, int length) { * @param list */ public int[] getElements(LinkedList list) { - return null; + if (size() == 0) { + return new int[0]; + } + Iterator iterator = list.iterator(); + Node fromNode = iterator().nextNode(); + int fromIndex = 0; + + int[] retArray = new int[list.size()]; + int retIndex = 0; + while (iterator.hasNext()) { + int index = (int) iterator.next(); + Node node = node(fromNode, fromIndex, index); + fromIndex = index; + fromNode = node; + if (node == null) { + return retArray; + } else { + retArray[retIndex++] = (int)node.data; + } + } + return retArray; + } + + private Node node(Node fromNode, int fromIndex, int index) { + Node next = fromNode; + int nextIndex = fromIndex; + while (next != null && nextIndex < index) { + next = next.next; + nextIndex++; + } + if (nextIndex == index) { + return next; + } else { + return null; + } } + /** * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 * 从当前链表中中删除在listB中出现的元素 @@ -229,6 +313,13 @@ private class LinkedListIterator implements Iterator { private Node next; + public LinkedListIterator() { + } + + private LinkedListIterator(Node next) { + this.next = next; + } + @Override public boolean hasNext() { return next != null; @@ -243,5 +334,15 @@ public Object next() { next = next.next; return ret.data; } + + + private Node nextNode() { + if (next == null) { + throw new IndexOutOfBoundsException("there is no node in list"); + } + Node ret = next; + next = next.next; + return ret; + } } } diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java index 396b1f6416..0a09990083 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java @@ -6,4 +6,5 @@ public interface List { public Object get(int index); public Object remove(int index); public int size(); + public Iterator iterator(); } diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java deleted file mode 100644 index 95f0085b66..0000000000 --- a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.coding.api; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Created by luoziyihao on 2/25/17. - */ -public class ArrayListTest { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Test - public void testAdd(){ - List list = new ArrayList(Arrays.asList(0, 1, 2, 3)); - logger.info("list={}", list); - list.add(5, 2); - logger.info("list={}", list); - } -} diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java index 06efea8aa0..0424c2945e 100644 --- a/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java @@ -1,16 +1,181 @@ package com.coding.basic; +import org.junit.Assert; import org.junit.Test; -import static org.junit.Assert.*; +import java.util.Arrays; /** * Created by luoziyihao on 3/23/17. */ public class LinkedListTest { + + @Test + public void add() throws Exception { + + } + + @Test + public void add1() throws Exception { + + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + + @Test + public void removeFirstHalf() throws Exception { + LinkedList linkedList = createAndFillLinkedList(0); + linkedList.removeFirstHalf(); + Assert.assertEquals("[]", linkedList.toString()); + } + @Test + public void removeFirstHalf1() throws Exception { + LinkedList linkedList = createAndFillLinkedList(1); + linkedList.removeFirstHalf(); + Assert.assertEquals("[1]", linkedList.toString()); + } + @Test + public void removeFirstHalf2() throws Exception { + LinkedList linkedList = createAndFillLinkedList(2); + linkedList.removeFirstHalf(); + Assert.assertEquals("[2]", linkedList.toString()); + } + @Test + public void removeFirstHalf3() throws Exception { + LinkedList linkedList = createAndFillLinkedList(3); + linkedList.removeFirstHalf(); + Assert.assertEquals("[2,3]", linkedList.toString()); + } + + private LinkedList createAndFillLinkedList() { + return createAndFillLinkedList(4); + } + + private LinkedList createAndFillLinkedList(int length) { + return createAndFillLinkedList(1, length); + } + + private LinkedList createAndFillLinkedList(int start, int length) { + LinkedList linkedList = new LinkedList(); + for (int i = start; i <= length; i++) { + linkedList.add(i); + } + return linkedList; + } + + @Test + public void remove1() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(0, 0); + Assert.assertEquals("[1,2,3,4]", list.toString()); + } + @Test + public void remove2() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(0, 1); + Assert.assertEquals("[2,3,4]", list.toString()); + } + @Test + public void remove3() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 0); + Assert.assertEquals("[1,2,3,4]", list.toString()); + } + @Test + public void remove4() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 1); + Assert.assertEquals("[1,3,4]", list.toString()); + } +@Test + public void remove5() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 3); + Assert.assertEquals("[1]", list.toString()); + } +@Test + public void remove6() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 4); + Assert.assertEquals("[1]", list.toString()); + } +@Test + public void remove7() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 5); + Assert.assertEquals("[1]", list.toString()); + } + + @Test + public void getElements() throws Exception { + LinkedList listA= createAndFillLinkedList(0,8); + LinkedList listB = createAndFillLinkedList(4, 4); + Assert.assertEquals("[4,5,6,7]", Arrays.toString(listA.getElements(listB))); + + } + + @Test + public void subtract() throws Exception { + + } + + @Test + public void removeDuplicateValues() throws Exception { + + } + + @Test + public void removeRange() throws Exception { + + } + + @Test + public void intersection() throws Exception { + + } + @Test public void iterator() throws Exception { + List linkedList = new LinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add("4"); + Assert.assertEquals("[1,2,3,4]", linkedList.toString()); } @Test @@ -21,7 +186,7 @@ public void reverse() throws Exception { linkedList.add("3"); linkedList.add("4"); linkedList.reverse(); - System.out.println(linkedList); + Assert.assertEquals("[4,3,2,1]", linkedList.toString()); } } \ No newline at end of file diff --git a/group17/1204187480/code/homework/parent/pom.xml b/group17/1204187480/code/homework/parent/pom.xml index 7af48c6fed..ace9bf9cc5 100644 --- a/group17/1204187480/code/homework/parent/pom.xml +++ b/group17/1204187480/code/homework/parent/pom.xml @@ -92,4 +92,13 @@ + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.19.1 + + + \ No newline at end of file diff --git a/group17/1264835468/.gitignore b/group17/1264835468/.gitignore index 71f96b185c..1517b9e684 100644 --- a/group17/1264835468/.gitignore +++ b/group17/1264835468/.gitignore @@ -1,5 +1,7 @@ -/bin/ +/bin/ .classpath .project -.gitignore \ No newline at end of file +.gitignore +/.idea/ +.iml diff --git a/group17/1264835468/src/assignment0226/ArrayUtil.java b/group17/1264835468/src/assignment0226/ArrayUtil.java new file mode 100644 index 0000000000..f8be858ad9 --- /dev/null +++ b/group17/1264835468/src/assignment0226/ArrayUtil.java @@ -0,0 +1,183 @@ +package assignment0226; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeSet; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + int mid = origin.length / 2; + for (int i = 0; i < mid; i++) { + int temp = origin[i]; + int reversePosition = origin.length - 1; + origin[i] = origin[reversePosition]; + origin[reversePosition] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + int count = 0; + + for (int i : oldArray) { + if (i != 0) + count++; + } + int[] newArray = new int[count]; + int currentPos = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) + newArray[currentPos++] = oldArray[i]; + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + TreeSet set = new TreeSet<>(); + for (Integer integer : array1) { + set.add(integer); + } + for (Integer integer : array2) { + set.add(integer); + } + int[] result = new int[set.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = set.pollFirst(); + } + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + return Arrays.copyOf(oldArray, size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) + return new int[0]; + List fList = new ArrayList<>(); + fList.add(1); + fList.add(1); + int last = fList.size() - 1; + while (fList.get(last) < max) { + fList.add(fList.get(last) + fList.get(last - 1)); + last++; + } + int[] result = new int[fList.size() - 1]; + for (int i = 0; i < result.length; i++) { + result[i] = fList.get(i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + boolean[] isPrime = new boolean[max]; + List primes = new ArrayList<>(); + for (int i = 0; i < isPrime.length; i++) { + isPrime[i] = true; + } + for (int i = 2; i * i < max; i++) { + for (int j = i; i * j < max; j++) + isPrime[i * j] = false; + } + for (int i = 2; i < isPrime.length; i++) { + if (isPrime[i]) + primes.add(i); + } + int[] result = new int[primes.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = primes.get(i); + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int sum = 0; + ArrayList perfectNumbers = new ArrayList<>(); + for (int i = 1; i < max; i++) { + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) + perfectNumbers.add(i); + sum = 0; + } + + int[] result = new int[perfectNumbers.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = perfectNumbers.get(i); + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i : array) { + stringBuilder.append(i + seperator); + } + stringBuilder.delete(stringBuilder.length() - seperator.length(), stringBuilder.length()); + + return stringBuilder.toString(); + } +} diff --git a/group17/1264835468/src/assignment0226/ArrayUtilTest.java b/group17/1264835468/src/assignment0226/ArrayUtilTest.java new file mode 100644 index 0000000000..1ccd845e1c --- /dev/null +++ b/group17/1264835468/src/assignment0226/ArrayUtilTest.java @@ -0,0 +1,100 @@ +package assignment0226; + +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] array = new int[] {}; + ArrayUtil.reverseArray(array); + assertArrayEquals(new int[] {}, array); + + array = new int[] { 1 }; + ArrayUtil.reverseArray(array); + assertArrayEquals(new int[] { 1 }, array); + + array = new int[] { 1, 2, 3 }; + ArrayUtil.reverseArray(array); + assertArrayEquals(new int[] { 3, 2, 1 }, array); + } + + @Test + public void testRemoveZero() { + int[] array = new int[] {}; + assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); + + array = new int[] { 0 }; + assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); + + array = new int[] { 1 }; + assertArrayEquals(new int[] { 1 }, ArrayUtil.removeZero(array)); + + array = new int[] { 1, 2, 0, 0, 3 }; + assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); + + array = new int[] { 1, 2, 3 }; + assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); + } + + @Test + public void testMerge() { + int[] array1 = { 3, 5, 7, 8 }; + int[] array2 = { 4, 5, 6, 7 }; + assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, ArrayUtil.merge(array1, array2)); + } + + @Test + public void testGrow() { + int[] array = { 3, 5, 7 }; + assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, ArrayUtil.grow(array, 5)); + assertArrayEquals(new int[] { 3, 5, 7 }, ArrayUtil.grow(array, 3)); + } + + @Test + public void testFibonacci() { + assertArrayEquals(new int[] {}, ArrayUtil.fibonacci(1)); + + assertArrayEquals(new int[] { 1, 1 }, ArrayUtil.fibonacci(2)); + + assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, ArrayUtil.fibonacci(15)); + } + + @Test + public void testGetPrimes() { + assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(1)); + + assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(2)); + + assertArrayEquals(new int[] { 2 }, ArrayUtil.getPrimes(3)); + + assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, ArrayUtil.getPrimes(20)); + } + + @Test + public void testGetPerfectNumbers() { + assertArrayEquals(new int[] { 6 }, ArrayUtil.getPerfectNumbers(10)); + + assertArrayEquals(new int[] { 6, 28 }, ArrayUtil.getPerfectNumbers(100)); + + assertArrayEquals(new int[] { 6, 28, 496 }, ArrayUtil.getPerfectNumbers(1000)); + + assertArrayEquals(new int[] { 6, 28, 496, 8128 }, ArrayUtil.getPerfectNumbers(10000)); + + } + + @Test + public void testJoin() { + assertEquals("3-4-5", ArrayUtil.join(new int[] { 3, 4, 5 }, "-")); + + assertEquals("345", ArrayUtil.join(new int[] { 3, 4, 5 }, "")); + + assertEquals("3", ArrayUtil.join(new int[] { 3 }, "")); + + assertEquals("3--4--5", ArrayUtil.join(new int[] { 3, 4, 5 }, "--")); + } + +} diff --git a/group17/1264835468/src/assignment0226/LoginAction.java b/group17/1264835468/src/assignment0226/LoginAction.java new file mode 100644 index 0000000000..652b5359ac --- /dev/null +++ b/group17/1264835468/src/assignment0226/LoginAction.java @@ -0,0 +1,42 @@ +package assignment0226; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group17/1264835468/src/assignment0226/Struts.java b/group17/1264835468/src/assignment0226/Struts.java new file mode 100644 index 0000000000..457b6ad953 --- /dev/null +++ b/group17/1264835468/src/assignment0226/Struts.java @@ -0,0 +1,99 @@ +package assignment0226; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static File configFile = new File("./src/struts.xml");; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + // 0 + XmlParser phraser = new XmlParser(configFile); + String className = phraser.getClassNameByActionName(actionName); + View view = new View(); + try { + // 1 + Class actionClass = Class.forName(className); + Constructor constructor = actionClass.getConstructor(); + Object instance = constructor.newInstance(); + invokeSetters(actionClass, instance, parameters); + + // 2 + String result = invokeExecute(actionClass, instance); + + // 3 + Map getterResult = invokeGetters(actionClass, instance); + view.setParameters(getterResult); + + // 4 + String resultJsp = phraser.getResultJsp(actionName, result); + view.setJsp(resultJsp); + + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException + | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + private static Map invokeGetters(Class actionClass, Object instance) + throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Map result = new HashMap<>(); + for (Method method : actionClass.getDeclaredMethods()) { + if (method.getName().matches("get.*")) { + String fieldName = method.getName().substring(3).toLowerCase(); + String value = (String) (method.invoke(instance)); + result.put(fieldName, value); + } + } + return result; + } + + private static String invokeExecute(Class actionClass, Object instance) throws NoSuchMethodException, + SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method method = actionClass.getDeclaredMethod("execute"); + return (String) method.invoke(instance); + } + + private static void invokeSetters(Class actionClass, Object instance, Map parameters) + throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + for (String fieldName : parameters.keySet()) { + invokeSetter(actionClass, instance, fieldName, parameters.get(fieldName)); + } + } + + private static void invokeSetter(Class actionClass, Object instance, String fieldName, String value) + throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); + Method setter = actionClass.getDeclaredMethod(setterName, String.class); + setter.invoke(instance, value); + } +} diff --git a/group17/1264835468/src/assignment0226/StrutsTest.java b/group17/1264835468/src/assignment0226/StrutsTest.java new file mode 100644 index 0000000000..1e677c38b3 --- /dev/null +++ b/group17/1264835468/src/assignment0226/StrutsTest.java @@ -0,0 +1,38 @@ +package assignment0226; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 密码和预设的不一致 + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/1264835468/src/assignment0226/View.java b/group17/1264835468/src/assignment0226/View.java new file mode 100644 index 0000000000..3bd518e451 --- /dev/null +++ b/group17/1264835468/src/assignment0226/View.java @@ -0,0 +1,26 @@ +package assignment0226; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group17/1264835468/src/assignment2_26/XmlParser.java b/group17/1264835468/src/assignment0226/XmlParser.java similarity index 95% rename from group17/1264835468/src/assignment2_26/XmlParser.java rename to group17/1264835468/src/assignment0226/XmlParser.java index aa34335cb6..4969951082 100644 --- a/group17/1264835468/src/assignment2_26/XmlParser.java +++ b/group17/1264835468/src/assignment0226/XmlParser.java @@ -1,72 +1,72 @@ -package assignment2_26; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -public class XmlParser { - private File file; - List actionElements; - - public XmlParser(File file) { - this.file = file; - actionElements = new ArrayList<>(); - getActionsFromFile(); - } - - public String getClassNameByActionName(String actionName) { - Element action = getElementByActionName(actionName); - return action.getAttribute("class"); - } - - public String getResultJsp(String actionName, String resultName) { - Element action = getElementByActionName(actionName); - NodeList results = action.getChildNodes(); - for (int i = 0; i < results.getLength(); i++) { - Node child = results.item(i); - if (child instanceof Element) { - Element result = (Element) child; - if (result.getAttribute("name").equals(resultName)) - return result.getTextContent(); - } - } - throw new RuntimeException("not found result named:" + resultName); - } - - private Element getElementByActionName(String actionName) { - for (Element element : actionElements) { - if (element.getAttribute("name").equals(actionName)) { - return element; - } - } - throw new RuntimeException("no such element named " + actionName); - } - - private void getActionsFromFile() { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - try { - DocumentBuilder builder = factory.newDocumentBuilder(); - Document document = builder.parse(file); - Element root = document.getDocumentElement(); - NodeList children = root.getChildNodes(); - for (int i = 0; i < children.getLength(); i++) { - Node child = children.item(i); - if (child instanceof Element) - actionElements.add((Element) child); - } - } catch (ParserConfigurationException | SAXException | IOException e) { - e.printStackTrace(); - } - } -} +package assignment0226; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class XmlParser { + private File file; + List actionElements; + + public XmlParser(File file) { + this.file = file; + actionElements = new ArrayList<>(); + getActionsFromFile(); + } + + public String getClassNameByActionName(String actionName) { + Element action = getElementByActionName(actionName); + return action.getAttribute("class"); + } + + public String getResultJsp(String actionName, String resultName) { + Element action = getElementByActionName(actionName); + NodeList results = action.getChildNodes(); + for (int i = 0; i < results.getLength(); i++) { + Node child = results.item(i); + if (child instanceof Element) { + Element result = (Element) child; + if (result.getAttribute("name").equals(resultName)) + return result.getTextContent(); + } + } + throw new RuntimeException("not found result named:" + resultName); + } + + private Element getElementByActionName(String actionName) { + for (Element element : actionElements) { + if (element.getAttribute("name").equals(actionName)) { + return element; + } + } + throw new RuntimeException("no such element named " + actionName); + } + + private void getActionsFromFile() { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(file); + Element root = document.getDocumentElement(); + NodeList children = root.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + if (child instanceof Element) + actionElements.add((Element) child); + } + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group17/1264835468/src/assignment0326/jvm/loader/ClassFileLoader.java b/group17/1264835468/src/assignment0326/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..a5add91113 --- /dev/null +++ b/group17/1264835468/src/assignment0326/jvm/loader/ClassFileLoader.java @@ -0,0 +1,59 @@ +package assignment0326.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/3/30. + */ +public class ClassFileLoader { + + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + String path = className.replace(".", File.separator); + File classFile=null; + for (String p: clzPaths) { + classFile=new File(p+File.separator+path+".class"); + if(classFile.exists()) + break; + } + if(classFile==null) + throw new RuntimeException("no such class file"); + + byte[] bytes=new byte[(int)classFile.length()]; + try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(classFile))){ + bufferedInputStream.read(bytes, 0, bytes.length); + + } catch (IOException e) { + e.printStackTrace(); + } + return bytes; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuilder stringBuilder=new StringBuilder(); + for (int i = 0; i strings=new ArrayList<>(); + for (int i = 0; i <10; i++) { + strings.add(String.valueOf(new Random().nextInt(3000))); + } + System.out.println(strings); + System.out.println(strings.stream().map(s-> s.charAt(0)).sorted().distinct().limit(5).collect(Collectors.toList())); + } + + public int findMinDifference(List timePoints) { + List list=new ArrayList<>(); + + for (String s:timePoints) { + list.add(parse(s)); + } + Collections.sort(list); + int min=Integer.MAX_VALUE; + for (int i = 0; i < list.size()-1; i++) { + min=Math.min(min,Math.min(Math.abs(list.get(i+1)-list.get(i)),24*60-Math.abs(list.get(i+1)-list.get(i)))); + } + return min; + } + + private Integer parse(String s) { + return Integer.parseInt(s.substring(0, 2)) * 60 + Integer.parseInt(s.substring(3, 5)); + } +} diff --git a/group17/1264835468/src/assignment0326/lru/LRUPageFrame.java b/group17/1264835468/src/assignment0326/lru/LRUPageFrame.java new file mode 100644 index 0000000000..fd2f2693c4 --- /dev/null +++ b/group17/1264835468/src/assignment0326/lru/LRUPageFrame.java @@ -0,0 +1,128 @@ +package assignment0326.lru; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + Node prev; + Node next; + int pageNum; + Node() { + } + public Node(int pageNum) { + this.pageNum = pageNum; + } + + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + private int size; + public LRUPageFrame(int capacity) { + if(capacity<=0) + throw new IllegalArgumentException("capacity:"+capacity+" <= 0"); + this.capacity = capacity; + first=null; + last=null; + size=0; + + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + Node target=search(pageNum); + if (target == null) { + target=new Node(pageNum); + linkFirst(target); + }else{ + moveToFirst(target); + } + if(size>capacity){ + removeLast(); + } + } + + private Node search(int pageNum) { + Node f=first; + while (f!=null){ + if (f.pageNum == pageNum) { + return f; + } + f=f.next; + } + return null; + } + + private void linkFirst(Node target) { + if(first==null){ + first=last=target; + }else { + target.next = first; + first.prev = target; + first = target; + } + size++; + } + + private void moveToFirst(Node target) { + if(target==first){ + return; + } + + Node prevOfTarget=target.prev; + prevOfTarget.next=target.next; + + if(target==last) { + last=prevOfTarget; + }else { + target.next.prev = prevOfTarget; + } + + target.next=first; + first.prev=target; + first=target; + + } + + private void removeLast() { + Node prevOfLast=last.prev; + last.prev=null; + last=prevOfLast; + + if(last==null){ + first=null; + }else { + last.next=null; + } + size--; + } + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group17/1264835468/src/assignment0326/lru/LRUPageFrameTest.java b/group17/1264835468/src/assignment0326/lru/LRUPageFrameTest.java new file mode 100644 index 0000000000..7a54f54c8d --- /dev/null +++ b/group17/1264835468/src/assignment0326/lru/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package assignment0326.lru; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by Administrator on 2017/3/29. + */ + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group17/1264835468/src/assignment2_26/ArrayUtil.java b/group17/1264835468/src/assignment2_26/ArrayUtil.java deleted file mode 100644 index 89d2a0db29..0000000000 --- a/group17/1264835468/src/assignment2_26/ArrayUtil.java +++ /dev/null @@ -1,183 +0,0 @@ -package assignment2_26; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.TreeSet; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = - * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * - * @param origin - * @return - */ - public static void reverseArray(int[] origin) { - int mid = origin.length / 2; - for (int i = 0; i < mid; i++) { - int temp = origin[i]; - int reversePosition = origin.length - 1; - origin[i] = origin[reversePosition]; - origin[reversePosition] = temp; - } - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} - * - * @param oldArray - * @return - */ - - public static int[] removeZero(int[] oldArray) { - int count = 0; - for (int i : oldArray) { - if (i != 0) - count++; - } - int[] newArray = new int[count]; - int currentPos = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) - newArray[currentPos++] = oldArray[i]; - } - return newArray; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = - * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * - * @param array1 - * @param array2 - * @return - */ - - public static int[] merge(int[] array1, int[] array2) { - TreeSet set = new TreeSet<>(); - for (Integer integer : array1) { - set.add(integer); - } - for (Integer integer : array2) { - set.add(integer); - } - int[] result = new int[set.size()]; - for (int i = 0; i < result.length; i++) { - result[i] = set.pollFirst(); - } - return result; - } - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param size - * @return - */ - public static int[] grow(int[] oldArray, int size) { - return Arrays.copyOf(oldArray, size); - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , - * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] - * - * @param max - * @return - */ - public static int[] fibonacci(int max) { - if (max <= 1) - return new int[0]; - List fList = new ArrayList<>(); - fList.add(1); - fList.add(1); - int last = fList.size() - 1; - while (fList.get(last) < max) { - fList.add(fList.get(last) + fList.get(last - 1)); - last++; - } - int[] result = new int[fList.size() - 1]; - for (int i = 0; i < result.length; i++) { - result[i] = fList.get(i); - } - return result; - } - - /** - * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * - * @param max - * @return - */ - public static int[] getPrimes(int max) { - boolean[] isPrime = new boolean[max]; - List primes = new ArrayList<>(); - for (int i = 0; i < isPrime.length; i++) { - isPrime[i] = true; - } - for (int i = 2; i * i < max; i++) { - for (int j = i; i * j < max; j++) - isPrime[i * j] = false; - } - for (int i = 2; i < isPrime.length; i++) { - if (isPrime[i]) - primes.add(i); - } - int[] result = new int[primes.size()]; - for (int i = 0; i < result.length; i++) { - result[i] = primes.get(i); - } - return result; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * - * @param max - * @return - */ - public static int[] getPerfectNumbers(int max) { - int sum = 0; - ArrayList perfectNumbers = new ArrayList<>(); - for (int i = 1; i < max; i++) { - for (int j = 1; j < i; j++) { - if (i % j == 0) { - sum += j; - } - } - if (sum == i) - perfectNumbers.add(i); - sum = 0; - } - - int[] result = new int[perfectNumbers.size()]; - for (int i = 0; i < result.length; i++) { - result[i] = perfectNumbers.get(i); - } - return result; - } - - /** - * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" - * - * @param array - * @param s - * @return - */ - public static String join(int[] array, String seperator) { - StringBuilder stringBuilder = new StringBuilder(); - for (int i : array) { - stringBuilder.append(i + seperator); - } - stringBuilder.delete(stringBuilder.length() - seperator.length(), stringBuilder.length()); - - return stringBuilder.toString(); - } - -} diff --git a/group17/1264835468/src/assignment2_26/ArrayUtilTest.java b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java deleted file mode 100644 index 753c026796..0000000000 --- a/group17/1264835468/src/assignment2_26/ArrayUtilTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package assignment2_26; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class ArrayUtilTest { - - @Test - public void testReverseArray() { - int[] array = new int[] {}; - ArrayUtil.reverseArray(array); - assertArrayEquals(new int[] {}, array); - - array = new int[] { 1 }; - ArrayUtil.reverseArray(array); - assertArrayEquals(new int[] { 1 }, array); - - array = new int[] { 1, 2, 3 }; - ArrayUtil.reverseArray(array); - assertArrayEquals(new int[] { 3, 2, 1 }, array); - } - - @Test - public void testRemoveZero() { - int[] array = new int[] {}; - assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); - - array = new int[] { 0 }; - assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); - - array = new int[] { 1 }; - assertArrayEquals(new int[] { 1 }, ArrayUtil.removeZero(array)); - - array = new int[] { 1, 2, 0, 0, 3 }; - assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); - - array = new int[] { 1, 2, 3 }; - assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); - } - - @Test - public void testMerge() { - int[] array1 = { 3, 5, 7, 8 }; - int[] array2 = { 4, 5, 6, 7 }; - assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, ArrayUtil.merge(array1, array2)); - } - - @Test - public void testGrow() { - int[] array = { 3, 5, 7 }; - assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, ArrayUtil.grow(array, 5)); - assertArrayEquals(new int[] { 3, 5, 7 }, ArrayUtil.grow(array, 3)); - } - - @Test - public void testFibonacci() { - assertArrayEquals(new int[] {}, ArrayUtil.fibonacci(1)); - - assertArrayEquals(new int[] { 1, 1 }, ArrayUtil.fibonacci(2)); - - assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, ArrayUtil.fibonacci(15)); - } - - @Test - public void testGetPrimes() { - assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(1)); - - assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(2)); - - assertArrayEquals(new int[] { 2 }, ArrayUtil.getPrimes(3)); - - assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, ArrayUtil.getPrimes(20)); - } - - @Test - public void testGetPerfectNumbers() { - assertArrayEquals(new int[] { 6 }, ArrayUtil.getPerfectNumbers(10)); - - assertArrayEquals(new int[] { 6, 28 }, ArrayUtil.getPerfectNumbers(100)); - - assertArrayEquals(new int[] { 6, 28, 496 }, ArrayUtil.getPerfectNumbers(1000)); - - assertArrayEquals(new int[] { 6, 28, 496, 8128 }, ArrayUtil.getPerfectNumbers(10000)); - - } - - @Test - public void testJoin() { - assertEquals("3-4-5", ArrayUtil.join(new int[] { 3, 4, 5 }, "-")); - - assertEquals("345", ArrayUtil.join(new int[] { 3, 4, 5 }, "")); - - assertEquals("3", ArrayUtil.join(new int[] { 3 }, "")); - - assertEquals("3--4--5", ArrayUtil.join(new int[] { 3, 4, 5 }, "--")); - } - -} diff --git a/group17/1264835468/src/assignment2_26/LoginAction.java b/group17/1264835468/src/assignment2_26/LoginAction.java deleted file mode 100644 index 3101d322b8..0000000000 --- a/group17/1264835468/src/assignment2_26/LoginAction.java +++ /dev/null @@ -1,42 +0,0 @@ -package assignment2_26; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * - * @author liuxin - * - */ -public class LoginAction { - private String name; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute() { - if ("test".equals(name) && "1234".equals(password)) { - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getMessage() { - return this.message; - } -} diff --git a/group17/1264835468/src/assignment2_26/Struts.java b/group17/1264835468/src/assignment2_26/Struts.java deleted file mode 100644 index ad704e0433..0000000000 --- a/group17/1264835468/src/assignment2_26/Struts.java +++ /dev/null @@ -1,99 +0,0 @@ -package assignment2_26; - -import java.io.File; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - -public class Struts { - - private static File configFile = new File("./src/struts.xml");; - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - // 0 - XmlParser phraser = new XmlParser(configFile); - String className = phraser.getClassNameByActionName(actionName); - View view = new View(); - try { - // 1 - Class actionClass = Class.forName(className); - Constructor constructor = actionClass.getConstructor(); - Object instance = constructor.newInstance(); - invokeSetters(actionClass, instance, parameters); - - // 2 - String result = invokeExecute(actionClass, instance); - - // 3 - Map getterResult = invokeGetters(actionClass, instance); - view.setParameters(getterResult); - - // 4 - String resultJsp = phraser.getResultJsp(actionName, result); - view.setJsp(resultJsp); - - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException - | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - e.printStackTrace(); - } - - return view; - } - - private static Map invokeGetters(Class actionClass, Object instance) - throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Map result = new HashMap<>(); - for (Method method : actionClass.getDeclaredMethods()) { - if (method.getName().matches("get.*")) { - String fieldName = method.getName().substring(3).toLowerCase(); - String value = (String) (method.invoke(instance)); - result.put(fieldName, value); - } - } - return result; - } - - private static String invokeExecute(Class actionClass, Object instance) throws NoSuchMethodException, - SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method method = actionClass.getDeclaredMethod("execute"); - return (String) method.invoke(instance); - } - - private static void invokeSetters(Class actionClass, Object instance, Map parameters) - throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, - InvocationTargetException { - for (String fieldName : parameters.keySet()) { - invokeSetter(actionClass, instance, fieldName, parameters.get(fieldName)); - } - } - - private static void invokeSetter(Class actionClass, Object instance, String fieldName, String value) - throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, - InvocationTargetException { - String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); - Method setter = actionClass.getDeclaredMethod(setterName, String.class); - setter.invoke(instance, value); - } -} diff --git a/group17/1264835468/src/assignment2_26/StrutsTest.java b/group17/1264835468/src/assignment2_26/StrutsTest.java deleted file mode 100644 index f47410209b..0000000000 --- a/group17/1264835468/src/assignment2_26/StrutsTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package assignment2_26; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name", "test"); - params.put("password", "1234"); - - View view = Struts.runAction(actionName, params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name", "test"); - params.put("password", "123456"); // 密码和预设的不一致 - - View view = Struts.runAction(actionName, params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/group17/1264835468/src/assignment2_26/View.java b/group17/1264835468/src/assignment2_26/View.java deleted file mode 100644 index e96197fad6..0000000000 --- a/group17/1264835468/src/assignment2_26/View.java +++ /dev/null @@ -1,26 +0,0 @@ -package assignment2_26; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - - public Map getParameters() { - return parameters; - } - - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/group17/1264835468/src/struts.xml b/group17/1264835468/src/struts.xml index ad43b47967..9ba23d1e24 100644 --- a/group17/1264835468/src/struts.xml +++ b/group17/1264835468/src/struts.xml @@ -1,6 +1,6 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp diff --git a/group17/1282579502/src/com/coderising/jvm/loader/ClassFileLoader.java b/group17/1282579502/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..6d669e294f --- /dev/null +++ b/group17/1282579502/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,86 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + String classPath = convertToFilePath(className); + File targetFile = null; + for(int i = 0; i< clzPaths.size(); i++){ + String fullPath = clzPaths.get(i)+File.separator+classPath; + System.out.println("path: " + fullPath); + File temp = new File(fullPath); + if(temp.exists()) { + targetFile = temp; + break; + } + } + + if(targetFile != null){ + System.out.println("targetFile: " + targetFile.getAbsolutePath()); + } + long fileLength = targetFile.length(); + System.out.println("File length: " + fileLength); + byte[] byteArray = new byte[(int)fileLength]; + FileInputStream is = null; + try { + is = new FileInputStream(targetFile); + is.read(byteArray); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + if(is != null){ + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + + return byteArray; + + + } + + private String convertToFilePath(String className){ + return className.replaceAll("\\.", File.separator) + ".class"; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + for(String item : clzPaths){ + sb.append(item + ";"); + } + return sb.substring(0, sb.length()-1); + } + + + + + +} \ No newline at end of file diff --git a/group17/1282579502/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group17/1282579502/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..4df3f4b529 --- /dev/null +++ b/group17/1282579502/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,103 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + static String path3 = "/Users/erlisuo/Documents/workspace/codeRising2017working/1282579502/bin"; + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + loader.addClassPath(path3); + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path3); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + @Test + public void testToHexString(){ + byte b1 = 'a'; + byte b2 = 'b'; + System.out.println("Binary: " + Integer.toBinaryString(b1) + " decimal: " + Integer.toString(b1) + " hex: " + Integer.toHexString(b1)); + System.out.println("Binary: " + Integer.toBinaryString(b2) + " decimal: " + Integer.toString(b2) + " hex: " + Integer.toHexString(b2)); + + byte[] bArray = new byte[]{b1, b2}; + System.out.println(byteToHexString(bArray)); + } + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i keyHash; + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + this.count = 0; + keyHash = new HashMap(); + first = new Node(); + first.pageNum = -99; + last = new Node(); + last.pageNum = -99; + first.next = last; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + Node targetPageNode = keyHash.get(new Integer(pageNum)); + + if(targetPageNode == null){ + targetPageNode = new Node(); + targetPageNode.pageNum = pageNum; + addNewPage(targetPageNode); + } + else{ + moveToTop(targetPageNode); + } + + } + /* + * null - first - f1 - f2 -f3 + */ + private void moveToTop(Node targetNode){ + if(targetNode != first.next){ + targetNode.prev.next = targetNode.next; + if(targetNode.next != null){ + targetNode.next.prev = targetNode.prev; + } + + targetNode.next = first.next; + if(first.next != null){ + first.next.prev = targetNode; + } + + first.next = targetNode; + targetNode.prev = first; + } + } + + private void addNewPage(Node targetPageNode){ + //first.next ?= null + // + targetPageNode.next = first.next; + if(first.next != null){ + first.next.prev = targetPageNode; + } + + first.next = targetPageNode; + targetPageNode.prev = first; + + keyHash.put(targetPageNode.pageNum, targetPageNode); + count++; + if(count > capacity){ + popOutLast(); + } + } + /* + * t3 - t2 - t1 - last - null + */ + private void popOutLast(){ + Node tailNode = last.prev; //t1 + if(tailNode != null){ + Node preTailNode = tailNode.prev; //t2 + if(preTailNode != null){ + keyHash.remove(preTailNode.pageNum); + preTailNode.next = last; //t2 -> last + last.prev = preTailNode; + count --; + } + } + } + public void printList(){ + int tmpcount = 0; + Node node = first.next; + while(node != last && tmpcount++<20){ + System.out.println("current: "+node.pageNum+ " parent: " + node.prev.pageNum); + node = node.next; + } + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first.next; + while(node != null){ + if(node == last) break; + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + + } + + String returnStr = null; + if(buffer.charAt(buffer.length() -1 ) == ','){ + returnStr = buffer.substring(0, buffer.length() -1); + }else{ + returnStr = buffer.toString(); + } + return returnStr; + } + +} \ No newline at end of file diff --git a/group17/1282579502/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group17/1282579502/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..0bfbe14fd0 --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,55 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + +// @Test +// public void toStringTest(){ +// LRUPageFrame frame = new LRUPageFrame(3); +// frame.access(7); +// frame.access(0); +// frame.access(1); +// System.out.println("array: " + frame.toString()); +// frame.access(2); +// System.out.println("array: " + frame.toString()); +// +// frame.access(0); +// System.out.println("array: " + frame.toString()); +// frame.access(0); +// System.out.println("array: " + frame.toString()); +// frame.access(3); +// System.out.println("array: " + frame.toString()); +// +// frame.access(0); +// System.out.println("array: " + frame.toString()); +// +// frame.access(4); +// System.out.println("array: " + frame.toString()); +// } + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java b/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..638cf181ae --- /dev/null +++ b/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,126 @@ +package com.coding.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + private int currentSize; + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + currentSize = 0; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + // 判断缓存是否命中 + Node n = find(pageNum); + if(n != null){ + // 命中,则把节点提到表头 + moveNodeToHead(n); + }else{ + // 若未命中,新增节点放到表头,如果链表长度超过capacity,移除链表尾部节点 + n = new Node(); + n.pageNum = pageNum; + + if(currentSize >= capacity){ + removeLast(); + } + addNodeToHead(n); + } + } + + + + private void addNodeToHead(Node n) { + if(first == null){ + first = n; + last = n; + }else{ + n.next = first; + first.prev = n; + first = n; + } + currentSize++; + } + + private void removeLast() { + Node prevN = last.prev; + prevN.next = null; + last.prev = null; + last = prevN; + currentSize--; + } + + private void moveNodeToHead(Node n) { + if(n == first){ + return ; + }else if(n == last){ + Node preN = n.prev; + preN.next = null; + last.prev = null; + last = preN; + }else{ + // 中间节点 + Node preN = n.prev; + Node nextN = n.next; + preN.next = nextN; + nextN.prev = preN; + } + n.prev = null; + n.next = first; + first.prev = n; + first = n; + } + + private Node find(int pageNum) { + Node n = first; + while(n != null){ + if(n.pageNum == pageNum){ + return n; + } + n = n.next; + } + return null; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..67cf36067b --- /dev/null +++ b/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LinkedList.java b/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/group17/240094626/work_jvm_1/data-structure/src/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group17/240094626/work_jvm_1/src/com/coderising/jvm/loader/ClassFileLoader.java b/group17/240094626/work_jvm_1/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..b9d924a887 --- /dev/null +++ b/group17/240094626/work_jvm_1/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,73 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + if(null == className || "".equals(className)){ + return null; + } + className = className.replace(".", File.separator)+".class"; + Iterator it = clzPaths.iterator(); + byte[] bytes = null; + while(it.hasNext() && bytes == null){ + String filePath = it.next()+File.separator+className; + bytes = getClassFile(filePath); + } + return bytes; + + } + + + private byte[] getClassFile(String filePath) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + InputStream is = new FileInputStream(new File(filePath)); + byte[] buffer = new byte[1024]; + int len = 0; + while((len=is.read(buffer)) > 0){ + bos.write(buffer,0, len); + } + } catch (Exception e) { + e.printStackTrace(); + } + return bos.toByteArray(); + } + + + public void addClassPath(String path) { + if(null != path && !"".equals(path)){ + clzPaths.add(path); + } + } + + + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + Iterator it = clzPaths.iterator(); + while(it.hasNext()){ + if(sb.length() > 0){ + sb.append(";"); + } + sb.append(it.next()); + } + return sb.toString(); + } + + + + + +} diff --git a/group17/240094626/work_jvm_1/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group17/240094626/work_jvm_1/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..a205045d2e --- /dev/null +++ b/group17/240094626/work_jvm_1/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.test; + +import java.io.File; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "E:\\workspace-indigo-32-statsdb\\warm-up\\bin";//"C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i { + private Node head; + private int size = 0; + + public void add(T o) { + if (head == null) { + head = new Node(null, o); + size++; + } else + addLast(o); + } + + private Node getLast() { + Node last = head; + while (last.next != null) { + last = last.next; + } + return last; + } + + private Node getNodeIndex(int index) { + if (index > size - 1) + throw new IndexOutOfBoundsException("size : " + size + ", index : " + index); + Node target = head; + for (int i = 0; i < size; i++) { + if (i == index) + return target; + target = target.next; + } + return null; + } + + public void add(int index, T o) { + Node node = getNodeIndex(index - 1); + Node nextNode = node.next; + node.next = new Node(nextNode, o); + size++; + } + + public T get(int index) { + Node node = getNodeIndex(index); + return node.data; + } + + public T remove(int index) { + Node prev = getNodeIndex(index - 1); + Node now = getNodeIndex(index); + prev.next = now.next; + size--; + return now.data; + } + + public int size() { + return size; + } + + public void addFirst(T o) { + if (head != null) + head = new Node(null, o); + else { + Node newNode = new Node(head, o); + head = newNode; + } + size++; + } + + public void addLast(T o) { + add(size, o); + } + + public T removeFirst() { + Node removeNode = head; + if (head != null) + head = head.next; + size--; + return removeNode == null ? null : removeNode.data; + } + + public T removeLast() { + Node last = getNodeIndex(size - 1); + Node prev = getNodeIndex(size - 2); + prev.next = null; + size--; + return last.data; + } + + public Iterator iterator() { + return null; + } + + private int getIndex(Node node) { + Node temp = head; + int index = 0; + while (temp != null) { + if (temp == node) { + return index; + } + } + return -1; + } + + private int getIndexByData(T data) { + Node temp = head; + int index = 0; + while (temp != null) { + if ((data == null && temp.data == null) || temp.data.equals(data)) + return index; + index++; + temp = temp.next; + } + return -1; + } + + + private static class Node { + T data; + Node next; + + Node(Node next, T data) { + this.next = next; + this.data = data; + } + + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + Node temp = head; + while (temp != null) { + sb.append(temp.data).append("-->"); + temp = temp.next; + } + return sb.toString().substring(0, sb.lastIndexOf("-->")); + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node cur = null; + Node prev = null; + while (head != null) { + cur = head; + head = head.next; + cur.next = prev; + prev = cur; + } + head = cur; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + head = getNodeIndex(size / 2); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (size <= (i + length) || i < 0) + throw new IndexOutOfBoundsException("size : " + size + ", i + length : " + (i + length)); + Node rightNode = getNodeIndex(i + length); + if (i == 0) + head = rightNode; + else { + Node leftNode = getNodeIndex(i - 1); + leftNode.next = rightNode; + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public Object[] getElements(LinkedList list) { + Object[] result = new Object[list.size]; + if (list != null) { + for (int i = 0; i < list.size; i++) { + result[i] = get(list.get(i)); + } + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + if (list != null && list.size > 0) { + for (int i = 0; i < list.size; i++) { + int index = getIndexByData(list.get(i)); + if (index != -1) + remove(index); + else + throw new RuntimeException("wrong element of removed list"); + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node temp = head; + List list = new ArrayList(); + int index = 0; + while (temp != null) { + if (list.contains(temp.data)) + remove(index--); + else + list.add(temp.data); + temp = temp.next; + index++; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Integer first = (Integer) get(0); + Integer last = (Integer) getLast().data; + if (first > max || last < min) + return; + List indexRange = new ArrayList(); + Node temp = (Node) head; + int index = 0; + while (temp != null) { + if (temp.data >= min && temp.data <= max) { + indexRange.add(index); + } + index++; + temp = temp.next; + } + remove(indexRange.get(0), indexRange.size()); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList newList = new LinkedList(); + merge(newList, (Node) this.head, list.head); + return newList; + } + + private void merge(LinkedList newList, Node thisHead, Node mergeHead) { + if (thisHead == null && mergeHead == null) + return; + if (thisHead == null) { + //无论是否包含,有元素的链表必须指向next + if (!newList.contains(mergeHead.data)) + newList.add(mergeHead.data); + mergeHead = mergeHead.next; + merge(newList, null, mergeHead); + } + if (mergeHead == null) { + if (!newList.contains(thisHead.data)) + newList.add(thisHead.data); + thisHead = thisHead.next; + merge(newList, thisHead, null); + } + //要再进行一次判断是因为递归到最底层return之后,返回上一层时某个链表已经为null了,但是上一层还是会将剩下的执行完 + if (thisHead != null && mergeHead != null) { + if (thisHead.data < mergeHead.data && !newList.contains(thisHead.data)) { + newList.add(thisHead.data); + thisHead = thisHead.next; + merge(newList, thisHead, mergeHead); + } else if (!newList.contains(mergeHead.data)) { + newList.add(mergeHead.data); + mergeHead = mergeHead.next; + merge(newList, thisHead, mergeHead); + } + } + } + + private boolean contains(Integer data) { + int index = this.getIndexByData((T) data); + return index != -1; + } +} diff --git a/group17/785396327/3.12/link/LinkedListTest.java b/group17/785396327/3.12/link/LinkedListTest.java new file mode 100644 index 0000000000..87edc35e99 --- /dev/null +++ b/group17/785396327/3.12/link/LinkedListTest.java @@ -0,0 +1,145 @@ +package link; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by gongxun on 2017/3/13. + */ +public class LinkedListTest { + private LinkedList linkedList; + + @Before + public void startUp() { + linkedList = new LinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); +// System.out.println(linkedList); + } + + @After + public void tearDown() { + + } + + @Test + public void addFirst() { + linkedList.addFirst("1"); + System.out.println(linkedList); + } + + @Test + public void add() { + linkedList.add("1"); + linkedList.add("2"); + System.out.println(linkedList); + } + + @Test + public void add2() { + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add(1, "0"); + System.out.println(linkedList); + } + + @Test + public void addLast() { + linkedList.add("1"); + linkedList.addLast("2"); + System.out.println(linkedList); + } + + @Test + public void get() { + String value = linkedList.get(2); + System.out.println(value); + } + + @Test + public void remove() { + String removeValue = linkedList.remove(3); + System.out.println(removeValue); + } + + @Test + public void reverse() { + linkedList.reverse(); + System.out.println(linkedList); + } + + @Test + public void removeFirstHalf() { + linkedList.removeFirstHalf(); + System.out.println(linkedList); + } + + @Test + public void removeByRange() { + linkedList.remove(0, 2); + System.out.println(linkedList); + } + + @Test + public void getElements() { + LinkedList indexList = new LinkedList(); + indexList.add(0); + indexList.add(2); + indexList.add(3); + Object[] elements = linkedList.getElements(indexList); + System.out.println(Arrays.toString(elements)); + } + + @Test + public void subtract() { + LinkedList indexList = new LinkedList(); + indexList.add("2"); + indexList.add("0"); + linkedList.subtract(indexList); + System.out.println(linkedList); + } + + @Test + public void removeDuplicateValues() { + linkedList.add("3"); + linkedList.add("7"); + linkedList.add("0"); + linkedList.add("1"); + System.out.println(linkedList); + linkedList.removeDuplicateValues(); + System.out.println(linkedList); + } + + @Test + public void removeRange() { + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(5); + list.add(7); + System.out.println(list); + list.removeRange(1, 6); + System.out.println(list); + } + + @Test + public void intersection() { + LinkedList list1 = new LinkedList(); + list1.add(2); + list1.add(3); + list1.add(7); + LinkedList list2 = new LinkedList(); + list2.add(2); + list2.add(3); + list2.add(7); + LinkedList newList = list1.intersection(list2); + System.out.println(newList); + } + +} diff --git a/group17/785396327/3.26/jvm_1/ClassFileLoader.java b/group17/785396327/3.26/jvm_1/ClassFileLoader.java new file mode 100644 index 0000000000..95f68a5a7f --- /dev/null +++ b/group17/785396327/3.26/jvm_1/ClassFileLoader.java @@ -0,0 +1,50 @@ +package jvm_1; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by william on 2017/4/5. + */ +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + InputStream fis = null; + String filePath = clzPaths.get(0) + "\\\\" + className.replaceAll("\\.", "\\\\") + ".class"; + byte[] buffer = new byte[(int) new File(filePath).length()]; + try { + if (clzPaths.size() > 0 && className != null && !className.trim().equals("")) { + fis = new FileInputStream(filePath); + while (fis.read(buffer) != -1) ; + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (fis != null) + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + fis = null; + } + } + return buffer; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + public String getClassPath() { + StringBuilder sb = new StringBuilder(); + for (String clzPath : clzPaths) { + sb.append(clzPath).append(";"); + } + return sb.substring(0, sb.length() - 1); + } +} diff --git a/group17/785396327/3.26/jvm_1/ClassFileloaderTest.java b/group17/785396327/3.26/jvm_1/ClassFileloaderTest.java new file mode 100644 index 0000000000..067cd93c2b --- /dev/null +++ b/group17/785396327/3.26/jvm_1/ClassFileloaderTest.java @@ -0,0 +1,86 @@ +package jvm_1; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by william on 2017/4/5. + */ +public class ClassFileloaderTest { + static String path1 = "D:\\workspace\\IDEA\\homework\\coding2017\\group17\\785396327\\out\\production\\785396327"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "jvm_1.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1020, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "jvm_1.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + if (clzPaths.size()<=0){ + return null; + } + for (int i = 0; i < clzPaths.size(); i++) { + String path = clzPaths.get(i) + convertName(className); + File f = new File(path); + if(f.exists()){ + try { + return readFile(f); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + }else{ + f = null; + continue; + } + } + System.err.println("classpath:" +getClassPath()+ "class:" + convertName(className)+" not found."); + return null; + } + /** + * 文件读取二进制字节流 + * @param f + * @return + * @throws FileNotFoundException + */ + private byte[] readFile(File f) throws FileNotFoundException { + FileInputStream fis = new FileInputStream(f); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] b = new byte[BUFFERSIZE]; + try { + int len = 0; + while((len = fis.read(b))!=-1){ + baos.write(b,0,len); + } + } catch (IOException e) { + e.printStackTrace(); + } finally{ + try { + fis.close(); + baos.flush(); + baos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + return baos.toByteArray(); + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < clzPaths.size(); i++) { + sb.append(clzPaths.get(i)).append(";"); + } + int len = sb.length(); + if(len!=0){ + sb.deleteCharAt(len-1); + } + return sb.toString(); + } + + /** + * convert className to FilePath style className
+ * For example:com.sun.lang to \com\sun\lang + * + * @param className + * @return FilePath style className + */ + private String convertName(String className) { + StringBuilder sb = new StringBuilder(); + String[] pack = className.split("\\."); + for (int i = 0; i < pack.length; i++) { + sb.append("\\").append(pack[i]); + } + sb.append(".class"); + return sb.toString(); + } + + public static void main(String[] args) { + String d = "com.taiji.array.Load"; + ClassFileLoader cc = new ClassFileLoader(); + System.out.print(cc.convertName(d)); + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/jvm/test/EmployeeV1.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..3e0f0d08b0 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,29 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LRU/LRUPageFrame.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LRU/LRUPageFrame.java new file mode 100644 index 0000000000..e26bd91e93 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LRU/LRUPageFrame.java @@ -0,0 +1,147 @@ +package com.coding.basic.LRU; + +public class LRUPageFrame { + private int capacity; + private Node first;// 链表头 + private Node last;// 链表尾 + private int length = 0; + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + // this loop select for pageNum,grow it first when 'found'. + for (Node n = first; n != null; n = n.next) { + if (n.pageNum == pageNum) { + growFirst(n); + return; + } + } + // if didn't found it + if (ensureFull()) { + removeLast(); + } + add(pageNum); + } + + private void add(int pageNum) { + if (isEmpty()) { + Node newNode = new Node(null, null, pageNum); + first = newNode; + last = newNode; + length++; + } else { + addToFirst(pageNum); + } + } + + private void addToFirst(int pageNum) { + Node newNode = new Node(null, null, pageNum); + newNode.next = first; + first.prev = newNode; + first = newNode; + length++; + } + + /** + * ensure the LRUPageFrame is full or Not + * + * @return if full return true,else false + */ + private boolean ensureFull() { + if (length < capacity) { + return false; + } else { + return true; + } + } + + /** + * grow the Node'position to first + * + * @param n + */ + private void growFirst(Node n) { + // if the node is already first ,return. + if (first.pageNum == n.pageNum) { + return; + } + remove(n); + addToFirst(n.pageNum); + } + + private void remove(Node n) { + if (isEmpty()) { + return; + } + if (n.next == null) { + removeLast(); + return; + } + if (n.prev == null) { + removeFirst(); + return; + } + Node prev = n.prev; + Node next = n.next; + n.next = null; + n.prev = null; + prev.next = next; + next.prev = prev; + length--; + } + + private void removeFirst() { + Node next = first.next; + first.next = null; + first = next; + } + + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + length--; + } + /** + * ensure the LRUPageFrame is empty or Not. + * @return + */ + public boolean isEmpty() { + return length < 1; + } + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + + private static class Node { + Node prev; + Node next; + int pageNum; + + Node(Node prev, Node next, int pageNum) { + this.prev = prev; + this.next = next; + this.pageNum = pageNum; + } + } +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coderising/jvm/loader/ClassFileLoaderTest.java b/group17/82427129/JavaUtil/src/test/java/com/coderising/jvm/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..bd06965cdd --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coderising/jvm/loader/ClassFileLoaderTest.java @@ -0,0 +1,73 @@ +package com.coderising.jvm.loader; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileLoaderTest { + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "D:\\workProgram\\GitRepo\\coding2017\\group17\\82427129\\JavaUtil\\target\\classes"; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path2); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testGetClassPath() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path2); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LRU/LRUPageFrameTest.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LRU/LRUPageFrameTest.java new file mode 100644 index 0000000000..4dd2ca2baf --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LRU/LRUPageFrameTest.java @@ -0,0 +1,29 @@ +package com.coding.basic.LRU; + +import org.junit.Assert; +import org.junit.Test; + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group17/article/20170326-20170402.md b/group17/article/20170326-20170402.md new file mode 100644 index 0000000000..5df4ce08e6 --- /dev/null +++ b/group17/article/20170326-20170402.md @@ -0,0 +1,56 @@ +# 自由写作 + +## 须知 +--- + +交作业时请在QQ 号后面填上各自的文章链接, 比如: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 文章 +--- + +1204187480 + +102228177 + +876385982 + +785396327 + +1059107701 + +240094626 + +82427129 + +296910598 + +1264835468 http://www.jianshu.com/p/848fd6be5247 + +516886559 + +1282579502 https://www.evernote.com/l/AZ2Rx5pxgf9I-JqkSpFANMwTK9fXR0KFV50 + +614982500 + +865797761 + +1540186032 + +176653813 + +116665530 + +51075907 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + diff --git a/group17/article/20170402-20170409.md b/group17/article/20170402-20170409.md new file mode 100644 index 0000000000..3d45ad0516 --- /dev/null +++ b/group17/article/20170402-20170409.md @@ -0,0 +1,56 @@ +# 自由写作 + +## 须知 +--- + +交作业时请在QQ 号后面填上各自的文章链接, 比如: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 文章 +--- + +1204187480 + +102228177 + +876385982 + +785396327 + +1059107701 + +240094626 + +82427129 + +296910598 + +1264835468 + +516886559 + +1282579502 + +614982500 + +865797761 + +1540186032 + +176653813 + +116665530 + +51075907 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + diff --git a/group17/article/template.md b/group17/article/template.md index 09c7951d71..0b06ef733e 100644 --- a/group17/article/template.md +++ b/group17/article/template.md @@ -10,9 +10,9 @@ ## 文章 --- -1204187480 http://blog.leanote.com/post/luoziyihao/js%E9%97%AD%E5%8C%85 +1204187480 -102228177 +102228177 http://note.youdao.com/noteshare?id=1f8f4a9d861e24948cdf0219a0d39f4e 876385982 @@ -26,11 +26,11 @@ 296910598 -1264835468 http://www.jianshu.com/p/634ca8cdd6e3 +1264835468 516886559 -1282579502 https://www.evernote.com/shard/s413/sh/142601dd-edc3-4e37-871e-37a7489d7634/a092bf080e1aefbaeab96d34edac8cf0 +1282579502 614982500 diff --git a/group17/count/homework.md b/group17/count/homework.md index e474336779..640cf4698f 100644 --- a/group17/count/homework.md +++ b/group17/count/homework.md @@ -17,3 +17,7 @@ * [20170305-20170312](https://github.com/luoziyihao/coding2017/blob/master/group17/article/20170305-20170312.md) + * [20170326-20170402](https://github.com/luoziyihao/coding2017/blob/master/group17/article/20170326-20170402.md) + * [20170402-20170409](https://github.com/luoziyihao/coding2017/blob/master/group17/article/20170402-20170409.md) + +20170326-20170402.md 20170402-20170409.md diff --git a/group17/count/reward.md b/group17/count/reward.md new file mode 100644 index 0000000000..4a0fe7ea81 --- /dev/null +++ b/group17/count/reward.md @@ -0,0 +1,16 @@ +## 17组 201703 获奖 + +### 代码坚持奖 + +82427129 +1282579502 +102228177 +1264835468 + +### 博客坚持奖 + +82427129 +1282579502 +102228177 +1264835468 + diff --git a/group22/1158477486/src/TestCollection/ArrayUtil.java b/group22/1158477486/src/TestCollection/ArrayUtil.java new file mode 100644 index 0000000000..c5df8c2b1c --- /dev/null +++ b/group22/1158477486/src/TestCollection/ArrayUtil.java @@ -0,0 +1,259 @@ +package TestCollection; +import java.util.ArrayList; + + +public class ArrayUtil { + + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + for(int i=0,j=origin.length-1;i array[j] ) // ˳ˣͽһ + { + swap = array[j]; + array[j] = array[j-1]; + array[j-1] = swap; + } + } + } + + return array; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int newArray[]=new int [oldArray.length+size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] a=new int [100]; + a[0]=1; + a[1]=1; + int i =2; + for(;ilist=new ArrayList(); + for (int i = 2; i list1=new ArrayList(); + ArrayListlist=new ArrayList(); + for(int i=1;i parameters) { + + View view = new View(); + /* + 0. ȡļstruts.xml + 1. actionNameҵӦclass LoginAction, ͨʵ + parametersеݣösetter parametersе + ("name"="test" , "password"="1234") , + ǾӦõ setNamesetPassword + 2. ͨöexectue ÷ֵ"success" + 3. ͨҵgetter getMessage, + ͨã ֵγһHashMap , {"message": "¼ɹ"} , + ŵViewparameters + ŵViewjspֶС + */ + + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read("src/struts.xml"); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Element root= document.getRootElement(); + List list = root.elements("action"); + String className = null; + Element newElement = null; + for (Element element : list) { + if(element.attribute("name").getValue().equals(actionName)){ + Attribute attribute = element.attribute("class"); + newElement = element; + className = attribute.getValue(); + } + } + Class clazz = null; + try { + clazz = Class.forName(className); + Object obj = clazz.newInstance(); + for (String key : parameters.keySet()) + { + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if(method.getName().toLowerCase().equals(("set"+key).toLowerCase())){ + method.invoke(obj,parameters.get(key)); + } + } + + } + + String value = (String) clazz.getMethod("execute").invoke(obj); + List elements = newElement.elements(); + + String message = ""; + String jsp = ""; + for (Element element : elements) { + if(element.attribute("name").getValue().equals(value)){ + jsp = element.getText(); + } + } + if("success".equals(value)){ + message = "login successful"; + }else if("fail".equals(value)){ + message = "login failed,please check your user/pwd"; + } + view.setJsp(jsp); + Map p = new HashMap(); + p.put("message",message); + view.setParameters(p); + + return view; + + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return view; + } +} \ No newline at end of file diff --git a/group22/1158477486/src/TestCollection/StrutsTest.java b/group22/1158477486/src/TestCollection/StrutsTest.java new file mode 100644 index 0000000000..b7103b9c5c --- /dev/null +++ b/group22/1158477486/src/TestCollection/StrutsTest.java @@ -0,0 +1,51 @@ +package TestCollection; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + + + + + + + + + + + +public class StrutsTest { + + + + @Test + public void testLoginActionSuccess(){ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + + + @Test + public void testLoginActionFailed() throws DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + + params.put("name","test"); + + params.put("password","123456"); //ԤIJһ + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group22/1158477486/src/TestCollection/View.java b/group22/1158477486/src/TestCollection/View.java new file mode 100644 index 0000000000..ddcbd5862a --- /dev/null +++ b/group22/1158477486/src/TestCollection/View.java @@ -0,0 +1,43 @@ +package TestCollection; + +import java.util.Map; + + + +public class View { + + private String jsp; + + private Map parameters; + + + + public String getJsp() { + + return jsp; + + } + + public View setJsp(String jsp) { + + this.jsp = jsp; + + return this; + + } + + public Map getParameters() { + + return parameters; + + } + + public View setParameters(Map parameters) { + + this.parameters = parameters; + + return this; + + } + +} \ No newline at end of file diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java index 1dc4dd4342..dbca3e4361 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java @@ -73,7 +73,7 @@ public Object remove(int index) { Node lastNode = null; if (index + 1 <= size - 1) //判断是否有下一位 nextNode = findNode(index + 1); - if (index - 1 > 0) //判断是否有上一位 + if (index - 1 >= 0) //判断是否有上一位 lastNode = findNode(index - 1); if (lastNode == null) { head = nextNode; @@ -172,17 +172,17 @@ public Object next() { } /** - * 鎶婅閾捐〃閫嗙疆 - * 渚嬪閾捐〃涓�3->7->10 , 閫嗙疆鍚庡彉涓� 10->7->3 + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ } /** - * 鍒犻櫎涓�釜鍗曢摼琛ㄧ殑鍓嶅崐閮ㄥ垎 - * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫�涓�7->8 - * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫�涓�,8,10 + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 */ public void removeFirstHalf(){ @@ -190,7 +190,7 @@ public void removeFirstHalf(){ } /** - * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱�锛�娉ㄦ剰i浠�寮� + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 * @param i * @param length */ @@ -198,11 +198,11 @@ public void remove(int i, int length){ } /** - * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁� - * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵�寚瀹氱殑鍏冪礌 - * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 * listB = 1->3->4->6 - * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * 返回的结果应该是[101,301,401,601] * @param list */ public static int[] getElements(LinkedList list){ @@ -210,8 +210,8 @@ public static int[] getElements(LinkedList list){ } /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ�閫掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩� - * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 * @param list */ @@ -221,16 +221,16 @@ public void subtract(LinkedList list){ } /** - * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ�閫掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩� - * 鍒犻櫎琛ㄤ腑鎵�湁鍊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ } /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ�閫掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩� - * 璇曞啓涓�珮鏁堢殑绠楁硶锛屽垹闄よ〃涓墍鏈夊�澶т簬min涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛� + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) * @param min * @param max */ @@ -239,12 +239,11 @@ public void removeRange(int min, int max){ } /** - * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸�澧炴湁搴忔帓鍒楋紙鍚屼竴琛ㄤ腑鐨勫厓绱犲�鍚勪笉鐩稿悓锛� - * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸�澧炴湁搴忔帓鍒� + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 * @param list */ public LinkedList intersection( LinkedList list){ return null; } - } diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedListTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedListTest.java index 254aa95c53..c566888cdf 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedListTest.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedListTest.java @@ -65,7 +65,7 @@ public void testGet() { @Test public void testRemoveInt() { - LinkedList li = new LinkedList(); + /*LinkedList li = new LinkedList(); li.add("1"); li.add("2"); li.add("3"); @@ -75,8 +75,23 @@ public void testRemoveInt() { assertEquals(li.get(0), "1"); assertEquals(li.get(1), "2"); assertEquals(li.get(2), "3"); - assertEquals(li.get(3), "5"); + assertEquals(li.get(3), "5");*/ + LinkedList ll = new LinkedList(); + ll.add(11); + ll.add(101); + ll.add(201); + ll.add(301); + ll.add(401); + ll.add(501); + ll.add(601); + ll.add(701); + ll.remove(0); + ll.remove(1); + //ll.remove(3); + //ll.remove(4); + System.out.println(ll.get(0)+" "+ll.get(1)+" "+ll.get(2)+" "+ll.get(3)); + } @Test diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/DownloadThread.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/DownloadThread.java new file mode 100644 index 0000000000..f59f585528 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/DownloadThread.java @@ -0,0 +1,43 @@ +package com.github.mrwengq.tid; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.github.mrwengq.tid.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String fileName; + CyclicBarrier cb; + + public DownloadThread( Connection conn, String fileName ,int startPos, int endPos,CyclicBarrier cb){ + + this.conn = conn; + this.fileName = fileName; + this.startPos = startPos; + this.endPos = endPos; + this.cb = cb; + } + public void run(){ + byte[] b = null; + try { + b = conn.read(startPos,endPos); + RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); + raf.seek(startPos); + raf.write(b); + raf.close(); + conn.close(); + cb.await(); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/FileDownloader.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/FileDownloader.java new file mode 100644 index 0000000000..21de21e939 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/FileDownloader.java @@ -0,0 +1,129 @@ +package com.github.mrwengq.tid; + +import java.io.FileNotFoundException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.github.mrwengq.tid.api.Connection; +import com.github.mrwengq.tid.api.ConnectionException; +import com.github.mrwengq.tid.api.ConnectionManager; +import com.github.mrwengq.tid.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + String fileName; + + public FileDownloader(String _url,String fileName) { + this.url = _url; + this.fileName = fileName; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + + CyclicBarrier cb = new CyclicBarrier(3, new Runnable() { + + @Override + public void run() { + listener.notifyFinished(); + + } + }); + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + System.out.println(length); + createFile(length); //创建占位文件 + + int size = length/3; + int pyl = (length - (length%3))/3; + + if(length<3){ + + cb = new CyclicBarrier(3, new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + new DownloadThread(conn,fileName,0,length-1,cb).start(); + + + }else{ + + for(int i =0;i<3;i++){ + if(2==i){ + new DownloadThread(conn,fileName, i*size, (i+1)*size+length%3-1,cb).start(); + System.out.println("第i线程"+i+"起始"+i*size+"-结束"+(i*size+length%3-1)); + break; + } + + new DownloadThread(conn,fileName,i*size,(i+1)*size-1,cb).start(); + System.out.println("第i线程"+i+"起始"+i*size+"-结束"+((i+1)*size-1)); + } + + } + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + private void createFile( int length){ + try { + RandomAccessFile raf = new RandomAccessFile(fileName,"rw"); + while(length>0){ + raf.write(0); + length --; + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/FileDownloaderTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/FileDownloaderTest.java new file mode 100644 index 0000000000..da68d5031f --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/FileDownloaderTest.java @@ -0,0 +1,60 @@ +package com.github.mrwengq.tid; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.mrwengq.tid.api.ConnectionManager; +import com.github.mrwengq.tid.api.DownloadListener; +import com.github.mrwengq.tid.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + //String url = "http://localhost:8080/test.jpg"; + String url = "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + String fileName = "C:\\Users\\Administrator\\Desktop\\个人文档\\test.jpg"; + FileDownloader downloader = new FileDownloader(url,fileName); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/Connection.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/Connection.java new file mode 100644 index 0000000000..e92c3796ca --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/Connection.java @@ -0,0 +1,23 @@ +package com.github.mrwengq.tid.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/ConnectionException.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/ConnectionException.java new file mode 100644 index 0000000000..ce3b3a66c9 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.github.mrwengq.tid.api; + +public class ConnectionException extends Exception { + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/ConnectionManager.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/ConnectionManager.java new file mode 100644 index 0000000000..2112030d07 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.github.mrwengq.tid.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/DownloadListener.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/DownloadListener.java new file mode 100644 index 0000000000..97f89fef60 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.github.mrwengq.tid.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/impl/ConnectionImpl.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/impl/ConnectionImpl.java new file mode 100644 index 0000000000..52f8d0f5c1 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/impl/ConnectionImpl.java @@ -0,0 +1,72 @@ +package com.github.mrwengq.tid.impl; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + +import com.github.mrwengq.tid.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URL url; + + public ConnectionImpl(String url ){ + try { + this.url = new URL(url); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + URLConnection con = url.openConnection(); + con.setRequestProperty("RANGE","bytes="+startPos+"-"+endPos); + BufferedInputStream bf = new BufferedInputStream(con.getInputStream()); + byte[] b = new byte[1024]; + int tolen = endPos - startPos +1; + ByteArrayOutputStream bo = new ByteArrayOutputStream(); + while(bo.size()tolen){ + byte[] data = bo.toByteArray(); + return Arrays.copyOf(data, tolen); + } + return bo.toByteArray(); + } + + @Override + public int getContentLength() { + int a = 0; + try { + + URLConnection con = url.openConnection(); + a = con.getContentLength(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return a; + } + + @Override + public void close() { + + + } + + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/impl/ConnectionManagerImpl.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..62e0b3e6e0 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/impl/ConnectionManagerImpl.java @@ -0,0 +1,21 @@ +package com.github.mrwengq.tid.impl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + + +import com.github.mrwengq.tid.api.Connection; +import com.github.mrwengq.tid.api.ConnectionException; +import com.github.mrwengq.tid.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/Iterator.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/Iterator.java new file mode 100644 index 0000000000..3dd2a731f0 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/Iterator.java @@ -0,0 +1,10 @@ +package com.github.mrwengq.tid.list; + + +public interface Iterator +{ + + public abstract boolean hasNext(); + + public abstract Object next(); +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/LinkedList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/LinkedList.java new file mode 100644 index 0000000000..77b0b0ca2b --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/LinkedList.java @@ -0,0 +1,401 @@ +package com.github.mrwengq.tid.list; + +public class LinkedList implements List { + private Node head; + private int size =0; + private static class Node { + + Object data; + Node next; + + public Node(Object o) { + data = o; + next = null; + } + } + + + public void add(Object o) { + if (size == 0) { + head = new Node(o); + } else { + Node node = new Node(o); + Node lastNode = findNode(size-1); + lastNode.next = node; + } + size++; + } + + private Node findNode(int index) {//用于查找节点 + Node no = head; + for (; index > 0; index--) + no = no.next; + + return no; + } + + public void add(int index, Object o) { + if (index < 0 || index > size - 1) + throw new ArrayIndexOutOfBoundsException(); + Node node = new Node(o); + Node indexNode = findNode(index); + if (index - 1 < 0) { + node.next = indexNode; + head = node; + size++; + return; + } else { + Node lastNode = findNode(index - 1); + lastNode.next = node; + node.next = indexNode; + size++; + return; + } + } + + public Object get(int index) { + if (index < 0 || index > size - 1) + throw new ArrayIndexOutOfBoundsException(); + else + return findNode(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1 || size == 0) + throw new ArrayIndexOutOfBoundsException(); + Node indexNode = findNode(index); + if (size == 1) { + head = null; + size = 0; + return indexNode.data; + } + Node nextNode = null; + Node lastNode = null; + if (index + 1 <= size - 1) //判断是否有下一位 + nextNode = findNode(index + 1); + if (index - 1 >= 0) //判断是否有上一位 + lastNode = findNode(index - 1); + if (lastNode == null) { + head = nextNode; + size--; + return indexNode.data; + }else if (nextNode == null) { + lastNode.next = null; + size--; + return indexNode.data; + } else { + lastNode.next = nextNode; + size--; + return indexNode.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + if (size == 0) { + head = node; + size++; + return; + } else { + node.next = head; + head = node; + size++; + return; + } + } + + public void addLast(Object o) { + Node node = new Node(o); + if (size == 0) { + head = node; + size++; + return; + } else { + Node lastNode = findNode(size-1); + lastNode.next = node; + size++; + return; + } + } + + public Object removeFirst() { + if (size == 0) { + return null; + } else { + Node nextNode = head.next; + Object ob = head.data; + head = nextNode; + size--; + return ob; + } + } + + public Object removeLast() { + if (size == 0) { + return null; + } else { + Node node = findNode(size-1); //size -1 为最后一位 -2为前一位 + if(size-2>=0){ + Node lastNode = findNode(size - 2); + lastNode.next = null; + } + size--; + return node.data; + } + } + + public Iterator iterator() { + return new Iterator() { + + int index = -1; + + public boolean hasNext() { + index++; + if(index7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(size==0){ + throw new RuntimeException(); + } + int cs = size/2; + int endIndex = size -1; + for(int i = 0;i5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(size<2){ + throw new RuntimeException(); + } + + int len = size/2; + Node node = findNode(len-1);//len-1为删除链表的最后一位下标 + for( int j = len-2; j>=0 ; j--){ + Node temp = findNode(j); + temp.next = null; + } + head = node.next; + node.next = null; + size -= len; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(0 == size||i > size-1){ + throw new RuntimeException(); + } + Node beforNode = findNode(i-1); //i的前一个元素 + Node afterNode = findNode(i+length);//被删除最大下标节点的下一个节点 + for( int j = i+length-1; j101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int temp = list.size()-1; + if(temp>size){ + throw new RuntimeException(); + } + int[] b = new int[list.size()]; + for(int i = 0;i0){ + for (; temp > 0; temp--) + no = no.next; + } + b[i] = (int)no.data; + } + return b; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + if(list==null){ + throw new RuntimeException(); + } + for(int i = 0;i min){ + lmax = lmid-1; + if((int)this.get(lmax)min){ + rmin = lmin; + break; + } + } + } + while(lminmax){ + rmax = lmid; + } + }else if((int)this.get(lmid)>=max){ + lmax = lmid -1; + if((int)this.get(lmax)=rmin;i--){ + Node removeNode = findNode(i); + removeNode.next = null; + size--; + } + beforNode.next = afterNode; + } + + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + int len = size; + int llen = list.size(); + int i = 0; + int j = 0; + LinkedList ll= new LinkedList(); + while(true){ + if(i == len &&j == llen ){ + break; + } + if(i>len-1){ + ll.add(list.get(j++)); + continue; + } + if(j>llen-1){ + ll.add(this.get(i++)); + continue; + } + if((int)get(i)<(int)list.get(j)){ + ll.add(this.get(i++)); + }else if((int)get(i)>(int)list.get(j)){ + ll.add(list.get(j++)); + }else{ + ll.add(list.get(j++)); + i++; + } + } + return ll; + } +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/LinkedListTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/LinkedListTest.java new file mode 100644 index 0000000000..91fe49652a --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/LinkedListTest.java @@ -0,0 +1,176 @@ +package com.github.mrwengq.tid.list; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + LinkedList ll = null; + + @Before + public void setUp() throws Exception { + ll = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverse() { + ll.add(3); + ll.add(7); + ll.add(10); + ll.add(6); + ll.add(12); + assertEquals((int)ll.get(0), 3); + assertEquals((int)ll.get(1), 7); + assertEquals((int)ll.get(2), 10); + assertEquals((int)ll.get(3), 6); + assertEquals((int)ll.get(4), 12); + ll.reverse(); + assertEquals((int)ll.get(0), 12); + assertEquals((int)ll.get(1), 6); + assertEquals((int)ll.get(2), 10); + assertEquals((int)ll.get(3), 7); + assertEquals((int)ll.get(4), 3); + + } + + @Test + public void testRemoveFirstHalf() { + ll.add(2); + ll.add(5); + ll.add(7); + ll.add(8); + ll.add(10); + assertEquals((int)ll.get(0), 2); + assertEquals((int)ll.get(1), 5); + assertEquals((int)ll.get(2), 7); + assertEquals((int)ll.get(3), 8); + assertEquals((int)ll.get(4), 10); + assertEquals(ll.size(),5); + ll.removeFirstHalf(); + assertEquals((int)ll.get(0), 7); + assertEquals((int)ll.get(1), 8); + assertEquals((int)ll.get(2), 10); + assertEquals(ll.size(),3); + } + + @Test + public void testRemoveIntInt() { + ll.add(2); + ll.add(5); + ll.add(7); + ll.add(8); + ll.add(10); + assertEquals(ll.size(),5); + ll.remove(1, 2); + assertEquals((int)ll.get(0), 2); + assertEquals((int)ll.get(1), 8); + assertEquals((int)ll.get(2), 10); + assertEquals(ll.size(),3); + } + + @Test + public void testGetElements() { + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(6); + assertEquals(list.size(),4); + ll.add(11); + ll.add(101); + ll.add(201); + ll.add(301); + ll.add(401); + ll.add(501); + ll.add(601); + ll.add(701); + int [] b = ll.getElements(list); + assertArrayEquals(new int[]{101,301,401,601}, b); + } + + @Test + public void testSubtract() { + ll.add(11); + ll.add(101); + ll.add(201); + ll.add(301); + ll.add(401); + ll.add(501); + ll.add(601); + ll.add(701); + LinkedList list = new LinkedList(); + list.add(11); + list.add(201); + list.add(301); + list.add(401); + ll.subtract(list); + assertEquals(list.size(),4); + assertEquals((int)ll.get(0), 101); + assertEquals((int)ll.get(1), 501); + assertEquals((int)ll.get(2), 601); + assertEquals((int)ll.get(3), 701); + } + + @Test + public void testRemoveDuplicateValues() { + ll.add(11); + ll.add(101); + ll.add(101); + ll.add(101); + ll.add(401); + ll.add(501); + ll.add(501); + ll.add(701); + ll.removeDuplicateValues(); + assertEquals((int)ll.get(0), 11); + assertEquals((int)ll.get(1), 101); + assertEquals((int)ll.get(2), 401); + assertEquals((int)ll.get(3), 501); + assertEquals((int)ll.get(4), 701); + + } + + @Test + public void testRemoveRange() { + ll.add(11); + ll.add(101); + ll.add(201); + ll.add(301); + ll.add(401); + ll.add(501); + ll.add(601); + ll.add(701); + ll.removeRange(400, 700); + assertEquals(ll.size(),5); + + } + + @Test + public void testIntersection() { + ll.add(11); + ll.add(101); + ll.add(201); + + LinkedList list = new LinkedList(); + list.add(101); + list.add(121); + list.add(300); + ll = ll.intersection(list); + + assertEquals(ll.size(), 5); + assertEquals((int)ll.get(0), 11); + assertEquals((int)ll.get(1), 101); + assertEquals((int)ll.get(2), 121); + assertEquals((int)ll.get(3), 201); + assertEquals((int)ll.get(4), 300); + + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/List.java b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/List.java new file mode 100644 index 0000000000..eaf561cb97 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/tid/list/List.java @@ -0,0 +1,16 @@ +package com.github.mrwengq.tid.list; + + +public interface List +{ + + public abstract void add(Object obj); + + public abstract void add(int i, Object obj); + + public abstract Object get(int i); + + public abstract Object remove(int i); + + public abstract int size(); +} diff --git a/group22/1258890344/src/com/coderising/download/DownloadThread.java b/group22/1258890344/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..af605fc3e3 --- /dev/null +++ b/group22/1258890344/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,31 @@ +package com.coderising.download; + +import java.io.File; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + File file; + + public DownloadThread( Connection conn, int startPos, int endPos,File file){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.file=file; + } + + public void run(){ + try { + conn.read(startPos, endPos,file); +// conn.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/group22/1258890344/src/com/coderising/download/FileDownloader.java b/group22/1258890344/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f0bebf1623 --- /dev/null +++ b/group22/1258890344/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,105 @@ +package com.coderising.download; + +import java.io.File; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + String path; + int threadNum;//线程数 + int fileLength;//要下载的文件的大小 + File file; + + DownloadListener listener; + + ConnectionManager cm; + + public FileDownloader(){ + + } + public FileDownloader(String _url,String _path,int _threadNum) { + this.url = _url; + this.path=_path; + this.threadNum=_threadNum; + } + + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + conn = cm.open(this.url);//打开网络连接 + + file=new File(path); + if(!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + if(file.exists()){ + file.delete(); + } + file.createNewFile();//创建 文件 + + + fileLength = conn.getContentLength();//获取要下载的文件的大小 + System.out.println("文件总长度:"+fileLength+"字节"); + + int blockSize=fileLength/threadNum; //每个线程平均下载的块的大小 + + for(int i=1;i<=threadNum;i++){ + int startPos=(i-1)*blockSize; + int endPos=i*blockSize-1; + if(i==threadNum){ + endPos=fileLength; + } + System.out.println("线程"+i+"下载"+startPos+"字节~"+endPos+"字节"); + + new Thread(new DownloadThread(conn,startPos,endPos,file)).start(); + + } + + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + while(file.length()7->10 , 逆置后变为 10->7->3 */ - public void reverse(){ - + public void reverse(){ + for(int i=0;i<(size/2);i++){ + Object data1=get(i); + Object data2=get(size-1); + Object object=data1; + data1=data2; + data2=object; + } } /** @@ -140,7 +146,9 @@ public void reverse(){ */ public void removeFirstHalf(){ - + for(int i=0;i<(size/2);i++){ + remove(i); + } } /** @@ -149,7 +157,9 @@ public void removeFirstHalf(){ * @param length */ public void remove(int i, int length){ - + for(int j=i;j<(i+length);j++){ + remove(j); + } } /** * 假定当前链表和list均包含已升序排列的整数 @@ -159,7 +169,12 @@ public void remove(int i, int length){ * 返回的结果应该是[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ + public int[] getElements(LinkedList list){ + int[] array=new int[list.size]; + int i=0; + for(Node head=list.head;head!=null;head=head.next){ + array[i]=(int) this.get((int)head.data); + } return null; } @@ -171,7 +186,7 @@ public static int[] getElements(LinkedList list){ */ public void subtract(LinkedList list){ - + } /** @@ -198,6 +213,7 @@ public void removeRange(int min, int max){ * @param list */ public LinkedList intersection( LinkedList list){ + return null; } } diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/DownloadThread.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..0d9e351975 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,35 @@ +package com.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CountDownLatch cdl; + + public DownloadThread( Connection conn, int startPos, int endPos, CountDownLatch cdl){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.cdl = cdl; + } + public void run(){ + try { + byte[] read = conn.read(startPos, endPos); + RandomAccessFile raf = new RandomAccessFile("F:\\test.rar", "rwd"); + raf.seek(startPos); + raf.write(read); + raf.close(); + cdl.countDown(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/FileDownloader.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..2afed85ba7 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,81 @@ +package com.coderising.download; + +import java.util.concurrent.CountDownLatch; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + public static final int THREAD_NUM = 5; + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + CountDownLatch cdl = new CountDownLatch(THREAD_NUM); + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + int averageLength = length/THREAD_NUM; + for (int i = 0; i < THREAD_NUM; i++) { + if(i == THREAD_NUM - 1){ + new DownloadThread(conn,averageLength * i,length-1,cdl).start(); + }else{ + new DownloadThread(conn,averageLength * i,averageLength * (i + 1) - 1,cdl).start(); + } + } + try { + cdl.await(); + listener.notifyFinished(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/FileDownloaderTest.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..5d7da6d0bb --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,53 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://127.0.0.1:8020/demo/java.rar"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + downloader.execute(); + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } + +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/Connection.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/ConnectionException.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/ConnectionManager.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/DownloadListener.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..aba6afc252 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,51 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + + +public class ConnectionImpl implements Connection{ + + private URLConnection urlConnection; + private URL url; + public ConnectionImpl(String url){ + try { + this.url = new URL(url); + urlConnection = this.url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + URLConnection uc = url.openConnection(); + uc.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream inputStream = uc.getInputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = 0; + while((length = inputStream.read(buffer)) != -1){ + baos.write(buffer, 0, length); + } + inputStream.close(); + baos.close(); + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + return urlConnection.getContentLength(); + } + + @Override + public void close() { + + } + +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/group22/1335499238/coding2017/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..c7d47979c8 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group22/1335499238/week01/src/basic/ArrayList.java b/group22/1335499238/week01/src/basic/ArrayList.java index 64b3045312..5e099a6c72 100644 --- a/group22/1335499238/week01/src/basic/ArrayList.java +++ b/group22/1335499238/week01/src/basic/ArrayList.java @@ -18,7 +18,7 @@ public ArrayList(int capacity){ }else if(capacity == 0){ }else{ - new IllegalArgumentException("initsize:"+capacity); + throw new IllegalArgumentException("initsize:"+capacity); } } @@ -39,13 +39,13 @@ public void add(int index, Object o) { @Override public Object get(int index) { - checkIndex(index); + checkIndex(index + 1); return elementData[index]; } @Override public Object remove(int index) { - checkIndex(index); + checkIndex(index + 1); Object removeparam = elementData[index]; int numMoved = size - index - 1; if(numMoved > 0){ diff --git a/group22/1335499238/week01/src/basic/LinkedList.java b/group22/1335499238/week01/src/basic/LinkedList.java index 9af1471bfb..5e20329a2e 100644 --- a/group22/1335499238/week01/src/basic/LinkedList.java +++ b/group22/1335499238/week01/src/basic/LinkedList.java @@ -28,14 +28,14 @@ public void add(int index, Object o) { @Override public Object get(int index) { - checkIndex(index); + checkIndex(index + 1); return findByIndex(index).data; } @Override public Object remove(int index) { Node remove = null; - checkIndex(index); + checkIndex(index + 1); Node next = findByIndex(index+1); if(index == 0){ remove = head; @@ -135,7 +135,12 @@ private void checkIndex(int index){ * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ - + Node current = this.head; + for (int i = 0; i < size-1; i++) { + removeFirst(); + add(size - i, current.data); + current = current.next; + } } /** @@ -145,7 +150,10 @@ public void reverse(){ * */ public void removeFirstHalf(){ - + int total = size/2; + for (int i = 0; i < total; i++) { + removeFirst(); + } } /** @@ -154,7 +162,15 @@ public void removeFirstHalf(){ * @param length */ public void remove(int i, int length){ - + if(i < 0 || length < 0){ + throw new IllegalArgumentException("参数异常"); + } + if(i + length > size){ + throw new IndexOutOfBoundsException(); + } + for (int j = 0; j < length; j++) { + remove(i); + } } /** * 假定当前链表和listB均包含已升序排列的整数 @@ -165,7 +181,20 @@ public void remove(int i, int length){ * @param list */ public int[] getElements(LinkedList list){ - return null; + if(list == null || list.head == null){ + return new int[0]; + } + int result[] = new int [list.size]; + Iterator iterator = list.iterator(); + int index = 0; + while(iterator.hasNext()){ + int next = (int)iterator.next(); + if(next < size){ + result[index] = (int)this.get(next); + } + index++; + } + return result; } /** @@ -175,14 +204,33 @@ public int[] getElements(LinkedList list){ */ public void subtract(LinkedList list){ - + if(list == null || list.head == null){ + return; + } + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + Object next = iterator.next(); + Iterator iteratorInner = this.iterator(); + int index = 0; + while(iteratorInner.hasNext()){ + if(next.equals(iteratorInner.next())){ + this.remove(index); + break; + } + index++; + } + } } /** * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + for (int i = 0; i < size; i++) { + if(findByIndex(i).data == findByIndex(i+1).data){ + remove(i); + } + } } /** * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 @@ -191,7 +239,25 @@ public void removeDuplicateValues(){ * @param max */ public void removeRange(int min, int max){ - + if(min >= max){ + throw new IllegalArgumentException("参数异常"); + } + int minIndex = 0; + int maxIndex = 0; + boolean flag = true; + for (int i = 0; i < size; i++) { + int current = (int)get(i); + if(flag && current > min){ + minIndex = i; + flag = false; + }else if(current >= max){ + maxIndex = i; + break; + }else{ + maxIndex = size; + } + } + remove(minIndex, maxIndex - minIndex); } /** @@ -200,7 +266,20 @@ public void removeRange(int min, int max){ * @param list */ public LinkedList intersection( LinkedList list){ - return null; + if(list == null || list.head == null){ + return null; + } + LinkedList linkedList = new LinkedList(); + Iterator iterator = this.iterator(); + while(iterator.hasNext()){ + Object next = iterator.next(); + Iterator iterator2 = list.iterator(); + while(iterator2.hasNext()){ + if(next.equals(iterator2.next())){ + linkedList.add(next); + } + } + } + return linkedList; } - } diff --git a/group22/17457741/src/thirdwork/DownloadThread.java b/group22/17457741/src/thirdwork/DownloadThread.java new file mode 100644 index 0000000000..836342423b --- /dev/null +++ b/group22/17457741/src/thirdwork/DownloadThread.java @@ -0,0 +1,5 @@ +package thirdwork; + +public class DownloadThread { + +} diff --git a/group22/17457741/src/thirdwork/FileDownloader.java b/group22/17457741/src/thirdwork/FileDownloader.java new file mode 100644 index 0000000000..b1b7be257e --- /dev/null +++ b/group22/17457741/src/thirdwork/FileDownloader.java @@ -0,0 +1,5 @@ +package thirdwork; + +public class FileDownloader { + +} diff --git a/group22/17457741/src/thirdwork/api/Connection.java b/group22/17457741/src/thirdwork/api/Connection.java new file mode 100644 index 0000000000..c7f1a71bf4 --- /dev/null +++ b/group22/17457741/src/thirdwork/api/Connection.java @@ -0,0 +1,5 @@ +package thirdwork.api; + +public class Connection { + +} diff --git a/group22/17457741/src/thirdwork/api/ConnectionException.java b/group22/17457741/src/thirdwork/api/ConnectionException.java new file mode 100644 index 0000000000..f4a7d55715 --- /dev/null +++ b/group22/17457741/src/thirdwork/api/ConnectionException.java @@ -0,0 +1,5 @@ +package thirdwork.api; + +public class ConnectionException { + +} diff --git a/group22/17457741/src/thirdwork/api/ConnectionManager.java b/group22/17457741/src/thirdwork/api/ConnectionManager.java new file mode 100644 index 0000000000..41a08c7712 --- /dev/null +++ b/group22/17457741/src/thirdwork/api/ConnectionManager.java @@ -0,0 +1,5 @@ +package thirdwork.api; + +public class ConnectionManager { + +} diff --git a/group22/17457741/src/thirdwork/api/DownloadListener.java b/group22/17457741/src/thirdwork/api/DownloadListener.java new file mode 100644 index 0000000000..6bd3c6fa75 --- /dev/null +++ b/group22/17457741/src/thirdwork/api/DownloadListener.java @@ -0,0 +1,5 @@ +package thirdwork.api; + +public class DownloadListener { + +} diff --git a/group22/17457741/src/thirdwork/impl/ConnectionImpl.java b/group22/17457741/src/thirdwork/impl/ConnectionImpl.java new file mode 100644 index 0000000000..7e1b049d8c --- /dev/null +++ b/group22/17457741/src/thirdwork/impl/ConnectionImpl.java @@ -0,0 +1,5 @@ +package thirdwork.impl; + +public class ConnectionImpl { + +} diff --git a/group22/17457741/src/thirdwork/impl/ConnectionManagerImpl.java b/group22/17457741/src/thirdwork/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..2548fa6b22 --- /dev/null +++ b/group22/17457741/src/thirdwork/impl/ConnectionManagerImpl.java @@ -0,0 +1,5 @@ +package thirdwork.impl; + +public class ConnectionManagerImpl { + +} diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 81c1db409c..57dfdef603 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -129,8 +129,11 @@ public Node insertAsSucc(Object data) { * Ѹ * Ϊ 3->7->10 , úΪ 10->7->3 */ - public void reverse(){ - + public void reverse(){ + int times = theSize; + int index = 0; + while (0 < --times) + add(index++, removeLast()); } /** @@ -139,8 +142,10 @@ public void reverse(){ * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 */ - public void removeFirstHalf(){ - + public void removeFirstHalf(){ + int times = theSize / 2; + while (0 < times--) + removeFirst(); } /** @@ -148,19 +153,46 @@ public void removeFirstHalf(){ * @param i * @param length */ - public void remove(int i, int length){ + public void remove(int i, int length){ + Node head = get(i).pred(); //ɾ(head, tail)֮Ԫ ɾ[i, i + length - 1]֮Ԫ + Node tail = get(i + length - 1).succ(); + head.succ = tail; + tail.pred = head; + theSize -= length; } /** * ٶǰlistе * ӵǰȡЩlistָԪ * 統ǰ = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 + * list = 1->3->4->6 * صĽӦ[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ - return null; + public int[] getElements(LinkedList list){ + Iterator itSelf = iterator(); + Iterator itList = list.iterator(); + int[] ret = new int[list.size()]; + + int i = 0; //listԪصֵǰбҪȡԪص + lastI = 0;//һȡԪص + moveTimes = 0; + value = itSelf.next(); + index = 0;//ҪصԪص + + while (itList.hasNext()) { + i = itList.next(); + if (theSize <= i) throw new IndexOutOfBoundsException(); + + moveTimes = i - lastI; + while (0 < moveTimes--) + value = itSelf.next(); + + ret[index++] = value; + lastI = i; + } + + return ret; } /** @@ -169,9 +201,37 @@ public static int[] getElements(LinkedList list){ * @param list */ + //eȵԪصȣʧ򷵻-1 + private int find(Object e) { + Iterator it = iterator(); + int i = -1; //ҪصԪص + Object value = null; + + while (it.hasNext()) { + value = it.next(); + i++; + if (value == e) return i; + if (e < value) return -1; + } - public void subtract(LinkedList list){ - + return -1; + } + + public void subtract(LinkedList list){ + Iterator it = list.iterator(); + Object value = null; + int i = -1; + + while (it.hasNext()) { + value = it.next(); + i = find(value); + + //ɾȥظԪ + while (0 <= i) { + remove(i); + i = find(value); + } + } } /** @@ -179,7 +239,20 @@ public void subtract(LinkedList list){ * ɾֵͬĶԪأʹòԱԪصֵͬ */ public void removeDuplicateValues(){ - + Node current = header.succ(); + Node next = current; + int removedNum = 0; + + while ((next = next.succ()) != trailer) { + if (current.data() == next.data()) { + removedNum++; + } else { + current.succ = next; + next.pred = current; + current = next; + } + } + theSize -= removedNum; } /** @@ -188,7 +261,26 @@ public void removeDuplicateValues(){ * @param min * @param max */ + //[low, min]U[max, end] + + public void removeRange(int min, int max){ + //ɾȥ(i, j] + int i = 0, j = 0; + Iterator it = iterator(); + while (it.hasNext()) { + Object value = it.next(); + if (value <= min) i++; + if (value < max) j++; + else break; //if(max <= value) break; + } + + Node head = get(i); + Node tail = get(j).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= (j - i); } @@ -197,7 +289,27 @@ public void removeRange(int min, int max){ * ҪCԪΪǰlistԪصĽұCеԪֵ * @param list */ - public LinkedList intersection( LinkedList list){ - return null; + //ABԪصĺϼ + public LinkedList intersection(LinkedList list){ + LinkedList ret = new LinkedList(); + Iterator it = iterator(); + Iterator itList = list.iterator(); + Object value1 = null, value2 = null; + + if (it.hasNext() && itList.hasNext()) { + value1 = it.next(); + value2 = itList.next(); + } + + while (value1 != null && value2 != null) { + if (value1 < value2) value1 = it.hasNext() ? it.next() : null; + else if (value2 < value1) value2 = itList.hasNext() ? itList.next() : null; + else { + ret.add(value1); + value1 = it.hasNext() ? it.next() : null; + value2 = itList.hasNext() ? itList.next() : null; + } + } + return ret; } } diff --git a/group22/2622819383/Task2/ArrayUtil.java b/group22/2622819383/Task2/ArrayUtil.java new file mode 100644 index 0000000000..ff947431ce --- /dev/null +++ b/group22/2622819383/Task2/ArrayUtil.java @@ -0,0 +1,272 @@ +public class ArrayUtil { + + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int lo = 0; + int hi = origin.length - 1; + while (lo < hi) + swap(origin, lo++, hi--); + } + private void swap(int[] array, int lo, int hi) { + int temp = array[lo]; + array[lo] = array[hi]; + array[hi] = temp; + } + + /** + * µһ飺 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * ҪֵΪ0ȥΪ0ֵһµ飬ɵΪ + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] ret = new int[oldArray.length]; + int i = 0; + for (int j = 0; j < oldArray.length; j++) { + if (oldArray[j] != 0) + ret[i++] = oldArray[j]; + } + int[] old = ret; + ret = new int[i]; + for (int j = 0; j < i; j++) + ret[j] = old[j]; + + return ret; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int m = array1.length; //array1 m i + int n = array2.length; //array2 n j + int[] ret = new int[m + n]; // ret m+n k + int k = 0; + for (int i = 0, j = 0; i < m || j < n; ) { + if (i < m && (n <= j || array1[i] < array2[j])) ret[k++] = array1[i++]; + if (j < n && (m <= i || array2[j] < array1[i])) ret[k++] = array2[j++]; + if (i < m && j < n && array1[i] == array2[j]) { + ret[k++] = array1[i++]; + j++; + } + } + int[] old = ret; + ret = new int[k]; + for (int i = 0; i < k; i++) + ret[i] = old[i]; + + return ret; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] ret = new int[oldArray.length + size]; + + for (int i = 0; i < oldArray.length; i++) + ret[i] = oldArray[i]; + + return ret; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] ret = new int[max / 2 + 10]; + int f = 1, g = 0, i = 0; + for ( ; f < max; i++) { + ret[i] = f; + f = g + f; + g = f - g; + } + int[] old = ret; + ret = new int[i]; + for (int j = 0; j < i; j++) + ret[j] = old[j]; + return ret; + } + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] ret = new int[max / 3 + 10]; //ʡʼٵĿռ䣻ret i + int i = 0; //iret + //˻: max < 5 + if (2 < max) { ret[i++] = 2; } + if (3 < max) { ret[i++] = 3; } + if (5 < max) { ret[i++] = 5; } + if (7 < max) { + //ֻΪ6k+16k+5 + //kСֵ1 + //жkֵ6k + 1 <= max6k + 5maxıȽҪԼȷ + int k = 1; + while (6 * k + 1 <= max) { + int m = 6 * k + 1; + int n = 6 * k + 5; + if(isPrime(ret, m)) ret[i++] = m; + if (max < n) break; + if (isPrime(ret, n)) ret[i++] = n; + k++; + } + }//O(n/3) * O((n^0.5) / 3) + int[] old = ret; + ret = new int[i]; + for (int j = 0; j < i; j++) + ret[j] = old[j]; + return ret; + } + + private boolean isPrime(int[] primeArray, int target) { + //O((n^0.5) / 3) + boolean isPrime = true; + int min = (int)Math.sqrt(target); + for (int i = 0; primeArray[i] <= min; i++) { + if (target % primeArray[i] == 0) { + isPrime = false; + break; + } + } + + return isPrime; + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 + * һֵmax һ飬 Сmax + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] ret = new int[48]; + int[] supportArr = getPrimes(max); + int j = 0; + for (int i = 2; i < max; i++) { + if (i % 2 != 0) continue; + if (isPerfectNumber(i, supportArr)) ret[j++] = i; + } + int[] old = ret; + ret = new int[j]; + for (int i = 0; i < j; i++) + ret[i] = old[i]; + return ret; + } + private boolean isPerfectNumber(int target, int[] supportArr) { + //ùʽperfectNum = ( 2^p-1 ) * 2^(p-1) = ( 2^(count+1)-1 ) * ( 2^count ) + //p=count+12^p-1=2^(count+1)-1Ҳ + //count: 2ĸ + boolean isPerfectNum = true; + int count = amountOfTwo(target); + + int test0 = (int)Math.pow(2, count); + int test1 = count + 1; + int test2 = test0 * 2 - 1; + + if (count == 0) isPerfectNum = false; + else if (!isPrime(supportArr, test1)) isPerfectNum = false; + else if (!isPrime(supportArr, test2)) isPerfectNum = false; + else if (test0 * test2 != target) isPerfectNum = false; + + return isPerfectNum; + } + private int amountOfTwo(int num) { + int count = 0; + while (num % 2 == 0) { + num /= 2; + count++; + } + return count; + } + + + /** + * seperator array + * array= [3,8,9], seperator = "-" + * 򷵻ֵΪ"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String ret = ""; + if (array.length < 1) return ret; + ret += array[0]; + for (int i = 1; i < array.length; i++) + ret += seperator + array[i]; + return ret; + } + + public static void main(String[] args) { + ArrayUtil au = new ArrayUtil(); + + int[] arr0 = au.fibonacci(50000000); + for (int i = 0; i < arr0.length; i++) + System.out.print(arr0[i] + " "); + // arr1 = {3,}; + //System.out.println(au.join(arr0, "-")); + //System.out.println(au.join(arr1, "-")); + + } + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task2/litestruts/LoginAction.java b/group22/2622819383/Task2/litestruts/LoginAction.java new file mode 100644 index 0000000000..c3318361ae --- /dev/null +++ b/group22/2622819383/Task2/litestruts/LoginAction.java @@ -0,0 +1,41 @@ + + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public void setName(String name){ + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password){ + this.password = password; + } + + public String getMessage(){ + return this.message; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } +} diff --git a/group22/2622819383/Task2/litestruts/Struts.java b/group22/2622819383/Task2/litestruts/Struts.java new file mode 100644 index 0000000000..3109d1f40b --- /dev/null +++ b/group22/2622819383/Task2/litestruts/Struts.java @@ -0,0 +1,167 @@ +//ƪοѧԱ2415980327 + + +import java.io.InputStream; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static Element parseXml(String fileName) { + InputStream input = Struts.class.getResourceAsStream(fileName); + SAXReader reader = new SAXReader(); + Document document = null; + + try { + document = reader.read(input); + Element struts = document.getRootElement(); + return struts; + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. ȡļstruts.xml + + 1. actionNameҵӦclass LoginAction, ͨʵ + parametersеݣösetter parametersе + ("name"="test" , "password"="1234") , + ǾӦõ setNamesetPassword + + 2. ͨöexectue ÷ֵ"success" + + 3. ͨҵgetter getMessage, + ͨã ֵγһHashMap , {"message": "¼ɹ"} , + ŵViewparameters + + 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + ŵViewjspֶС + + */ + Element struts = parseXml("struts.xml"); + List actions = struts.elements(); + List resultRefs = new ArrayList<>(); + String actionClass = ""; + for (Element element : actions) + if (actionName.equals(element.attributeValue("name"))) { + actionClass = element.attributeValue("class"); + resultRefs = element.elements(); + break; + } + + Set attributes = parameters.keySet(); + Iterator it = attributes.iterator(); + try { + Object action = Class.forName(actionClass).newInstance(); + while (it.hasNext()) { + String attribute = it.next(); + Method method = action.getClass().getDeclaredMethod("set" + + attribute.substring(0, 1).toUpperCase() + + attribute.substring(1), String.class); + method.invoke(action, parameters.get(attribute)); + } + + Method execute = action.getClass().getDeclaredMethod("execute"); + String result = (String)execute.invoke(execute); + + Map actionParam = new HashMap(); + Method[] methods = action.getClass().getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().startsWith("get")) { + String methodName = method.getName(); + String name = methodName.substring(3, 4).toUpperCase() + methodName.substring(4); + String value = (String)method.invoke(action); + actionParam.put(name, value); + } + } + + + View view = new View(); + view.setParameters(actionParam); + for (Element element : resultRefs) { + if (result.equals(element.attributeValue("name"))) { + view.setJsp((String)element.getData()); + break; + } + } + return view; + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + String actionName = "login"; + Element struts = parseXml("struts.xml"); + List actions = struts.elements(); + for (Element element : actions) { + if (actionName.equals(element.attributeValue("name"))) { + System.out.println(element.attributeValue("class")); + + for(Element element1:(List)element.elements()){ + System.out.println(element1.getData()); + } + } + } + } +} + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task2/litestruts/StrutsTest.java b/group22/2622819383/Task2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9b188e8d5a --- /dev/null +++ b/group22/2622819383/Task2/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ + + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //ԤIJһ + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group22/2622819383/Task2/litestruts/View.java b/group22/2622819383/Task2/litestruts/View.java new file mode 100644 index 0000000000..4f2ca4ec30 --- /dev/null +++ b/group22/2622819383/Task2/litestruts/View.java @@ -0,0 +1,23 @@ + + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/2622819383/Task3/LinkedList.java b/group22/2622819383/Task3/LinkedList.java new file mode 100644 index 0000000000..02ef7abc52 --- /dev/null +++ b/group22/2622819383/Task3/LinkedList.java @@ -0,0 +1,181 @@ +public class LinkedList { + /** + * Ѹ + * Ϊ 3->7->10 , úΪ 10->7->3 + */ + public void reverse() { + int times = theSize - 1; //һԪȻƶֻtheSize - 1β + int index = 0; + while (0 < times) { + add(index++, removeLast()); + times--; + } + } + + /** + * ɾһǰ벿 + * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + + */ + public void removeFirstHalf() { + int times = theSize / 2; + while (0 < times--) + removeFirst(); + } + + /** + * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * @param i + * @param length + */ + public void remove(int i, int length) { + Node head = get(i).pred(); //ɾ(head, tail)֮Ԫ ɾ[i, i + length - 1]֮Ԫ + Node tail = get(i + length - 1).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= length; + } + /** + * ٶǰlistе + * ӵǰȡЩlistָԪ + * 統ǰ = 11->101->201->301->401->501->601->701 + * list = 1->3->4->6 + * صĽӦ[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list) { + Iterator it = iterator(); + int[] ret = new int[list.size()]; + int start = -1; + int value = 0; + int i = 0; //ret + + for (Integer num : list) { + while (start < num && it.hasNext()) { + value = it.next(); + start++; + } + ret[i++] = value; + } + return ret; + } + + /** + * ֪еԪֵУԵ洢ṹ + * ӵǰɾlistгֵԪ + + * @param list + */ + public void subtract(LinkedList list) { + Object current = null; + for (Object e : list) { + Iterator it = iterator(); + while (it.hasNext()) { + current = it.next(); + if (current.compareTo(e) == 0) + it.remove(); + if (current.compareTo(e) > 0) + break; + } + } + } + + /** + * ֪ǰеԪֵУԵ洢ṹ + * ɾֵͬĶԪأʹòԱԪصֵͬ + */ + public void removeDuplicateValues() { + Node current = header.succ(); + Node next = current; + int removedNum = 0; + + while ((next = next.succ()) != trailer) { + if (current.data() == next.data()) { + removedNum++; + } else { + current.succ = next; + next.pred = current; + current = next; + } + } + theSize -= removedNum; + } + + /** + * ֪еԪֵУԵ洢ṹ + * дһЧ㷨ɾֵminСmaxԪأдԪأ + * @param min + * @param max + */ + + + public void removeRange(int min, int max) { + //˫ɾȥ(p, q)Ľڵ + Node p = header; + Node q = null; + int removedNum = 0; //ҪɾȥڵĿ + while ((p = p.succ()) != trailer && (p.data() <= min)) + + p = p.prev(); + q = p; + while ((q = q.succ()) != trailer && (q.data() < max)) + removedNum++; + p.succ = q; + q.prev = p; + theSize -= removedNum; + + + + /* + //ɾȥ(i, j] + int i = 0, j = 0; + Iterator it = iterator(); + while (it.hasNext()) { + int value = it.next(); + if (value <= min) i++; + if (value < max) j++; + else break; //if(max <= value) break; + } + + Node head = get(i); + Node tail = get(j).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= (j - i); + */ + + } + + /** + * 赱ǰͲlistָԪֵУͬһеԪֵͬ + * ҪCԪΪǰlistԪصĽұCеԪֵ + * @param list + */ + //ABԪصĺϼ + public LinkedList intersection(LinkedList list) { + LinkedList ret = new LinkedList(); + Iterator it = iterator(); + Iterator itList = list.iterator(); + Object value1 = null, value2 = null; + + if (it.hasNext() && itList.hasNext()) { + value1 = it.next(); + value2 = itList.next(); + } + //nullΪϵı־ + //ѭ־һLinkedListѾ + while (value1 != null && value2 != null) { + if (value1 < value2) value1 = it.hasNext() ? it.next() : null; + else if (value2 < value1) value2 = itList.hasNext() ? itList.next() : null; + else { + ret.add(value1); + value1 = it.hasNext() ? it.next() : null; + value2 = itList.hasNext() ? itList.next() : null; + } + } + return ret; + } +} diff --git a/group22/2622819383/Task3/download/Connection.java b/group22/2622819383/Task3/download/Connection.java new file mode 100644 index 0000000000..14eb68f232 --- /dev/null +++ b/group22/2622819383/Task3/download/Connection.java @@ -0,0 +1,53 @@ +import java.net.URL; +import java.net.HttpURLConnection; +import java.io.IOException; +import java.io.InputStream; +import java.io.ByteArrayOutputStream; + +public class Connection { + private URL url; + private HttpURLConnection conn; + + //һConnectionӦһHttpURLConnection + public Connection(String url) { + try { + this.url = new URL(url); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void initConnection() { + try { + conn = (HttpURLConnection)url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + //ӷstartPos-endPosֽڷΧԴݵһֽ + //Range: ڿͻ˵˵󣬿ֶָͨļijһδС䵥λ + public byte[] read(int startPos, int endPos) throws IOException { + initConnection(); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream in = conn.getInputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + byte[] buf = new byte[1024]; + int hasRead = 0; + while ((hasRead = in.read(buf)) != -1) { + out.write(buf, 0, hasRead); + } + out.close(); + in.close(); + return out.toByteArray(); + } + + public int getContentLength() { + initConnection(); + return conn.getContentLength(); + } + + public void close() { + } +} \ No newline at end of file diff --git a/group22/2622819383/Task3/download/DownloadThread.java b/group22/2622819383/Task3/download/DownloadThread.java new file mode 100644 index 0000000000..a04911cf7c --- /dev/null +++ b/group22/2622819383/Task3/download/DownloadThread.java @@ -0,0 +1,34 @@ +import java.io.IOException; +import java.net.HttpURLConnection; +import java.io.RandomAccessFile; +import java.util.concurrent.locks.*; +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String targetURL; + //final ReentrantLock lock = new ReentrantLock(); + + + public DownloadThread(Connection conn, int startPos, int endPos, String targetURL) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.targetURL = targetURL; + } + + public void run() { + System.out.println("߳" + getName() + "startPos:" + startPos + "; endPos:" + endPos); + try { + RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); + byte[] buf = conn.read(startPos, endPos); + raf.seek(startPos); + raf.write(buf); + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("߳" + this.getName() + "."); + } +} diff --git a/group22/2622819383/Task3/download/FileDownloader.java b/group22/2622819383/Task3/download/FileDownloader.java new file mode 100644 index 0000000000..c4d8fa167d --- /dev/null +++ b/group22/2622819383/Task3/download/FileDownloader.java @@ -0,0 +1,66 @@ +import java.util.List; +import java.util.ArrayList; +import java.io.RandomAccessFile; +import java.io.IOException; +public class FileDownloader { + + String url; + + public FileDownloader(String url) { + this.url = url; + } + + public void execute() throws IOException { + // ʵĴ룬 ע⣺ Ҫö߳ʵ + // ӿ, Ҫд⼸ӿڵʵִ + // (1) ConnectionManager , ԴһӣͨConnectionԶȡеһΣstartPos, endPosָ + // (2) DownloadListener, Ƕ߳أ Ŀͻ˲֪ʲôʱҪʵֵ + // ̶ִ߳Ժ listenernotifiedFinished ͻ˾յ֪ͨ + // ʵ˼· + // 1. ҪConnectionManageropenӣ ȻͨConnection.getContentLengthļij + // 2. 3߳أ עÿ߳ҪȵConnectionManageropen + // Ȼread readжȡļĿʼλúͽλõIJ ֵbyte[] + // 3. byteд뵽ļ + // 4. е̶߳Ժ ҪlistenernotifiedFinished + + // Ĵʾ룬 Ҳ˵ֻһ̣߳ Ҫɶ̵߳ġ + Connection conn = null; + conn = new Connection(url); + int length = conn.getContentLength(); + String targetURL = "E:/" + url.substring(url.lastIndexOf("/") + 1); + RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); + raf.setLength(length); + raf.close(); + + for (int i = 0; i < 3; i++) { + int part = length / 3; + int start = i * part; + int end = (i == 2) ? length - 1 : (i + 1) * part - 1; + DownloadThread t = new DownloadThread(conn, start, end, targetURL); + t.start(); + } + } + + /* + DownloadListener listener; + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + */ + + public static void main(String[] args) { + try { + new FileDownloader("http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png").execute(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group22/627559964/src/com/coding/basic/LinkedList.java b/group22/627559964/src/com/coding/basic/LinkedList.java index dcae1fb835..39b0250ae3 100644 --- a/group22/627559964/src/com/coding/basic/LinkedList.java +++ b/group22/627559964/src/com/coding/basic/LinkedList.java @@ -1,7 +1,7 @@ package com.coding.basic; /** - * ԶLinkList + * 自定义LinkList * * @author xiongrui233 * @@ -9,7 +9,7 @@ public class LinkedList implements List { /** - * ڵṹ + * 定义链表节点结构 * * @author xiongrui233 * @@ -19,7 +19,7 @@ private static class Node { Node next; } - // ڵ + // 链表节点 private Node head = new Node(); private int size = 0; @@ -29,7 +29,7 @@ public LinkedList() { } /** - * Ԫ + * 添加元素 * * @param o */ @@ -38,7 +38,7 @@ public void add(Object o) { } /** - * Ԫ + * 添加元素 * * @param index * @param o @@ -64,7 +64,7 @@ public void add(int index, Object o) { } /** - * ȡԪ + * 获取元素 * * @param index */ @@ -77,7 +77,7 @@ public Object get(int index) { } /** - * ɾԪ + * 删除元素 * * @param index */ @@ -102,7 +102,7 @@ public Object remove(int index) { } /** - * LinkedListĴС + * 返回LinkedList的大小 * * @return size */ @@ -111,7 +111,7 @@ public int size() { } /** - * LinkedListһλԪ + * 在LinkedList第一的位置添加元素 * * @param o */ @@ -120,7 +120,7 @@ public void addFirst(Object o) { } /** - * LinkedListԪ + * 在LinkedList最后添加元素 * @param o */ public void addLast(Object o) { @@ -135,7 +135,7 @@ public void addLast(Object o) { } /** - * ƳһλԪ + * 移除链表第一位元素 * * @return obj */ @@ -144,7 +144,7 @@ public Object removeFirst() { } /** - * ƳһλԪ + * 移除链表最后一位元素 * * @return obj */ @@ -153,7 +153,7 @@ public Object removeLast() { } /** - * ʵIteratorӿ + * 实现Iterator接口 * * @return Iterator */ @@ -185,7 +185,7 @@ public Object next() { } /** - * Ѹ Ϊ 3->7->10 , úΪ 10->7->3 + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public LinkedList reverse() { LinkedList lis = new LinkedList(); @@ -196,8 +196,8 @@ public LinkedList reverse() { } /** - * ɾһǰ벿 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 list = 2->5->7->8->10 - * ,ɾԺֵΪ7,8,10 + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 */ public void removeFirstHalf() { int mid = size/2; @@ -207,7 +207,7 @@ public void removeFirstHalf() { } /** - * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 * * @param i * @param length @@ -225,9 +225,9 @@ public void remove(int i, int length) { } /** - * ٶǰlistе ӵǰȡЩlistָԪ 統ǰ = + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 - * صĽӦ[101,301,401,601] + * 返回的结果应该是[101,301,401,601] * * @param list */ @@ -243,7 +243,7 @@ public int[] getElements(LinkedList list) { } /** - * ֪еԪֵУԵ洢ṹ ӵǰɾlistгֵԪ + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 * * @param list */ @@ -261,7 +261,7 @@ public void subtract(LinkedList list) { } /** - * ֪ǰеԪֵУԵ洢ṹ ɾֵͬĶԪأʹòԱԪصֵͬ + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues() { for (int i = 0; i < this.size; i++) { @@ -276,7 +276,7 @@ public void removeDuplicateValues() { } /** - * ֪еԪֵУԵ洢ṹ дһЧ㷨ɾֵminСmaxԪأдԪأ + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) * * @param min * @param max @@ -293,8 +293,8 @@ public void removeRange(int min, int max) { } /** - * 赱ǰͲlistָԪֵУͬһеԪֵͬ - * ҪCԪΪǰlistԪصĽұCеԪֵ + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 * TODO * @param list */ diff --git "a/group22/910725683/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group22/910725683/week01/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" similarity index 100% rename from "group22/910725683/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" rename to "group22/910725683/week01/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" diff --git a/group22/910725683/week01/src/com/coding/basic/ArrayList.java b/group22/910725683/week01/src/com/coding/basic/ArrayList.java index acc0e9f8f4..becbf4f907 100644 --- a/group22/910725683/week01/src/com/coding/basic/ArrayList.java +++ b/group22/910725683/week01/src/com/coding/basic/ArrayList.java @@ -3,10 +3,10 @@ public class ArrayList implements List { - //ʼ// + //数组初始容量// private final int DEFAULT_CAPICITY=7; - //Ԫظ// + //数组元素个数// private int size = 0; private Object[] elementData = new Object[DEFAULT_CAPICITY]; @@ -16,11 +16,11 @@ public void add(Object o){ elementData[size++]=o; } public void add(int index, Object o){ - //indexҪ// + //index要连续的增加// checkIndex(index); ensureCapcity(size+1); - /* indexԪһλ,indexʼƶעindex0ʼ - * Ҫ+1򳤶Ϊsize-((index)+1)+1 + /* index及后面的元素左移一位,即从index开始移动,注意index从0开始, + * 即还要+1,则长度为size-((index)+1)+1 */ System.arraycopy(elementData, index, elementData, index+1, size-index); elementData[index]=o; @@ -35,8 +35,8 @@ public Object get(int index){ public Object remove(int index){ checkIndex(index); Object temp=elementData[index]; - /* indexԪһλ,index+1ʼƶעindex0ʼ - * Ҫ+1򳤶Ϊsize-((index+1)+1)+1 + /* index后面的元素左移一位,即从index+1开始移动,注意index从0开始, + * 即还要+1,则长度为size-((index+1)+1)+1 */ System.arraycopy(elementData, index+1, elementData, index, size-index-1); size--; @@ -62,30 +62,30 @@ public Object next(){ } } - //ж±ǷԽ粢ʾ// + //判断请求的下标是否越界并提示// private void checkIndex(int index){ if (index<0 || index >=size){ throw new IndexOutOfBoundsException("get " + index+" in "+size); } } - //жǷҪݲ// + //判断是否需要扩容并完成扩容// private void ensureCapcity(int size){ int oldLength=elementData.length; if (size>=oldLength){ - //util.ArrayListеĹʽԴʹõ12// + //util.ArrayList中的公式,源代码使用的右移1,即除2// int newLength=oldLength/2+oldLength; if (newLength7->10 , úΪ 10->7->3 + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ - /*ָ룬ʼʱָͷͷһ(ǰָ롢ָ) - *ѭÿƶ1ָƵβsize-1ڵҪƶ(size-1)-1 - *ȱǰֵָtempǰͷȻƶǰָ - *ƶ󣬽ǰָĽڵӵͷʼһƶ - *ѭע⵽ʵֻоĵڶڵ㵽ڸڵ㣬Ҫͷβڵ - *βڵҪӵͷͷڵָÿգȻ1<->2 - *άͷβ + /*两个指针,开始的时候指向头跟头后面的一个(前指针、后指针) + *循环:每次向后移动步长1,至后指针移到链表尾,size-1个节点需要移动(size-1)-1次 + *先保留前指针的值temp,即当前逆序链表的头,然后再移动前、后指针 + *移动后,将前指针的节点连接到逆序链表的头,开始下一次移动 + *循环结束后,注意到实际重连的只有旧链表的第二个节点到倒数第个节点,需要单独处理旧链表的头尾节点 + *旧链表的尾节点需要链接到逆序链表的头,旧链表的头节点的指针置空,不然会1<->2 + *维护头尾标记 */ Node current=head; Node currentAfter=current.next; @@ -161,12 +161,12 @@ public void reverse(){ } /** - * ɾһǰ벿 - * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 - * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 */ public void removeFirstHalf(){ - //intضϣС// + //int截断,不会有小数// int removeLength = size / 2; for (int i=1;i<=removeLength;i++){ removeFirst(); @@ -174,7 +174,7 @@ public void removeFirstHalf(){ } /** - * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 * @param i * @param length */ @@ -184,17 +184,17 @@ public void remove(int i, int length){ if (i+length-1>size){ length=size-i; } - //ӺǰɾֹԽ// + //从后往前删除,防止越界// for (int k=length;k>=i;k--){ remove(k); } } /** - * ٶǰlistе - * ӵǰȡЩlistָԪ - * 統ǰ = 11->101->201->301->401->501->601->701 + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 * listB = 1->3->4->6 - * صĽӦ[101,301,401,601] + * 返回的结果应该是[101,301,401,601] * @param list */ public int[] getElements(LinkedList list){ @@ -212,11 +212,11 @@ public int[] getElements(LinkedList list){ } /** - * ֪еԪֵУԵ洢ṹ - * ӵǰɾlistгֵԪ + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 * @param list */ - //ע⵽ѭlistʱ򣬱ڲѭıȽĽڵ±겢// + //注意到递增,在外层循环list的时候,保留内层循环的被比较链表的节点的下标并递增即可// public void subtract(LinkedList list){ int startIndex=0; Iterator iter=list.iterator(); @@ -235,10 +235,10 @@ public void subtract(LinkedList list){ } /** - * ֪ǰеԪֵУԵ洢ṹ - * ɾֵͬĶԪأʹòԱԪصֵͬ + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ - //ע⵽ҪɾĽڵ±겢// + //注意到递增,保留不需要删除的节点的下标并递增即可// public void removeDuplicateValues(){ int startIndex=1; int scr=(int)head.data; @@ -253,12 +253,12 @@ public void removeDuplicateValues(){ } /** - * ֪еԪֵУԵ洢ṹ - * дһЧ㷨ɾֵminСmaxԪأдԪأ + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) * @param min * @param max */ - //öַǵû뵽Ч㷨˵BȻᡣһһȽϵ// + //这个,想用二分法但是是单链表,没想到高效的算法(网上说是B树,然而不会。。。),一个一个比较的// public void removeRange(int min, int max){ Node current=head; Node temp=head; @@ -283,11 +283,11 @@ public void removeRange(int min, int max){ } /** - * 赱ǰͲlistָԪֵУͬһеԪֵͬ - * ҪCԪΪǰlistԪصĽұCеԪֵ + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 * @param list */ - //ע⵽ѭ±λõ// + //注意到递增,保留内循环下标位置递增即可// public LinkedList intersection( LinkedList list){ LinkedList result = new LinkedList(); int startIndex = 0; diff --git a/group22/910725683/week02/array/src/com/coderising/array/ArrayUtil.java b/group22/910725683/week02/array/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..347a230118 --- /dev/null +++ b/group22/910725683/week02/array/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int arrayLength = origin.length; + //注意整数截断,奇数长度不用额外处理 + for (int i = 0 ; i < arrayLength / 2 ; i++){ + int temp = origin[i]; + origin[i]=origin[arrayLength - i - 1]; + origin[arrayLength - i - 1] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int newArrayLength = 0; + for (int i : oldArray){ + if (i != 0){ + newArrayLength ++; + } + } + + int[] newArray = new int[newArrayLength]; + int newArrayIndex = 0; + for (int i : oldArray){ + if (i != 0){ + newArray[newArrayIndex++] = i; + } + } + + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + //注意此处算出来的newArrayLength是最大值,实际可能小于这个长度 + int newArrayLength = array1.length + array2.length; + int[] newArray = new int[newArrayLength]; + final int MAX_VALUE = Integer.MAX_VALUE; + int index1 = 0; + int index2 = 0; + int newArrayIndex = 0; + int element1; + int element2; + + //注意是两个数组都是递增的,可以交替步进比较,当大小翻转的时候交替,要求踢重,则相等的时候步进但不保存 + while(index1 < array1.length || index2 < array2.length){ + + //此处取巧的点在于已知数组是int型,故最大值是已知的,当步进到头时取最大值,即“钳位” + if (index1 < array1.length){ + element1 = array1[index1]; + }else{ + element1 = MAX_VALUE; + } + + if (index2 < array2.length){ + element2 = array2[index2]; + }else{ + element2 = MAX_VALUE; + } + + //谁小谁步进 + if (element1 < element2){ + newArray[newArrayIndex++] = element1; + index1++; + }else{ + if (element1 == element2){ + //相等后不再赋值给新数组 + index2++; + }else{ + newArray[newArrayIndex++] = element2; + index2++; + } + } + } + //移除没用到的位置 + return Arrays.copyOf(newArray, newArrayIndex); + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + //Java对引用类型自动赋值,故0不需要额外的处理 + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + ArrayList list = new ArrayList(); + int index = 0; + if (max >= 2){ + while( true ){ + int fibonacciNum = getFibonacci(index++); + if ( fibonacciNum < max ){ + list.add(fibonacciNum); + }else{ + break; + } + } + } + return list2Array(list); + } + + private int getFibonacci(int index){ + if (index == 0){ return 1; } + if (index == 1){ return 1; } + return getFibonacci(index - 2) + getFibonacci(index - 1); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList list = new ArrayList(); + + for (int i = 0 ; i < max ; i++){ + if (isPrime(i)){ list.add(i); } + } + + return list2Array(list); + } + + private boolean isPrime(int num){ + if (num == 0 || num == 1){ + return false; + } + for (int i = 2 ; i <= num / 2 ; i++){ + if (num % i == 0){ + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + ArrayList list = new ArrayList(); + + for (int i = 1 ; i < max ; i++){ + if (isPerfectNumbers(i)){ list.add(i); } + } + + return list2Array(list); + } + + private boolean isPerfectNumbers(int num){ + if (num == 1){ + return false; + } + int sum = 1; + //注意因子是对称的,故比较的上限是的平方根 + int sqr = (int) Math.sqrt(num); + for (int i = 2 ; i <= sqr ; i++){ + if (num % i == 0){ + sum = sum + i + num / i; + } + } + return sum == num; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0 ; i < array.length ; i++){ + sb.append(array[i]); + if (i != array.length - 1){ + sb.append(seperator); + } + } + return sb.toString(); + } + + private int[] list2Array(ArrayList list){ + int[] array = new int[list.size()]; + for (int i = 0 ; i < list.size() ; i++){ + array[i] = (int) list.get(i); + } + return array; + } +} \ No newline at end of file diff --git a/group22/910725683/week02/array/test/com/coderising/array/ArrayUtilTest.java b/group22/910725683/week02/array/test/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..483a537bd6 --- /dev/null +++ b/group22/910725683/week02/array/test/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,58 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayUtilTest { + + ArrayUtil au = new ArrayUtil(); + + @Test + public void testReverseArray() { + int[] a = new int[]{7, 9 , 30, 3}; + au.reverseArray(a); + Assert.assertArrayEquals(new int[]{3, 30, 9,7}, a); + int[] b = new int[]{7, 9, 30, 3, 4}; + au.reverseArray(b); + Assert.assertArrayEquals(new int[]{4,3, 30 , 9,7}, b); + } + + @Test + public void testRemoveZero() { + Assert.assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, au.removeZero(new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5})); + } + + @Test + public void testMerge() { + Assert.assertArrayEquals(new int[]{3,4,5,6,7,8}, au.merge(new int[]{3, 5, 7,8}, new int[]{4, 5, 6,7})); + } + + @Test + public void testGrow() { + Assert.assertArrayEquals(new int[]{2,3,6,0,0,0},au.grow(new int[]{2,3,6},3)); + } + + @Test + public void testFibonacci() { + Assert.assertArrayEquals(new int[]{1,1,2,3,5,8,13}, au.fibonacci(15)); + Assert.assertArrayEquals(new int[]{}, au.fibonacci(1)); + } + + @Test + public void testGetPrimes() { + Assert.assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, au.getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() { + Assert.assertArrayEquals(new int[]{6,28}, au.getPerfectNumbers(100)); + } + + @Test + public void testJoin() { + Assert.assertEquals("3-8-9", au.join(new int[]{3,8,9}, "-")); + } + +} diff --git a/group22/910725683/week02/litestruts/src/com/coderising/litestruts/LoginAction.java b/group22/910725683/week02/litestruts/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..eef38d702e --- /dev/null +++ b/group22/910725683/week02/litestruts/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是?个用来展示登录的业务类, 其中的用户名和密码都是硬编码的?? + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group22/910725683/week02/litestruts/src/com/coderising/litestruts/Struts.java b/group22/910725683/week02/litestruts/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..36f5f3838f --- /dev/null +++ b/group22/910725683/week02/litestruts/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,95 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.dom4j.Document; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + /* + 0. 读取配置文件struts.xml + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + View view = new View(); + + try{ + //读取strut.xml + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read("struts.xml"); + + try { + //XPath解析DOM树,获得actionName对应的类名称(XPath在爬虫里也非常好用) + Node actionClass = document.selectSingleNode("/struts/action[@name=\"" + actionName + "\"]/@class"); + Class classtype = Class.forName(actionClass.getText()); + Object obj = classtype.newInstance(); + + //按照parameters设置实例变量 + for (Entry entry : parameters.entrySet()){ + setter(obj,entry.getKey(),entry.getValue(),String.class); + } + try{ + //执行execute方法 + Method met = classtype.getMethod("execute"); + String result = (String) met.invoke(obj); + //获得执行后实例变量的值 + HashMap hashmap = new HashMap(); + for(Method m : classtype.getMethods() ) { + String methodName = m.getName(); + if (methodName.startsWith("get")){ + hashmap.put(methodName.substring(3,4).toLowerCase() + methodName.substring(4), m.invoke(obj).toString()); + } + } + view.setParameters(hashmap); + //获得执行结果对应的jsp + Node jsp = document.selectSingleNode("/struts/action[@name=\"" + actionName + "\"]/result[@name=\"" + result + "\"]"); + view.setJsp(jsp.getText()); + + } + catch(Exception e){ + e.printStackTrace(); + } + } + catch (Exception e){ + e.printStackTrace(); + } + }catch (Exception e) { + e.printStackTrace(); + } + return view; + } + + //按照参数名运行setter方法 + private static void setter(Object obj,String att,Object value,Class type){ + try{ + Method met = obj.getClass().getMethod("set"+initStr(att),type); + met.invoke(obj,value) ; + }catch(Exception e){ + e.printStackTrace() ; + } + } + + //小写驼峰命名法 + private static String initStr(String str){ + char[] ch = str.toCharArray(); + if (ch[0] >= 'a' && ch[0] <= 'z') { + ch[0] = (char) (ch[0] - 32); + } + return new String(ch); + } + +} diff --git a/group22/910725683/week02/litestruts/src/com/coderising/litestruts/View.java b/group22/910725683/week02/litestruts/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group22/910725683/week02/litestruts/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/910725683/week02/litestruts/test/com/coderising/litestruts/StrutsTest.java b/group22/910725683/week02/litestruts/test/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..7e45e699df --- /dev/null +++ b/group22/910725683/week02/litestruts/test/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一�? + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group23/565832157/src/com/coding/basic/LinkedList.java b/group23/565832157/src/com/coding/basic/LinkedList.java index 09fe0a8ff3..77657e57c1 100644 --- a/group23/565832157/src/com/coding/basic/LinkedList.java +++ b/group23/565832157/src/com/coding/basic/LinkedList.java @@ -1,3 +1,145 @@ +<<<<<<< HEAD:liuxin/src/com/coding/basic/LinkedList.java +package com.coding.basic; + +public class LinkedList implements List { + private int size; + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + + return size; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + } + + /** + * 把该链表逆置 + * head head + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + /** + * 长度超过1的单链表需要逆转 + */ + if(size>1){ + Node pre = head; + Node cur = head.next; + Node next = null; + while(cur!=null){ + next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + } + + } + } + + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始,删除length个元素 ,注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + + return null; + } +} +======= package com.coding.basic; public class LinkedList implements List { @@ -120,3 +262,4 @@ public LinkedList intersection( LinkedList list){ return null; } } +>>>>>>> upstream/master:group23/565832157/src/com/coding/basic/LinkedList.java diff --git a/group24/1148285693/learning2017/common/src/main/java/me/lzb/common/utils/ByteUtils.java b/group24/1148285693/learning2017/common/src/main/java/me/lzb/common/utils/ByteUtils.java new file mode 100644 index 0000000000..f6586ce761 --- /dev/null +++ b/group24/1148285693/learning2017/common/src/main/java/me/lzb/common/utils/ByteUtils.java @@ -0,0 +1,26 @@ +package me.lzb.common.utils; + +/** + * Created by LZB on 2017/4/14. + */ +public class ByteUtils { + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i + + me.lzb + common + 1.0 + diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/algorithm/LRUPageFrame.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/algorithm/LRUPageFrame.java deleted file mode 100644 index 1f1d0a36b7..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/algorithm/LRUPageFrame.java +++ /dev/null @@ -1,173 +0,0 @@ -package me.lzb.algorithm; - -/** - * 用双向链表实现LRU算法 - * @author lzb - * - */ -public class LRUPageFrame { - - private static class Node { - - Node prev; - Node next; - int pageNum; - - Node(Node prev, Node next, int pageNum) { - this.prev = prev; - this.next = next; - this.pageNum = pageNum; - } - } - - private int capacity; - - - private Node first;// 链表头 - private Node last;// 链表尾 - - - public LRUPageFrame(int capacity) { - if(capacity < 1){ -// throw new Exception("capacity boom"); - } - this.capacity = capacity; - - } - - /** - * 获取缓存中对象 - * - * @param pageNum - * @return - */ - public void access(int pageNum) { - if(capacity == 1){ - first = last = new Node(null, null, pageNum); - return; - } - - - if(first == null){ - first = last = new Node(null, null, pageNum); - return; - } - - if(first.pageNum == pageNum){ - return; - } - - Node tmp = first; - int size = 0; - for (int i = 0; i < capacity; i++) { - size = size + 1; - //如果发现一样的,把这个挪到最前面 - if(tmp.pageNum == pageNum){ - moveToFirst(tmp); - return; - } - //链表已经循环结束,但是个数还没满,更新last - if(tmp.next == null){ - last = tmp; - break; - } - tmp = tmp.next; - } - - - //没有相同的,在最顶端插入 - Node f = new Node(null, first, pageNum); - addAsFirst(f); - - //已经放满,更新last - if(size >= capacity){ - removeLastOne(); - } - - } - - /** - * 删除最后一个节点 - */ - private void removeLastOne(){ - last = last.prev; - //使GC ROOT 不可达 - last.next.prev = null; - last.next = null; - } - - /** - * 把某节点移动到最顶部 - * @param tmp 在链表中的任意节点 - */ - private void moveToFirst(Node tmp){ - if(tmp == first){ - return; - } - - if (tmp.next != null){ - tmp.next.prev = tmp.prev; - tmp.prev.next = tmp.next; - }else { - tmp.prev.next = null; - //当这个节点是last的时候,更新last - last = tmp.prev; - } - - tmp.next = first; - tmp.prev = null; - - first.prev = tmp; - first = tmp; - } - - /** - * 在顶部增加一个节点 - * @param node node - */ - private void addAsFirst(Node node){ - first.prev = node; - first = node; - } - - - - /** - * ASC - * @return - */ - public String toString(){ - StringBuilder buffer = new StringBuilder(); - Node node = first; - while(node != null){ - buffer.append(node.pageNum); - - node = node.next; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } - - - /** - * DESC - * @return - */ - public String toStringDESC(){ - - StringBuilder buffer = new StringBuilder(); - Node node = last; - while(node != null){ - buffer.append(node.pageNum); - - node = node.prev; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } - -} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java new file mode 100644 index 0000000000..af897cd58f --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java @@ -0,0 +1,97 @@ +package me.lzb.basic; + +/** + * 简易ArrayList + * Created by LZB on 2017/3/11. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = {}; + + + public void add(Object o) { +// if (elementData.length < size + 1) { +// Object[] target = new Object[size + 1]; +// System.arraycopy(elementData, 0, target, 0, elementData.length); +// elementData = target; +// } +// elementData[size] = o; +// size = size + 1; + add(size, o); + } + + + public void add(int index, Object o) throws IndexOutOfBoundsException { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index boom"); + } + + int leftSize = index; + int rightSize = size - index; + Object[] target = new Object[size + 1]; + System.arraycopy(elementData, 0, target, 0, leftSize); + target[index] = o; + System.arraycopy(elementData, index, target, index + 1, rightSize); + elementData = target; + size = size + 1; + } + + public Object get(int index) throws IndexOutOfBoundsException { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index boom"); + } + return elementData[index]; + } + + public Object remove(int index) throws IndexOutOfBoundsException { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index boom"); + } + Object removeObject = elementData[index]; + int leftSize = index; + int rightSize = size - index - 1; + Object[] target = new Object[elementData.length - 1]; + System.arraycopy(elementData, 0, target, 0, leftSize); + System.arraycopy(elementData, index + 1, target, index, rightSize); + elementData = target; + size = size - 1; + return removeObject; + } + + public int size() { + return size; + } + + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private ArrayList arrayList; + + int pos = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + if (pos >= arrayList.size) { + return false; + } + + return true; + } + + @Override + public Object next() { + Object result = arrayList.get(pos); + pos = pos + 1; + return result; + } + } +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java new file mode 100644 index 0000000000..eab32c80cc --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java @@ -0,0 +1,337 @@ +package me.lzb.basic; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] target = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + target[i] = origin[origin.length - 1 - i]; + } + origin = target; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int l = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + l = l + 1; + } + } + + int[] target = new int[l]; + + int a = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + target[a] = oldArray[i]; + a = a + 1; + } + } + + return target; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + //一个一个放进去,循环次数有点多 + public int[] merge(int[] array1, int[] array2) { + + + int[] tmp = new int[array1.length + array2.length]; + + + int mini = 0; + int a1 = array1[0]; + int a2 = array2[0]; + + if(a1 < a2){ + mini = a1; + }else { + mini = a2; + } + + tmp[0] = mini; + + int l3 = 0; + + for (int i = 1; i < array1.length + array2.length; i++) { + +// if(mini >= array1[l1 - 1] && mini >= array2[l2 - 1]){ +// l3 = i; +// break; +// } + + int oldMin = mini; + + + + int aa1 = mini; + if(mini < array1[array1.length - 1] ){ + for (int j = 0; j < array1.length; j++) { + if(array1[j] > mini){ + aa1 = array1[j]; + break; + } + } + + } + + int aa2 = mini; + if(mini < array2[array2.length - 1] ){ + for (int j = 0; j < array2.length; j++) { + if(array2[j] > mini){ + aa2 = array2[j]; + break; + } + } + } + + + if(aa1 != oldMin && aa2 != oldMin){ + if(aa1 < aa2){ + mini = aa1; + }else { + mini = aa2; + } + }else if(aa1 != oldMin){ + mini = aa1; + }else { + mini = aa2; + } + + + if(oldMin == mini){ + l3 = i; + break; + } + + tmp[i] = mini; + } + + int[] result = new int[l3]; + + System.arraycopy(tmp, 0, result, 0, l3); + + + + return result; + } + + + + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int newArray[] = new int[oldArray.length + size]; + + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1){ + return new int[0]; + } + + int[] result = {1, 1}; + + + int i = 2; + + int n = 0; + + while (n < max){ + int[] t = new int[result.length + 1]; + System.arraycopy(result, 0, t, 0, result.length); + n = t[i-1] + t[i - 2]; + + if(n >= max){ + return result; + } + + t[i] = n; + + result = t; + i = i + 1; + } + + return null; + } + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max <= 2){ + return new int[0]; + } + + if (max == 3){ + return new int[]{2}; + } + + + int[] primes = new int[max+1]; + primes[0] = 2; + int count = 1; + for (int i = 3; i < max; i = i + 2) { + + boolean isPrime = true; + for (int j = 3; j < i; j++) { + if(i % j == 0){ + isPrime = false; + break; + } + } + + if(isPrime){ + primes[count] = i; + count = count + 1; + } + } + + int[] result = new int[count]; + System.arraycopy(primes, 0, result, 0, count); + + return result; + + } + + private boolean isPrime(int a){ + if (a < 2) { + return false; + } + + if (a == 2) { + return true; + } + + if(a % 2 == 0){ + return false; + } + + + for (int i = 3; i < a; i = i + 2) { + if(a % i == 0){ + return false; + } + } + + return true; + } + + + + /** + * 所谓“完数”, 是指这个数恰好等于它的真因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 6){ + return new int[0]; + } + + + int[] pns = new int[max]; + + int count = 0; + for (int i = 6; i < max; i++) { + if (isPerfectNumber(i)){ + pns[count] = i; + count = count + 1; + } + } + + + + int[] result = new int[count]; + System.arraycopy(pns, 0, result, 0, count); + return result; + } + + + private boolean isPerfectNumber(int a){ + if(a < 6){ + return false; + } + + int sum = 0; + for (int i = 1; i < a; i++) { + if(a % i == 0){ + sum = sum + i; + } + } + + return sum == a; + } + + + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @return + */ + public static String join(int[] array, String seperator) { + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator ; + } + + result = result.substring(0, result.length() - 1); + return result; + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..88395e3010 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package me.lzb.basic; + +/** + * 左边比父节点小,右边比父节点大 + * Created by LZB on 2017/3/11. + */ +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(int data){ + this.data = data; + } + + public int getData() { + return data; + } + + + //这层满了就下一层继续add,直到找到空位 + public void add(int d){ + BinaryTreeNode b = new BinaryTreeNode(d); + if(compareTo(b)){ + //比父节点小,左边 + if(this.left == null){ + this.left = b; + }else { + this.left.add(d); + } + + }else {//相等不考虑 + //比父节点大,右边 + if(this.right == null){ + this.right = b; + }else { + this.right.add(d); + } + + } + } + + + public boolean compareTo(BinaryTreeNode node){ + if(this.data > node.getData()){ + return true; + } + return false; + } + + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/InfixExpr.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/InfixExpr.java new file mode 100644 index 0000000000..e30cc00cc2 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/InfixExpr.java @@ -0,0 +1,161 @@ +package me.lzb.basic; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * Created by LZB on 2017/4/15. + */ +public class InfixExpr { + + + private String expr; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + List list = processExpr(); + + Stack symbolStack = new Stack<>(); + Stack numberStack = new Stack<>(); + + boolean calLevel2 = false; + for (Node n : list) { + if (n.isNumber) { + numberStack.push(n.number); + if (calLevel2) { + calculate(symbolStack, numberStack, false); + calLevel2 = false; + } + } else { + symbolStack.push(n.symbol); + + if (n.isLevel2()) { + calLevel2 = true; + } + } + } + + + Stack tn = new Stack<>(); + int nsize = numberStack.size(); + for (int i = 0; i < nsize; i++) { + tn.push(numberStack.pop()); + } + + numberStack = tn; + + + Stack ts = new Stack<>(); + int ssize = symbolStack.size(); + for (int i = 0; i < ssize; i++) { + ts.push(symbolStack.pop()); + } + + symbolStack = ts; + + + while (!symbolStack.isEmpty()) { + calculate(symbolStack, numberStack, true); + } + + + return numberStack.pop(); + } + + + + private List processExpr() { + List list = new ArrayList<>(); + char[] array = this.expr.toCharArray(); + String number = ""; + for (int i = 0; i < array.length; i++) { + if (Character.isDigit(array[i])) { + number = number + String.valueOf(array[i]); + } else { + Node num = new Node(Float.valueOf(number), null, true, -1); + number = ""; + int calLevel = "+-".indexOf(array[i]) >= 0 ? 1 : 2; + Node sym = new Node(0, String.valueOf(array[i]), false, calLevel); + list.add(num); + list.add(sym); + } + } + + Node num = new Node(Float.valueOf(number), null, true, -1); + list.add(num); + return list; + } + + + private void calculate(Stack symbolStack, Stack numberStack, boolean isRe) { + if (symbolStack.isEmpty()) { + return; + } + + + String symbole = symbolStack.pop(); + + float right; + float left; + + if(isRe){ + left = numberStack.pop(); + right = numberStack.pop(); + }else { + right = numberStack.pop(); + left = numberStack.pop(); + } + + + + float r = calculate(symbole, left, right); + + numberStack.push(r); + } + + + private float calculate(String symbol, float left, float right) { + if ("+".equals(symbol)) { + return left + right; + } + + if ("-".equals(symbol)) { + return left - right; + } + + if ("*".equals(symbol)) { + return left * right; + } + + if ("/".equals(symbol)) { + return left / right; + } + + return 0; + } + + + private class Node { + float number; + String symbol; + boolean isNumber; + int calLevel;//加减1,乘除2 + + public Node(float number, String symbol, boolean isNumber, int calLevel) { + this.number = number; + this.symbol = symbol; + this.isNumber = isNumber; + this.calLevel = calLevel; + } + + private boolean isLevel2() { + return calLevel == 2; + } + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java new file mode 100644 index 0000000000..86e8cae942 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java @@ -0,0 +1,10 @@ +package me.lzb.basic; + +/** + * Created by LZB on 2017/3/11. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LRUPageFrame.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LRUPageFrame.java new file mode 100644 index 0000000000..6e0e570ce6 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LRUPageFrame.java @@ -0,0 +1,173 @@ +package me.lzb.basic; + +/** + * 用双向链表实现LRU算法 + * @author lzb + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node(Node prev, Node next, int pageNum) { + this.prev = prev; + this.next = next; + this.pageNum = pageNum; + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + if(capacity < 1){ +// throw new Exception("capacity boom"); + } + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if(capacity == 1){ + first = last = new Node(null, null, pageNum); + return; + } + + + if(first == null){ + first = last = new Node(null, null, pageNum); + return; + } + + if(first.pageNum == pageNum){ + return; + } + + Node tmp = first; + int size = 0; + for (int i = 0; i < capacity; i++) { + size = size + 1; + //如果发现一样的,把这个挪到最前面 + if(tmp.pageNum == pageNum){ + moveToFirst(tmp); + return; + } + //链表已经循环结束,但是个数还没满,更新last + if(tmp.next == null){ + last = tmp; + break; + } + tmp = tmp.next; + } + + + //没有相同的,在最顶端插入 + Node f = new Node(null, first, pageNum); + addAsFirst(f); + + //已经放满,更新last + if(size >= capacity){ + removeLastOne(); + } + + } + + /** + * 删除最后一个节点 + */ + private void removeLastOne(){ + last = last.prev; + //使GC ROOT 不可达 + last.next.prev = null; + last.next = null; + } + + /** + * 把某节点移动到最顶部 + * @param tmp 在链表中的任意节点 + */ + private void moveToFirst(Node tmp){ + if(tmp == first){ + return; + } + + if (tmp.next != null){ + tmp.next.prev = tmp.prev; + tmp.prev.next = tmp.next; + }else { + tmp.prev.next = null; + //当这个节点是last的时候,更新last + last = tmp.prev; + } + + tmp.next = first; + tmp.prev = null; + + first.prev = tmp; + first = tmp; + } + + /** + * 在顶部增加一个节点 + * @param node node + */ + private void addAsFirst(Node node){ + first.prev = node; + first = node; + } + + + + /** + * ASC + * @return + */ + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + /** + * DESC + * @return + */ + public String toStringDESC(){ + + StringBuilder buffer = new StringBuilder(); + Node node = last; + while(node != null){ + buffer.append(node.pageNum); + + node = node.prev; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LinkedList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LinkedList.java new file mode 100644 index 0000000000..268b69bf50 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LinkedList.java @@ -0,0 +1,510 @@ +package me.lzb.basic; + + +/** + * 简易LinkedList + * Created by LZB on 2017/3/11. + */ +public class LinkedList implements List { + + private int size = 0; + + + private Node first; + + private Node last; + + + private static class Node { + Object data; + Node next; +// int intData; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } +// public Node(Object data, Node next, int i) { +// this.data = data; +// this.next = next; +// this.intData = i; +// } + } + + +// public void add(int i) { +// if (first == null) { +// first = new Node(null, null, i); +// last = first; +// } else { +// Node n = new Node(null, null, i); +// last.next = n; +// last = n; +// } +// size = size + 1; +// } + public void add(Object o) { + if (first == null) { + first = new Node(o, null); + last = first; + } else { + Node n = new Node(o, null); + last.next = n; + last = n; + } + size = size + 1; + } +// +// public void addInt(int i) { +// if (first == null) { +// first = new Node(null, null, i); +// last = first; +// } else { +// Node n = new Node(null, null, i); +// last.next = n; +// last = n; +// } +// size = size + 1; +// } + + + public void add(int index, Object o) throws IndexOutOfBoundsException { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index boom"); + } + + if (index == size) { + add(o); + return; + } + + if (index == 0) { + Node n = new Node(0, first); + first = n; + size = size + 1; + return; + } + + Node before = first; + for (int i = 0; i < index - 1; i++) { + before = before.next; + } + + Node after = before.next; + + Node n = new Node(o, after); + + before.next = n; + + size = size + 1; + + } + + private Node getNode(int index){ + if (size == 0 || index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index boom"); + } + Node result = first; + for (int i = 0; i < index; i++) { + result = result.next; + } + return result; + } + + + public Object get(int index) { + return getNode(index).data; + } + + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index boom"); + } + + if (size == 1) { + Node result = last; + last = null; + first = null; + size = size - 1; + return result.data; + } + + if (index == size - 1) { + Node result = last; + last = null; + size = size - 1; + return result.data; + } + + + if (index == 0) { + Node result = first; + Node second = first.next; + first = second; + size = size - 1; + return result.data; + } + + + Node before = first; + for (int i = 0; i < index - 1; i++) { + before = before.next; + } + Node result = before.next; + Node after = before.next.next; + before.next = after; + size = size - 1; + return result.data; + } + + + + + + + + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0, o); + } + + + public void addLast(Object o) { + add(o); + } + + + public Object removeFirst() { + return remove(0); + } + + + public Object removeLast() { + return remove(size); + } + + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + + private class LinkedListIterator implements Iterator { + private LinkedList linkedList; + + int pos = 0; + + private LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + + if (pos >= linkedList.size) { + return false; + } + return true; + } + + @Override + public Object next() { + Object result = linkedList.get(pos); + pos = pos + 1; + return result; + } + } + + + //后面的方法先不用写的说 + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + //还可以用堆栈 先进后出 + + if(size() <= 1){ + return; + } + Object[] array = new Object[size]; + Node tmp = first; + for (int i = 0; i < size; i++) { + array[i] = tmp.data; + tmp = tmp.next; + } + this.first = null; + this.last = null; + for (int i = array.length - 1; i >= 0 ; i--) { + add(array[i]); + } + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + + if (size <= 1){ + return; + } + int b = size/ 2; + Node n = getNode(b); + this.first = n; + size = (size % 2) + b; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (size == 0 || i < 0 || i >= size){ + return; + } + + length = size - i >= length ? length : size - i; + + + if(i + length == size){ + this.first = null; + this.last = null; + size = 0; + return; + } + + if(i == 0){ + Node n = getNode(length); + first = n; + size = size - length; + return; + } + + + Node a = getNode(i - 1); + Node b = getNode(i + length); + a.next = b; + size = size - length; + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + + if(size <= 0 || list.size() <= 0){ + return new int[0]; + } + + + + int[] result = new int[list.size()]; + + Node tmp = list.first; + int index = 0; + Node tmp2 = first; + for (int i = 0; i < list.size(); i++) { + int newIndex = (int)tmp.data; + int maxJ = newIndex - index; + for (int j = 0; j <= maxJ; j++) { + + if(j == maxJ){ + result[i] = (int)tmp2.data; + break; + } + tmp2 = tmp2.next; + } + index = newIndex; + tmp = tmp.next; + } + + size = size - list.size(); + + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + for (int i = 0; i < list.size(); i++) { + this.remove(list.get(i)); + } + + } + + + public void remove(Object obj){ + if(size <= 0){ + return; + } + if(first.data.equals(obj)){ + first=first.next; + size = size - 1; + return; + } + Node tmp = first; + Node tmp2 = first.next; + for (int i = 1; i < size; i++) { + if(tmp2.data.equals(obj)){ + tmp.next = tmp2.next; + size = size - 1; + return; + } + tmp = tmp.next; + tmp2 = tmp2.next; + } + + } + + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if(size <= 1){ + return; + } + + Node tmp = first; + for (int i = 1; i < size; i++) { + if(tmp.next == null){ + break; + } + if (tmp.data.equals(tmp.next.data)){ + tmp.next = tmp.next.next; + } + tmp = tmp.next; + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + if(size <= 0){ + return; + } + + Node tmp = first; + int a = -1; + int b = -1; + for (int i = 0; i < size; i++) { + if((int)tmp.data > min && a == -1){ + a = i; + } + + if((int)tmp.data >= max && b == -1){ + b = i; + } + + tmp = tmp.next; + } + + + if(min < max){ + remove(a, b - a); + return; + + } + + + if(min == max){ + + } + + + if(min > max){ + + } + + + + return; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList result = new LinkedList(); + + if(list == null || list.size <= 0 || size <= 0){ + return result; + } + + int i1 = 0; + int i2 = 0; + + while( i1 < this.size && i2 s) { + + + } + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + Stack t = new Stack<>(); + for (Integer i : s) { + t.push(i); + } + + s.clear(); + + int size = t.size(); + for (int i = 0; i < size; i++) { + s.push(t.pop()); + } + + } + + public static void addToBottom(Stack s, Integer value) { + Stack t = new Stack<>(); + int size = s.size(); + for (int i = 0; i < size; i++) { + t.push(s.pop()); + } + + s.clear(); + + s.push(value); + + for (int i = 0; i < size; i++) { + s.push(t.pop()); + } + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s, Object o) { + Stack t = new Stack(); + int size = s.size(); + for (int i = 0; i < size; i++) { + Object ro = s.pop(); + if (!ro.equals(o)) { + t.push(ro); + } + } + + s.clear(); + + int sizet = t.size(); + for (int i = 0; i < sizet; i++) { + s.push(t.pop()); + } + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param len + * @return + */ + public static Object[] getTop(Stack s, int len) { + Object[] array = new Object[len]; + for (int i = 0; i < len; i++) { + array[i] = s.pop(); + } + + for (int i = len - 1; i >= 0; i--) { + s.push(array[i]); + } + return array; + } + + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * + * @param s + * @return + */ + public static boolean isValidPairs(String s) { + char[] array = s.toCharArray(); + Stack stack = new Stack<>(); + for (int i = 0; i < array.length; i++) { + stack.push(String.valueOf(array[i])); + } + + + int a = -1; + int b = -1; + int c = -1; + + + for (int i = 0; i < array.length; i++) { + String cc = stack.pop(); + + if ("{}".indexOf(cc) >= 0) { + if (a == -1) { + a = i; + } else { + if (stack.size() != a) { + return false; + } + } + } + + if ("[]".indexOf(cc) >= 0) { + + if (b == -1) { + b = i; + } else { + if (stack.size() != b) { + return false; + } + } + + } + + if ("()".indexOf(cc) >= 0) { + + if (c == -1) { + c = i; + } else { + if (stack.size() != c) { + return false; + } + } + + } + + } + return true; + } + + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayList.java deleted file mode 100644 index 080aa4f724..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayList.java +++ /dev/null @@ -1,97 +0,0 @@ -package me.lzb.datastructure; - -/** - * 简易ArrayList - * Created by LZB on 2017/3/11. - */ -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = {}; - - - public void add(Object o) { -// if (elementData.length < size + 1) { -// Object[] target = new Object[size + 1]; -// System.arraycopy(elementData, 0, target, 0, elementData.length); -// elementData = target; -// } -// elementData[size] = o; -// size = size + 1; - add(size, o); - } - - - public void add(int index, Object o) throws IndexOutOfBoundsException { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("index boom"); - } - - int leftSize = index; - int rightSize = size - index; - Object[] target = new Object[size + 1]; - System.arraycopy(elementData, 0, target, 0, leftSize); - target[index] = o; - System.arraycopy(elementData, index, target, index + 1, rightSize); - elementData = target; - size = size + 1; - } - - public Object get(int index) throws IndexOutOfBoundsException { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index boom"); - } - return elementData[index]; - } - - public Object remove(int index) throws IndexOutOfBoundsException { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index boom"); - } - Object removeObject = elementData[index]; - int leftSize = index; - int rightSize = size - index - 1; - Object[] target = new Object[elementData.length - 1]; - System.arraycopy(elementData, 0, target, 0, leftSize); - System.arraycopy(elementData, index + 1, target, index, rightSize); - elementData = target; - size = size - 1; - return removeObject; - } - - public int size() { - return size; - } - - - public Iterator iterator() { - return new ArrayListIterator(this); - } - - private class ArrayListIterator implements Iterator { - private ArrayList arrayList; - - int pos = 0; - - private ArrayListIterator(ArrayList arrayList) { - this.arrayList = arrayList; - } - - @Override - public boolean hasNext() { - if (pos >= arrayList.size) { - return false; - } - - return true; - } - - @Override - public Object next() { - Object result = arrayList.get(pos); - pos = pos + 1; - return result; - } - } -} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayUtil.java deleted file mode 100644 index de101845aa..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayUtil.java +++ /dev/null @@ -1,337 +0,0 @@ -package me.lzb.datastructure; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * - * @param origin - * @return - */ - public void reverseArray(int[] origin) { - int[] target = new int[origin.length]; - for (int i = 0; i < origin.length; i++) { - target[i] = origin[origin.length - 1 - i]; - } - origin = target; - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray) { - int l = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) { - l = l + 1; - } - } - - int[] target = new int[l]; - - int a = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) { - target[a] = oldArray[i]; - a = a + 1; - } - } - - return target; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * - * @param array1 - * @param array2 - * @return - */ - - //一个一个放进去,循环次数有点多 - public int[] merge(int[] array1, int[] array2) { - - - int[] tmp = new int[array1.length + array2.length]; - - - int mini = 0; - int a1 = array1[0]; - int a2 = array2[0]; - - if(a1 < a2){ - mini = a1; - }else { - mini = a2; - } - - tmp[0] = mini; - - int l3 = 0; - - for (int i = 1; i < array1.length + array2.length; i++) { - -// if(mini >= array1[l1 - 1] && mini >= array2[l2 - 1]){ -// l3 = i; -// break; -// } - - int oldMin = mini; - - - - int aa1 = mini; - if(mini < array1[array1.length - 1] ){ - for (int j = 0; j < array1.length; j++) { - if(array1[j] > mini){ - aa1 = array1[j]; - break; - } - } - - } - - int aa2 = mini; - if(mini < array2[array2.length - 1] ){ - for (int j = 0; j < array2.length; j++) { - if(array2[j] > mini){ - aa2 = array2[j]; - break; - } - } - } - - - if(aa1 != oldMin && aa2 != oldMin){ - if(aa1 < aa2){ - mini = aa1; - }else { - mini = aa2; - } - }else if(aa1 != oldMin){ - mini = aa1; - }else { - mini = aa2; - } - - - if(oldMin == mini){ - l3 = i; - break; - } - - tmp[i] = mini; - } - - int[] result = new int[l3]; - - System.arraycopy(tmp, 0, result, 0, l3); - - - - return result; - } - - - - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param size - * @return - */ - public int[] grow(int[] oldArray, int size) { - int newArray[] = new int[oldArray.length + size]; - - System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * - * @param max - * @return - */ - public int[] fibonacci(int max) { - if (max <= 1){ - return new int[0]; - } - - int[] result = {1, 1}; - - - int i = 2; - - int n = 0; - - while (n < max){ - int[] t = new int[result.length + 1]; - System.arraycopy(result, 0, t, 0, result.length); - n = t[i-1] + t[i - 2]; - - if(n >= max){ - return result; - } - - t[i] = n; - - result = t; - i = i + 1; - } - - return null; - } - - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * - * @param max - * @return - */ - public int[] getPrimes(int max) { - if (max <= 2){ - return new int[0]; - } - - if (max == 3){ - return new int[]{2}; - } - - - int[] primes = new int[max+1]; - primes[0] = 2; - int count = 1; - for (int i = 3; i < max; i = i + 2) { - - boolean isPrime = true; - for (int j = 3; j < i; j++) { - if(i % j == 0){ - isPrime = false; - break; - } - } - - if(isPrime){ - primes[count] = i; - count = count + 1; - } - } - - int[] result = new int[count]; - System.arraycopy(primes, 0, result, 0, count); - - return result; - - } - - private boolean isPrime(int a){ - if (a < 2) { - return false; - } - - if (a == 2) { - return true; - } - - if(a % 2 == 0){ - return false; - } - - - for (int i = 3; i < a; i = i + 2) { - if(a % i == 0){ - return false; - } - } - - return true; - } - - - - /** - * 所谓“完数”, 是指这个数恰好等于它的真因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * - * @param max - * @return - */ - public int[] getPerfectNumbers(int max) { - if (max < 6){ - return new int[0]; - } - - - int[] pns = new int[max]; - - int count = 0; - for (int i = 6; i < max; i++) { - if (isPerfectNumber(i)){ - pns[count] = i; - count = count + 1; - } - } - - - - int[] result = new int[count]; - System.arraycopy(pns, 0, result, 0, count); - return result; - } - - - private boolean isPerfectNumber(int a){ - if(a < 6){ - return false; - } - - int sum = 0; - for (int i = 1; i < a; i++) { - if(a % i == 0){ - sum = sum + i; - } - } - - return sum == a; - } - - - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * - * @param array - * @return - */ - public static String join(int[] array, String seperator) { - String result = ""; - for (int i = 0; i < array.length; i++) { - result = result + array[i] + seperator ; - } - - result = result.substring(0, result.length() - 1); - return result; - } - -} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/BinaryTreeNode.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/BinaryTreeNode.java deleted file mode 100644 index dfcaa60300..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/BinaryTreeNode.java +++ /dev/null @@ -1,53 +0,0 @@ -package me.lzb.datastructure; - -/** - * 左边比父节点小,右边比父节点大 - * Created by LZB on 2017/3/11. - */ -public class BinaryTreeNode { - - private int data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(int data){ - this.data = data; - } - - public int getData() { - return data; - } - - - //这层满了就下一层继续add,直到找到空位 - public void add(int d){ - BinaryTreeNode b = new BinaryTreeNode(d); - if(compareTo(b)){ - //比父节点小,左边 - if(this.left == null){ - this.left = b; - }else { - this.left.add(d); - } - - }else {//相等不考虑 - //比父节点大,右边 - if(this.right == null){ - this.right = b; - }else { - this.right.add(d); - } - - } - } - - - public boolean compareTo(BinaryTreeNode node){ - if(this.data > node.getData()){ - return true; - } - return false; - } - - -} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/Iterator.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/Iterator.java deleted file mode 100644 index b65f73b2e3..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/Iterator.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.lzb.datastructure; - -/** - * Created by LZB on 2017/3/11. - */ -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/LinkedList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/LinkedList.java deleted file mode 100644 index f79b7eaf18..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/LinkedList.java +++ /dev/null @@ -1,510 +0,0 @@ -package me.lzb.datastructure; - - -/** - * 简易LinkedList - * Created by LZB on 2017/3/11. - */ -public class LinkedList implements List { - - private int size = 0; - - - private Node first; - - private Node last; - - - private static class Node { - Object data; - Node next; -// int intData; - - public Node(Object data, Node next) { - this.data = data; - this.next = next; - } -// public Node(Object data, Node next, int i) { -// this.data = data; -// this.next = next; -// this.intData = i; -// } - } - - -// public void add(int i) { -// if (first == null) { -// first = new Node(null, null, i); -// last = first; -// } else { -// Node n = new Node(null, null, i); -// last.next = n; -// last = n; -// } -// size = size + 1; -// } - public void add(Object o) { - if (first == null) { - first = new Node(o, null); - last = first; - } else { - Node n = new Node(o, null); - last.next = n; - last = n; - } - size = size + 1; - } -// -// public void addInt(int i) { -// if (first == null) { -// first = new Node(null, null, i); -// last = first; -// } else { -// Node n = new Node(null, null, i); -// last.next = n; -// last = n; -// } -// size = size + 1; -// } - - - public void add(int index, Object o) throws IndexOutOfBoundsException { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("index boom"); - } - - if (index == size) { - add(o); - return; - } - - if (index == 0) { - Node n = new Node(0, first); - first = n; - size = size + 1; - return; - } - - Node before = first; - for (int i = 0; i < index - 1; i++) { - before = before.next; - } - - Node after = before.next; - - Node n = new Node(o, after); - - before.next = n; - - size = size + 1; - - } - - private Node getNode(int index){ - if (size == 0 || index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index boom"); - } - Node result = first; - for (int i = 0; i < index; i++) { - result = result.next; - } - return result; - } - - - public Object get(int index) { - return getNode(index).data; - } - - - public Object remove(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("index boom"); - } - - if (size == 1) { - Node result = last; - last = null; - first = null; - size = size - 1; - return result.data; - } - - if (index == size - 1) { - Node result = last; - last = null; - size = size - 1; - return result.data; - } - - - if (index == 0) { - Node result = first; - Node second = first.next; - first = second; - size = size - 1; - return result.data; - } - - - Node before = first; - for (int i = 0; i < index - 1; i++) { - before = before.next; - } - Node result = before.next; - Node after = before.next.next; - before.next = after; - size = size - 1; - return result.data; - } - - - - - - - - - public int size() { - return size; - } - - public void addFirst(Object o) { - add(0, o); - } - - - public void addLast(Object o) { - add(o); - } - - - public Object removeFirst() { - return remove(0); - } - - - public Object removeLast() { - return remove(size); - } - - - public Iterator iterator() { - return new LinkedListIterator(this); - } - - - private class LinkedListIterator implements Iterator { - private LinkedList linkedList; - - int pos = 0; - - private LinkedListIterator(LinkedList linkedList) { - this.linkedList = linkedList; - } - - @Override - public boolean hasNext() { - - if (pos >= linkedList.size) { - return false; - } - return true; - } - - @Override - public Object next() { - Object result = linkedList.get(pos); - pos = pos + 1; - return result; - } - } - - - //后面的方法先不用写的说 - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse() { - - //还可以用堆栈 先进后出 - - if(size() <= 1){ - return; - } - Object[] array = new Object[size]; - Node tmp = first; - for (int i = 0; i < size; i++) { - array[i] = tmp.data; - tmp = tmp.next; - } - this.first = null; - this.last = null; - for (int i = array.length - 1; i >= 0 ; i--) { - add(array[i]); - } - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf() { - - if (size <= 1){ - return; - } - int b = size/ 2; - Node n = getNode(b); - this.first = n; - size = (size % 2) + b; - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * - * @param i - * @param length - */ - public void remove(int i, int length) { - if (size == 0 || i < 0 || i >= size){ - return; - } - - length = size - i >= length ? length : size - i; - - - if(i + length == size){ - this.first = null; - this.last = null; - size = 0; - return; - } - - if(i == 0){ - Node n = getNode(length); - first = n; - size = size - length; - return; - } - - - Node a = getNode(i - 1); - Node b = getNode(i + length); - a.next = b; - size = size - length; - } - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * - * @param list - */ - public int[] getElements(LinkedList list) { - - if(size <= 0 || list.size() <= 0){ - return new int[0]; - } - - - - int[] result = new int[list.size()]; - - Node tmp = list.first; - int index = 0; - Node tmp2 = first; - for (int i = 0; i < list.size(); i++) { - int newIndex = (int)tmp.data; - int maxJ = newIndex - index; - for (int j = 0; j <= maxJ; j++) { - - if(j == maxJ){ - result[i] = (int)tmp2.data; - break; - } - tmp2 = tmp2.next; - } - index = newIndex; - tmp = tmp.next; - } - - size = size - list.size(); - - return result; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - * - * @param list - */ - - public void subtract(LinkedList list) { - for (int i = 0; i < list.size(); i++) { - this.remove(list.get(i)); - } - - } - - - public void remove(Object obj){ - if(size <= 0){ - return; - } - if(first.data.equals(obj)){ - first=first.next; - size = size - 1; - return; - } - Node tmp = first; - Node tmp2 = first.next; - for (int i = 1; i < size; i++) { - if(tmp2.data.equals(obj)){ - tmp.next = tmp2.next; - size = size - 1; - return; - } - tmp = tmp.next; - tmp2 = tmp2.next; - } - - } - - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues() { - if(size <= 1){ - return; - } - - Node tmp = first; - for (int i = 1; i < size; i++) { - if(tmp.next == null){ - break; - } - if (tmp.data.equals(tmp.next.data)){ - tmp.next = tmp.next.next; - } - tmp = tmp.next; - } - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * - * @param min - * @param max - */ - public void removeRange(int min, int max) { - if(size <= 0){ - return; - } - - Node tmp = first; - int a = -1; - int b = -1; - for (int i = 0; i < size; i++) { - if((int)tmp.data > min && a == -1){ - a = i; - } - - if((int)tmp.data >= max && b == -1){ - b = i; - } - - tmp = tmp.next; - } - - - if(min < max){ - remove(a, b - a); - return; - - } - - - if(min == max){ - - } - - - if(min > max){ - - } - - - - return; - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * - * @param list - */ - public LinkedList intersection(LinkedList list) { - LinkedList result = new LinkedList(); - - if(list == null || list.size <= 0 || size <= 0){ - return result; - } - - int i1 = 0; - int i2 = 0; - - while( i1 < this.size && i2 s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/datastructure/ArrayListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/datastructure/ArrayListTest.java deleted file mode 100644 index dcb879a948..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/datastructure/ArrayListTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package me.lzb.datastructure; - -import me.lzb.datastructure.ArrayList; -import me.lzb.datastructure.Iterator; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -/** - * ArrayList测试 - * Created by LZB on 2017/3/11. - */ - -public class ArrayListTest { - - private ArrayList arrayList; - - private String[] strArray; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Before - public void instantiate() throws Exception { - arrayList = new ArrayList(); - arrayList.add("a"); - arrayList.add("b"); - arrayList.add("c"); - arrayList.add("d"); - - strArray = new String[]{"a", "b", "c", "d"}; - } - - - @Test - public void sizeTest() { - Assert.assertEquals(4, arrayList.size(), 0); - } - - @Test - public void getTest() throws IndexOutOfBoundsException { - Assert.assertEquals("a", arrayList.get(0).toString()); - Assert.assertEquals("c", arrayList.get(2).toString()); - Assert.assertEquals("d", arrayList.get(3).toString()); - - thrown.expect(IndexOutOfBoundsException.class); - thrown.expectMessage("index boom"); - arrayList.get(100); - arrayList.get(-1); - } - - @Test - public void iteratoreTest(){ - Iterator iterator = arrayList.iterator(); - int a = 0; - while (iterator.hasNext()){ - Assert.assertEquals(strArray[a], iterator.next().toString()); - a = a + 1; - } - } - - - @Test - public void addTest() { - arrayList.add("f"); - Assert.assertEquals("f", arrayList.get(4).toString()); - } - - @Test - public void removeTest() throws IndexOutOfBoundsException { - String r1 = arrayList.remove(1).toString(); - Assert.assertEquals("b", r1); - Assert.assertEquals(3, arrayList.size()); - - String r0 = arrayList.remove(0).toString(); - Assert.assertEquals("a", r0); - Assert.assertEquals(2, arrayList.size()); - - String rs = arrayList.remove(arrayList.size() - 1).toString(); - Assert.assertEquals("d", rs); - Assert.assertEquals(1, arrayList.size()); - - thrown.expect(IndexOutOfBoundsException.class); - thrown.expectMessage("index boom"); - arrayList.remove(100); - - } - - - @Test - public void addIndexTest() throws IndexOutOfBoundsException { - arrayList.add(0, "0"); - Assert.assertEquals("0", arrayList.get(0).toString()); - arrayList.add(arrayList.size(), "s"); - Assert.assertEquals("s", arrayList.get(arrayList.size() - 1).toString()); - arrayList.add(2, "2a"); - Assert.assertEquals("2a", arrayList.get(2).toString()); - - thrown.expect(IndexOutOfBoundsException.class); - thrown.expectMessage("index boom"); - arrayList.add(10, "10a"); - } - - -} diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/datastructure/LinkedListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/datastructure/LinkedListTest.java deleted file mode 100644 index 1914195aa5..0000000000 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/datastructure/LinkedListTest.java +++ /dev/null @@ -1,260 +0,0 @@ -package me.lzb.datastructure; - -import me.lzb.datastructure.Iterator; -import me.lzb.datastructure.LinkedList; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - - -/** - * linkedliksTest - * Created by LZB on 2017/3/11. - */ -public class LinkedListTest { - - private LinkedList linkedList; - - private LinkedList intList; - - private String[] strArray; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Before - public void instantiate() throws Exception { - linkedList = new LinkedList(); - linkedList.add("a"); - linkedList.add("b"); - linkedList.add("c"); - linkedList.add("d"); - - strArray = new String[]{"a", "b", "c", "d"}; - - intList = new LinkedList(); - intList.add(0); - intList.add(1); - intList.add(2); - intList.add(3); - intList.add(4); - intList.add(5); - intList.add(6); - intList.add(7); - intList.add(8); - - } - - - @Test - public void iteratoreTest() { - Iterator iterator = linkedList.iterator(); - int a = 0; - while (iterator.hasNext()) { - Assert.assertEquals(strArray[a], iterator.next().toString()); - a = a + 1; - } - } - - - @Test - public void sizeTest() { - Assert.assertEquals(4, linkedList.size(), 0); - } - - @Test - public void getTest() throws IndexOutOfBoundsException { - Assert.assertEquals("a", linkedList.get(0).toString()); - Assert.assertEquals("b", linkedList.get(1).toString()); - Assert.assertEquals("d", linkedList.get(3).toString()); - - thrown.expect(IndexOutOfBoundsException.class); - thrown.expectMessage("index boom"); - linkedList.get(100); - linkedList.get(-1); - } - - - @Test - public void addTest() { - linkedList.add("f"); - Assert.assertEquals("f", linkedList.get(4).toString()); - } - - @Test - public void removeTest() throws IndexOutOfBoundsException { - String r1 = linkedList.remove(1).toString(); - Assert.assertEquals("b", r1); - Assert.assertEquals(3, linkedList.size()); - - String r0 = linkedList.remove(0).toString(); - Assert.assertEquals("a", r0); - Assert.assertEquals(2, linkedList.size()); - - String rs = linkedList.remove(linkedList.size() - 1).toString(); - Assert.assertEquals("d", rs); - Assert.assertEquals(1, linkedList.size()); - - thrown.expect(IndexOutOfBoundsException.class); - thrown.expectMessage("index boom"); - linkedList.remove(100); - linkedList.remove(-1); - } - - - @Test - public void addIndexTest() throws IndexOutOfBoundsException { - linkedList.add(0, "0"); - Assert.assertEquals("0", linkedList.get(0).toString()); - linkedList.add(linkedList.size(), "s"); - Assert.assertEquals("s", linkedList.get(linkedList.size() - 1).toString()); - linkedList.add(2, "2a"); - Assert.assertEquals("2a", linkedList.get(2).toString()); - thrown.expect(IndexOutOfBoundsException.class); - thrown.expectMessage("index boom"); - linkedList.add(100, "10a"); - - } - - - @Test - public void reverseTest() { - linkedList.reverse(); - Assert.assertEquals("[d,c,b,a]", linkedList.toString()); - } - - @Test - public void removeFirstHalfTest() { - intList.removeFirstHalf(); - Assert.assertEquals("[4,5,6,7,8]", intList.toString()); - Assert.assertEquals(5, intList.size()); - linkedList.removeFirstHalf(); - Assert.assertEquals("[c,d]", linkedList.toString()); - Assert.assertEquals(2, linkedList.size()); - } - - @Test - public void removeITest() { - intList.remove(0, 10); - Assert.assertEquals("[]", intList.toString()); - Assert.assertEquals(0, intList.size()); - - - linkedList.remove(1, 2); - Assert.assertEquals("[a,d]", linkedList.toString()); - Assert.assertEquals(2, linkedList.size()); - - - LinkedList l = new LinkedList(); - l.add("a"); - l.add("b"); - l.add("c"); - l.add("d"); - l.remove(0, 2); - Assert.assertEquals("[c,d]", l.toString()); - Assert.assertEquals(2, l.size()); - } - - - @Test - public void getElementsTest() { - int[] a = {1, 3, 4, 6}; - - LinkedList l = new LinkedList(); - l.add(1); - l.add(3); - l.add(4); - l.add(6); - int[] re = intList.getElements(l); - - Assert.assertEquals(a.length, re.length); - for (int i = 0; i < a.length; i++) { - Assert.assertEquals(a[i], re[i]); - } - - } - - - @Test - public void subtractTest() { - LinkedList l = new LinkedList(); - l.add(1); - l.add(3); - l.add(4); - l.add(6); - intList.subtract(l); - Assert.assertEquals(5, intList.size()); - Assert.assertEquals("[0,2,5,7,8]", intList.toString()); - } - - - @Test - public void removeDuplicateValuesTest() { - LinkedList list = new LinkedList(); - list.add(1); - list.add(1); - list.add(2); - list.add(2); - list.add(3); - list.add(5); - list.add(5); - list.add(6); - list.removeDuplicateValues(); - - Assert.assertEquals("[1,2,3,5,6]", list.toString()); - } - - - @Test - public void removeRangeTest() { - { - LinkedList linkedList = new LinkedList(); - - linkedList.add(11); - linkedList.add(12); - linkedList.add(13); - linkedList.add(14); - linkedList.add(16); - linkedList.add(16); - linkedList.add(19); - - linkedList.removeRange(10, 19); - Assert.assertEquals("[19]", linkedList.toString()); - } - - { - LinkedList linkedList = new LinkedList(); - - linkedList.add(11); - linkedList.add(12); - linkedList.add(13); - linkedList.add(14); - linkedList.add(16); - linkedList.add(16); - linkedList.add(19); - - linkedList.removeRange(10, 14); - Assert.assertEquals("[14,16,16,19]", linkedList.toString()); - } - } - - - @Test - public void intersectionTest() { - LinkedList list1 = new LinkedList(); - list1.add(1); - list1.add(6); - list1.add(7); - - LinkedList list2 = new LinkedList(); - list2.add(2); - list2.add(5); - list2.add(6); - - LinkedList newList = list1.intersection(list2); - Assert.assertEquals("[6]", newList.toString()); - } - -} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/AttributeInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..fd000bdcb3 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/AttributeInfo.java @@ -0,0 +1,22 @@ +package me.lzb.jvm.attr; + +/** + * Created by LZB on 2017/4/15. + */ +public abstract class AttributeInfo { + + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + + int attrNameIndex; + int attrLen; + + public AttributeInfo(int attrNameIndex, int attrLen) { + this.attrNameIndex = attrNameIndex; + this.attrLen = attrLen; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/CodeAttr.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..2addebeb39 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/CodeAttr.java @@ -0,0 +1,52 @@ +package me.lzb.jvm.attr; + +/** + * Created by LZB on 2017/4/15. + */ +public class CodeAttr extends AttributeInfo { + private int maxStack; + private int maxLocals; + private int codeLen; + private String code; + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + + public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen, String code /*ByteCodeCommand[] cmds*/) { + super(attrNameIndex, attrLen); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.codeLen = codeLen; + this.code = code; + //this.cmds = cmds; + } + + + public String getCode() { + return code; + } + + public LineNumberTable getLineNumTable() { + return lineNumTable; + } + + public void setLineNumTable(LineNumberTable lineNumTable) { + this.lineNumTable = lineNumTable; + } + + public LocalVariableTable getLocalVarTable() { + return localVarTable; + } + + public void setLocalVarTable(LocalVariableTable localVarTable) { + this.localVarTable = localVarTable; + } + + public StackMapTable getStackMapTable() { + return stackMapTable; + } + + public void setStackMapTable(StackMapTable stackMapTable) { + this.stackMapTable = stackMapTable; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LineNumberItem.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LineNumberItem.java new file mode 100644 index 0000000000..e621cf925a --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LineNumberItem.java @@ -0,0 +1,31 @@ +package me.lzb.jvm.attr; + +/** + * Created by LZB on 2017/4/16. + */ +public class LineNumberItem { + int startPC; + int lineNum; + + + public LineNumberItem(int startPC, int lineNum) { + this.startPC = startPC; + this.lineNum = lineNum; + } + + public int getStartPC() { + return startPC; + } + + // public void setStartPC(int startPC) { +// this.startPC = startPC; +// } + + public int getLineNum() { + return lineNum; + } + +// public void setLineNum(int lineNum) { +// this.lineNum = lineNum; +// } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LineNumberTable.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..5bf7b4759b --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LineNumberTable.java @@ -0,0 +1,23 @@ +package me.lzb.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by LZB on 2017/4/15. + */ +public class LineNumberTable extends AttributeInfo { + List items = new ArrayList<>(); + + + public void addLineNumberItem(LineNumberItem item) { + this.items.add(item); + } + + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + + } + + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LocalVariableItem.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..decef772a3 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LocalVariableItem.java @@ -0,0 +1,62 @@ +package me.lzb.jvm.attr; + +/** + * Created by LZB on 2017/4/15. + */ +public class LocalVariableItem { + private int startPC; + private int length; + private int nameIndex; + private int descIndex; + private int index; + + + public LocalVariableItem(int startPC, int length, int nameIndex, int descIndex, int index){ + this.startPC = startPC; + this.length = length; + this.nameIndex = nameIndex; + this.descIndex = descIndex; + this.index = index; + } + + + public int getStartPC() { + return startPC; + } + + public void setStartPC(int startPC) { + this.startPC = startPC; + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getDescIndex() { + return descIndex; + } + + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LocalVariableTable.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..9dca129d71 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/LocalVariableTable.java @@ -0,0 +1,20 @@ +package me.lzb.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by LZB on 2017/4/15. + */ +public class LocalVariableTable extends AttributeInfo { + List items = new ArrayList<>(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/StackMapTable.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..0b8947bdf8 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/attr/StackMapTable.java @@ -0,0 +1,15 @@ +package me.lzb.jvm.attr; + +/** + * Created by LZB on 2017/4/15. + */ +public class StackMapTable extends AttributeInfo { + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen, String code) { + super(attrNameIndex, attrLen); + this.originalCode = code; + } + + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/AccessFlag.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..224714a010 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/AccessFlag.java @@ -0,0 +1,29 @@ +package me.lzb.jvm.clz; + +/** + * Created by LZB on 2017/4/14. + */ +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/ClassFile.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..c5de9be5ce --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/ClassFile.java @@ -0,0 +1,131 @@ +package me.lzb.jvm.clz; + +import me.lzb.jvm.constant.ConstantPool; +import me.lzb.jvm.field.Field; +import me.lzb.jvm.method.Method; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by LZB on 2017/4/14. + */ +public class ClassFile { + + private String magicNumber; + + private int minorVersion; + + private int majorVersion; + + private AccessFlag accessFlag; + + private ClassIndex clzIndex; + + + private ConstantPool constantPool; + + private List fields = new ArrayList<>(); + + private List methods = new ArrayList<>(); + + + public String getMagicNumber() { + return magicNumber; + } + + public void setMagicNumber(String magicNumber) { + this.magicNumber = magicNumber; + } + + public int getMinorVersion() { + return minorVersion; + } + + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + + public int getMajorVersion() { + return majorVersion; + } + + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + + + public AccessFlag getAccessFlag() { + return accessFlag; + } + + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + public ConstantPool getConstantPool() { + return constantPool; + } + + public void setConstantPool(ConstantPool constantPool) { + this.constantPool = constantPool; + } + + + + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + } + + public List getMethods() { + return methods; + } + + public void setMethods(List methods) { + this.methods = methods; + } + + + + + + public ClassIndex getClzIndex() { + return clzIndex; + } + + public void setClzIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } +// System.out.println("Class Name:"+ getClassName()); +// +// System.out.println("Super Class Name:"+ getSuperClassName()); + + } + + + + + public int getConstantPoolCount() { + return constantPool.getSize(); + } + + public void addField(Field f){ + this.fields.add(f); + } + + public void addMethod(Method m){ + this.methods.add(m); + } + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/ClassIndex.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..8916290057 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/clz/ClassIndex.java @@ -0,0 +1,25 @@ +package me.lzb.jvm.clz; + +/** + * Created by LZB on 2017/4/14. + */ +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + + public int getSuperClassIndex() { + return superClassIndex; + } + + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ClassInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..200e59834a --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ClassInfo.java @@ -0,0 +1,33 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/14. + */ +public class ClassInfo extends ConstantInfo{ + private int type = ConstantInfo.Class_info; + private int utf8Index; + + public ClassInfo(ConstantPool pool) { + super(pool); + } + + @Override + public int getType() { + return type; + } + + public int getUtf8Index() { + return utf8Index; + } + + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ConstantInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..3bbabf2180 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ConstantInfo.java @@ -0,0 +1,44 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/14. + */ +public abstract class ConstantInfo { + + public static final String MAGIC_NUMBER = "cafebabe"; + + public static final int Class_info = 7; + + public static final int Fieldref_info = 9; + + public static final int Methodref_info = 10; + + public static final int String_info = 8; + + public static final int NameAndType_info = 12; + + public static final int Utf8_info = 1; + + + + protected ConstantPool constantPool; + + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + + + public abstract int getType(); +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ConstantPool.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..d78f0a71f4 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/ConstantPool.java @@ -0,0 +1,32 @@ +package me.lzb.jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by LZB on 2017/4/14. + */ +public class ConstantPool { + + private List constantInfoList = new ArrayList<>(); + + + + public void addConstantInfo(ConstantInfo constantInfo){ + constantInfoList.add(constantInfo); + } + + + public int getSize(){ + return constantInfoList.size() > 1 ? constantInfoList.size() - 1 : 0; + } + + public ConstantInfo getConstantInfo(int index){ + return constantInfoList.get(index); + } + + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfoList.get(index)).getValue(); + } + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/FieldRefInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..0ebab33b39 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/FieldRefInfo.java @@ -0,0 +1,35 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/15. + */ +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.Fieldref_info; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + @Override + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/MethodRefInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..263c185af5 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/MethodRefInfo.java @@ -0,0 +1,36 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/15. + */ +public class MethodRefInfo extends ConstantInfo { + private int type = ConstantInfo.Methodref_info; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + @Override + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/NameAndTypeInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..bdf158ec37 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,36 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/15. + */ +public class NameAndTypeInfo extends ConstantInfo { + private int type = ConstantInfo.NameAndType_info; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + @Override + public int getType() { + return type; + } + + public int getIndex1() { + return index1; + } + + public void setIndex1(int index1) { + this.index1 = index1; + } + + public int getIndex2() { + return index2; + } + + public void setIndex2(int index2) { + this.index2 = index2; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/NullConstantInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..7f9debba3b --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/NullConstantInfo.java @@ -0,0 +1,12 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/14. + */ +public class NullConstantInfo extends ConstantInfo{ + + @Override + public int getType() { + return -1; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/StringInfo.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..8ba36d8cfc --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/15. + */ +public class StringInfo extends ConstantInfo { + private int type = ConstantInfo.String_info; + + private int index; + + public StringInfo(ConstantPool pool) { + super(pool); + } + @Override + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/UTF8Info.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..d3f35c2fd7 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/constant/UTF8Info.java @@ -0,0 +1,38 @@ +package me.lzb.jvm.constant; + +/** + * Created by LZB on 2017/4/15. + */ +public class UTF8Info extends ConstantInfo { + private int type = ConstantInfo.Class_info; + + private String value; + + private int length; + + public UTF8Info(ConstantPool pool) { + super(pool); + } + + + @Override + public int getType() { + return type; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/field/Field.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/field/Field.java new file mode 100644 index 0000000000..f388fd01bf --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/field/Field.java @@ -0,0 +1,30 @@ +package me.lzb.jvm.field; + +import me.lzb.jvm.constant.ConstantPool; + +/** + * Created by LZB on 2017/4/15. + */ +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private ConstantPool pool; + + public Field(int accessFlag, int nameIndex, int descriptorIndex ,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + @Override + public String toString() { + String key = pool.getUTF8String(nameIndex); + String value = pool.getUTF8String(descriptorIndex); + return key + ":" + value; + } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/loader/ClassFileLoader.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..593694066d --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/loader/ClassFileLoader.java @@ -0,0 +1,56 @@ +package me.lzb.jvm.loader; + + +import me.lzb.common.utils.FileUtils; +import me.lzb.jvm.clz.ClassFile; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList<>(); + + public byte[] readBinaryCode(String className) throws IOException { +// String fileName = className.replaceAll(".*\\.", "") + ".class"; +// String pkg = className.replaceAll("\\.[^\\.]+$", ""); +// String packagePath = pkg.replaceAll("\\.", "\\\\"); + + className = className.replace('.', File.separatorChar) + ".class"; + for (String s : clzPaths) { + byte[] data = FileUtils.readByteCodes(s + className); + if (data != null) { + return data; + } + } + + throw new IOException(className + "is not exist"); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + public String getClassPath() { + StringBuilder buffer = new StringBuilder(); + for (Iterator iterator = clzPaths.iterator(); iterator.hasNext(); ) { + buffer.append(iterator.next() + (iterator.hasNext() ? ";" : "")); + } + return buffer.toString(); + } + + + public ClassFile loadClass(String className) throws IOException { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(codes); + return parser.parse(); + } + + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/loader/ClassFileParser.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..bfd3cbc2c0 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/loader/ClassFileParser.java @@ -0,0 +1,331 @@ +package me.lzb.jvm.loader; + +import me.lzb.common.utils.ByteUtils; +import me.lzb.common.utils.StringUtils; +import me.lzb.jvm.attr.*; +import me.lzb.jvm.clz.AccessFlag; +import me.lzb.jvm.clz.ClassFile; +import me.lzb.jvm.clz.ClassIndex; +import me.lzb.jvm.constant.*; +import me.lzb.jvm.field.Field; +import me.lzb.jvm.method.Method; + +import java.io.UnsupportedEncodingException; + +/** + * 处理字class文件字节流 + * Created by LZB on 2017/4/14. + */ +public class ClassFileParser { + + + private final byte[] data; + + private int index; + + public ClassFileParser(byte[] data) { + this.data = data; + } + + + public ClassFile parse() { + ClassFile classFile = new ClassFile(); + parserMagicNumber(classFile); + + if (!StringUtils.equals(classFile.getMagicNumber(), ConstantInfo.MAGIC_NUMBER)) { + throw new RuntimeException("It is not a java class file."); + } + + parserVersion(classFile); + + parserConstantPool(classFile); + + parserAccessFlag(classFile); + + parserClassIndex(classFile); + + parserInterface(classFile); + + parserField(classFile); + + parserMethod(classFile); + + return classFile; + } + + + private byte[] nextBytes(int nextLength) { + byte[] target = new byte[nextLength]; + System.arraycopy(data, index, target, 0, nextLength); + index = index + nextLength; + return target; + } + + private int nextBytesToInt(int nextLength) { + return ByteUtils.byteToInt(nextBytes(nextLength)); + } + + private String nextBytesToString(int nextLength) { + return ByteUtils.byteToHexString(nextBytes(nextLength)); + } + + + private void parserMagicNumber(ClassFile classFile) { + this.index = 0; + classFile.setMagicNumber(nextBytesToString(4)); + } + + private void parserVersion(ClassFile classFile) { + classFile.setMinorVersion(nextBytesToInt(2)); + classFile.setMajorVersion(nextBytesToInt(2)); + } + + private void parserConstantPool(ClassFile classFile) { + int count = nextBytesToInt(2); + + ConstantPool pool = new ConstantPool(); + pool.addConstantInfo(new NullConstantInfo()); + + for (int i = 1; i < count; i++) { + + int tag = nextBytesToInt(1); + + if (tag == ConstantInfo.Class_info) { + ClassInfo classInfo = new ClassInfo(pool); + classInfo.setUtf8Index(nextBytesToInt(2)); + + pool.addConstantInfo(classInfo); + continue; + } + + if (tag == ConstantInfo.Fieldref_info) { + FieldRefInfo fieldRefInfo = new FieldRefInfo(pool); + fieldRefInfo.setClassInfoIndex(nextBytesToInt(2)); + fieldRefInfo.setNameAndTypeIndex(nextBytesToInt(2)); + + pool.addConstantInfo(fieldRefInfo); + continue; + } + + if (tag == ConstantInfo.Methodref_info) { + MethodRefInfo methodRefInfo = new MethodRefInfo(pool); + methodRefInfo.setClassInfoIndex(nextBytesToInt(2)); + methodRefInfo.setNameAndTypeIndex(nextBytesToInt(2)); + + pool.addConstantInfo(methodRefInfo); + continue; + } + + if (tag == ConstantInfo.NameAndType_info) { + NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(pool); + nameAndTypeInfo.setIndex1(nextBytesToInt(2)); + nameAndTypeInfo.setIndex2(nextBytesToInt(2)); + + pool.addConstantInfo(nameAndTypeInfo); + continue; + } + + if (tag == ConstantInfo.String_info) { + StringInfo stringInfo = new StringInfo(pool); + stringInfo.setIndex(nextBytesToInt(2)); + + pool.addConstantInfo(stringInfo); + continue; + } + + if (tag == ConstantInfo.Utf8_info) { + UTF8Info utf8Info = new UTF8Info(pool); + int len = nextBytesToInt(2); + byte[] data = nextBytes(len); + String value = null; + try { + value = new String(data, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + utf8Info.setLength(len); + utf8Info.setValue(value); + + pool.addConstantInfo(utf8Info); + continue; + } + + + throw new RuntimeException("the constant pool tag " + tag + " has not been implemented yet." + i); + } + + + classFile.setConstantPool(pool); + } + + + private void parserAccessFlag(ClassFile classFile) { + AccessFlag flag = new AccessFlag(nextBytesToInt(2)); + classFile.setAccessFlag(flag); + } + + + private void parserClassIndex(ClassFile classFile) { + ClassIndex clzIndex = new ClassIndex(); + + int thisClassIndex = nextBytesToInt(2); + int superClassIndex = nextBytesToInt(2); + clzIndex.setThisClassIndex(thisClassIndex); + clzIndex.setSuperClassIndex(superClassIndex); + + classFile.setClzIndex(clzIndex); + } + + private void parserInterface(ClassFile classFile) { + int count = nextBytesToInt(2); + //TODO 实现interface + } + + + private void parserField(ClassFile classFile) { + int count = nextBytesToInt(2); + for (int i = 1; i <= count; i++) { + int accessFlags = nextBytesToInt(2); + int nameIndex = nextBytesToInt(2); + int descriptorIndex = nextBytesToInt(2); + int attributesCount = nextBytesToInt(2); + + if (attributesCount > 0) { + throw new RuntimeException("Field Attribute has not been implement"); + } + + Field field = new Field(accessFlags, nameIndex, descriptorIndex, classFile.getConstantPool()); + classFile.addField(field); + } + } + + private void parserMethod(ClassFile classFile) { + int count = nextBytesToInt(2); + for (int i = 1; i <= count; i++) { + int accessFlags = nextBytesToInt(2); + int nameIndex = nextBytesToInt(2); + int descriptorIndex = nextBytesToInt(2); + int attributesCount = nextBytesToInt(2); + + + Method method = new Method(accessFlags, nameIndex, descriptorIndex); + + + for (int j = 1; j <= attributesCount; j++) { + + int attributeNameIndex = nextBytesToInt(2); + + String attributeName = classFile.getConstantPool().getUTF8String(attributeNameIndex); + + if (StringUtils.equalsIgnoreCase(attributeName, AttributeInfo.CODE)) { + parserCodeAttr(attributeNameIndex, method, classFile); + continue; + } + + + throw new RuntimeException("only CODE attribute is implemented."); + } + + classFile.addMethod(method); + + } + } + + + private void parserCodeAttr(int attributeNameIndex, Method method, ClassFile classFile) { + int attributeLength = nextBytesToInt(4); + int maxStack = nextBytesToInt(2); + int maxLocals = nextBytesToInt(2); + int codeLength = nextBytesToInt(4); + + String code = nextBytesToString(codeLength); + CodeAttr codeAttr = new CodeAttr(attributeNameIndex, attributeLength, maxStack, maxLocals, codeLength, code); + + int exceptionTableLength = nextBytesToInt(2); + if (exceptionTableLength > 0) { + String exceptionTable = nextBytesToString(exceptionTableLength); + //TODO 异常 + } + + + int subAttrCount = nextBytesToInt(2); + for (int k = 1; k <= subAttrCount; k++) { + int subAttrIndex = nextBytesToInt(2); + + String subAttrName = classFile.getConstantPool().getUTF8String(subAttrIndex); + + if (StringUtils.equalsIgnoreCase(subAttrName, AttributeInfo.LINE_NUM_TABLE)) { + + parserLineNumberTable(codeAttr, subAttrIndex); + continue; + } + + if (StringUtils.equalsIgnoreCase(subAttrName, AttributeInfo.LOCAL_VAR_TABLE)) { + parserLocalVariableTable(codeAttr, subAttrIndex); + continue; + } + + if (StringUtils.equalsIgnoreCase(subAttrName, AttributeInfo.STACK_MAP_TABLE)) { + parserStackMapTable(codeAttr, subAttrIndex); + continue; + } + + + throw new RuntimeException("Need code to process" + subAttrName); + } + + + method.setCodeAttr(codeAttr); + } + + + private void parserLineNumberTable(CodeAttr codeAttr, int attributeNameIndex) { +// int attributeNameIndex = nextBytesToInt(2); + int attributeLength = nextBytesToInt(4); + + int lineNumberTableLength = nextBytesToInt(2); + + LineNumberTable table = new LineNumberTable(attributeNameIndex, attributeLength); + + for (int l = 1; l <= lineNumberTableLength; l++) { + int startPc = nextBytesToInt(2); + int lineNumber = nextBytesToInt(2); + LineNumberItem item = new LineNumberItem(startPc, lineNumber); + table.addLineNumberItem(item); + } + + codeAttr.setLineNumTable(table); + } + + + private void parserLocalVariableTable(CodeAttr codeAttr, int attributeNameIndex) { +// int attributeNameIndex = nextBytesToInt(2); + int attributeLength = nextBytesToInt(4); + + int localVariableTableLength = nextBytesToInt(2); + + LocalVariableTable table = new LocalVariableTable(attributeNameIndex, attributeLength); + + for (int l = 1; l <= localVariableTableLength; l++) { + int startPc = nextBytesToInt(2); + int lineNumber = nextBytesToInt(2); + int nameIndex = nextBytesToInt(2); + int descriptorIndex = nextBytesToInt(2); + int index = nextBytesToInt(2); + LocalVariableItem item = new LocalVariableItem(startPc, lineNumber, nameIndex, descriptorIndex, index); + table.addLocalVariableItem(item); + } + + codeAttr.setLocalVarTable(table); + } + + private void parserStackMapTable(CodeAttr codeAttr, int attributeNameIndex) { +// int attributeNameIndex = nextBytesToInt(2); + int attributeLength = nextBytesToInt(4); + String code = nextBytesToString(attributeLength); + StackMapTable table = new StackMapTable(attributeNameIndex, attributeLength, code); + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + codeAttr.setStackMapTable(table); + } + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/method/Method.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/method/Method.java new file mode 100644 index 0000000000..9c3e9ad984 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/jvm/method/Method.java @@ -0,0 +1,65 @@ +package me.lzb.jvm.method; + +import me.lzb.jvm.attr.CodeAttr; + +/** + * Created by LZB on 2017/4/15. + */ +public class Method { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + +// private ClassFile clzFile; + + + public Method(/*ClassFile clzFile,*/ int accessFlag, int nameIndex, int descriptorIndex) { +// this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + + public int getAccessFlag() { + return accessFlag; + } + + public void setAccessFlag(int accessFlag) { + this.accessFlag = accessFlag; + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getDescriptorIndex() { + return descriptorIndex; + } + + public void setDescriptorIndex(int descriptorIndex) { + this.descriptorIndex = descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr codeAttr) { + this.codeAttr = codeAttr; + } + +// public ClassFile getClzFile() { +// return clzFile; +// } +// +// public void setClzFile(ClassFile clzFile) { +// this.clzFile = clzFile; +// } +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/loader/ClassFileLoader.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/loader/ClassFileLoader.java deleted file mode 100644 index 86f017cebf..0000000000 --- a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/loader/ClassFileLoader.java +++ /dev/null @@ -1,57 +0,0 @@ -package me.lzb.loader; - -import me.lzb.utils.FileUtils; -import me.lzb.utils.StringUtils; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - - - -public class ClassFileLoader { - - private List clzPaths = new ArrayList<>(); - - public byte[] readBinaryCode(String className) throws IOException { - String fileName = className.replaceAll(".*\\.", "") + ".class"; - String pkg = className.replaceAll("\\.[^\\.]+$", ""); - String packagePath = pkg.replaceAll("\\.", "\\\\"); - - - String path = ""; - for (String s : clzPaths) { - if (FileUtils.isFileExist(s + packagePath, fileName)){ - path = s; - break; - } - } - - if(StringUtils.isBlank(path)){ - throw new IOException("class file not found"); - } - - return FileUtils.readByteCodes(path + packagePath + "\\" + fileName); - - } - - - public void addClassPath(String path) { - clzPaths.add(path); - } - - - - public String getClassPath(){ - StringBuilder buffer = new StringBuilder(); - for (Iterator iterator = clzPaths.iterator(); iterator.hasNext();) { - buffer.append(iterator.next() + (iterator.hasNext() ? ";" : "")); - } - return buffer.toString(); - } - - - - -} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/FileUtils.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/FileUtils.java deleted file mode 100644 index b0947d4326..0000000000 --- a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/FileUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -package me.lzb.utils; - -import java.io.*; - -/** - * Created by LZB on 2017/4/4. - */ -public class FileUtils { - - /** - * 判断文件是否存在 - * - * @param path 路径 - * @param name 文件名 - * @return true false - */ - public static boolean isFileExist(String path, String name) { - File file = new File(path + "\\" + name); - return file.exists(); - } - - /** - * 读取文件为二进制数组 - * @param clzFileName 文件路径 - * @return 数组 - * @throws IOException - */ - public static byte[] readByteCodes(String clzFileName) throws IOException { - - File f = new File(clzFileName); - - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - - - byte[] buffer = new byte[1024]; - int length = -1; - try { - while ((length = bis.read(buffer)) != -1) { - bos.write(buffer, 0, length); - } - } catch (IOException e) { - e.printStackTrace(); - throw e; - } finally { - if (bis != null) { - bis.close(); - } - if (bos != null) { - bos.close(); - } - } - - byte[] codes = bos.toByteArray(); - - bis.close(); - - return codes; - } - - -} diff --git a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/StringUtils.java b/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/StringUtils.java deleted file mode 100644 index b4ec543d49..0000000000 --- a/group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/StringUtils.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.lzb.utils; - -/** - * Created by LZB on 2017/4/4. - */ -public class StringUtils extends org.apache.commons.lang3.StringUtils { -} diff --git a/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/jvm/ClassFileloaderTest.java b/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/jvm/ClassFileloaderTest.java new file mode 100644 index 0000000000..f41b313c97 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/jvm/ClassFileloaderTest.java @@ -0,0 +1,269 @@ +package me.lzb.jvm; + +import me.lzb.common.utils.ByteUtils; +import me.lzb.jvm.clz.ClassFile; +import me.lzb.jvm.clz.ClassIndex; +import me.lzb.jvm.constant.*; +import me.lzb.jvm.field.Field; +import me.lzb.jvm.loader.ClassFileLoader; +import me.lzb.jvm.method.Method; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; + + +public class ClassFileloaderTest { + + + static String path1 = EmployeeV1.class.getResource("/").getPath(); +// static String path1 = "D:\\code\\learning\\coding2017\\group24\\1148285693\\learning2017\\mini-jvm\\target\\test-classes\\"; + static String path2 = "C:\\temp"; + + static String className = "me.lzb.jvm.EmployeeV1"; + + private static final String FULL_QUALIFIED_CLASS_NAME = "me/lzb/jvm/EmployeeV1"; +// private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void testPath() { + + String s = EmployeeV1.class.getResource("/").getPath(); + String s2 = EmployeeV1.class.getResource("").getPath(); + System.out.println(s); + System.out.println(s2); + + } + + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() throws Exception { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + + byte[] byteCodes = loader.readBinaryCode(className); + + Assert.assertEquals(1030, byteCodes.length); + + } + + + @Test + public void testMagicNumber() throws Exception { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3]}; + + String acctualValue = ByteUtils.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + static ClassFile clzFile = null; + + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + try { + clzFile = loader.loadClass(className); + } catch (IOException e) { + e.printStackTrace(); + } + clzFile.print(); + } + + + @Test + public void testVersion() { + + Assert.assertEquals(0, clzFile.getMinorVersion()); + Assert.assertEquals(52, clzFile.getMajorVersion()); + + } + + @Test + public void testConstantPool() { + + + ConstantPool pool = clzFile.getConstantPool(); + + Assert.assertEquals(53, pool.getSize()); + + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(7); + Assert.assertEquals(44, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(44); + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); + } + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(11); + Assert.assertEquals(48, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(48); + Assert.assertEquals("java/lang/Object", utf8Info.getValue()); + } + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(12); + Assert.assertEquals("name", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(13); + Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(14); + Assert.assertEquals("age", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(15); + Assert.assertEquals("I", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(16); + Assert.assertEquals("", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(17); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(18); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo) pool.getConstantInfo(1); + Assert.assertEquals(11, methodRef.getClassInfoIndex()); + Assert.assertEquals(36, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(36); + Assert.assertEquals(16, nameAndType.getIndex1()); + Assert.assertEquals(28, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo) pool.getConstantInfo(10); + Assert.assertEquals(7, methodRef.getClassInfoIndex()); + Assert.assertEquals(47, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(35); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + + @Test + public void testClassIndex() { + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo) clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo) clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + /** + * 下面是第三次JVM课应实现的测试用例 + */ + @Test + public void testReadFields() { + + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + + @Test + public void testMethods() { + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool, m, + "", + "(Ljava/lang/String;I)V", + "2ab700012a2bb500022a1cb50003b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool, m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb50002b1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool, m, + "setAge", + "(I)V", + "2a1bb50003b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool, m, + "sayHello", + "()V", + "b200041205b60006b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool, m, + "main", + "([Ljava/lang/String;)V", + "bb0007591208101db700094c2bb6000ab1"); + } + } + + private void assertMethodEquals(ConstantPool pool, Method m, String expectedName, String expectedDesc, String expectedCode) { + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } + +} diff --git a/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/jvm/EmployeeV1.java b/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/jvm/EmployeeV1.java new file mode 100644 index 0000000000..3fa9b0fc85 --- /dev/null +++ b/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/jvm/EmployeeV1.java @@ -0,0 +1,27 @@ +package me.lzb.jvm; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/loader/ClassFileloaderTest.java b/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/loader/ClassFileloaderTest.java deleted file mode 100644 index b544e7ddc4..0000000000 --- a/group24/1148285693/learning2017/mini-jvm/src/test/java/me/lzb/loader/ClassFileloaderTest.java +++ /dev/null @@ -1,103 +0,0 @@ -package me.lzb.loader; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - - - -public class ClassFileloaderTest { - - - static String path1 = EmployeeV1.class.getResource("/").getPath(); - static String path2 = "C:\\temp"; - - - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - - - @Test - public void testPath(){ - - String s = EmployeeV1.class.getResource("/").getPath(); - String s2 = EmployeeV1.class.getResource("").getPath(); - System.out.println(s); - System.out.println(s2); - - } - - - - @Test - public void testClassPath(){ - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - loader.addClassPath(path2); - - String clzPath = loader.getClassPath(); - - Assert.assertEquals(path1+";"+path2,clzPath); - - } - - @Test - public void testClassFileLength() throws Exception{ - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - - String className = "me.lzb.loader.EmployeeV1"; - - byte[] byteCodes = loader.readBinaryCode(className); - - // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 - Assert.assertEquals(1036, byteCodes.length); - - } - - - @Test - public void testMagicNumber() throws Exception{ - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "me.lzb.loader.EmployeeV1"; - byte[] byteCodes = loader.readBinaryCode(className); - byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - - String acctualValue = this.byteToHexString(codes); - - Assert.assertEquals("cafebabe", acctualValue); - } - - - - - - - private String byteToHexString(byte[] codes ){ - StringBuffer buffer = new StringBuffer(); - for(int i=0;i[] edgeList; + + /** + * 边 + */ + private class Edge { + /** + * 边的id + */ + int id; + + /** + * 是否被正向搜索 + */ + boolean isSearched; + + /** + * 顶点v + */ + int v; + + /** + * 顶点b + */ + int w; + + /** + * 保存回滚操作中,被回滚的的路径方向,以及,前提路径 + * 因为在不同级别的回滚中,可能会有多条临时路径,所以用list存放 + * 顶点->顶点:路径id->路径id->路径id + * 1->2:0->1->2 + */ + ArrayList to = new ArrayList<>(); + + /** + * 构造函数 + * @param v 顶点v + * @param w 顶点w + */ + public Edge(int v, int w) { + this.v = v; + this.w = w; + isSearched = false; + id = edgeCount; + } + + + /** + * 在当前前提路径下,是否有 + * @param v0 出发顶点 + * @param P 前提路径 + * @return true false + */ + public boolean isFrom(int v0, String P) { + return isTheSameTo(v0, getAnotherV(v0), P); + } + + /** + * 临时路径是否相同 + * @param v0 出发顶点 + * @param v1 到达顶点 + * @param p 前提路径 + * @return true false + */ + public boolean isTheSameTo(int v0, int v1, String p) { + if (to.size() == 0) { + return false; + } + String ss = v0 + PATH_SEPARATOR + v1 + ":" + p; + for (String s : to) { + if (ss.equals(s)) { + return true; + } + } + return false; + } + + /** + * 删除临时路径 + * @param v0 出发顶点 + * @param v1 到达顶点 + * @param p 前提路径 + */ + public void removeTo(int v0, int v1, String p) { + if (to.size() == 0) { + return; + } + String ss = v0 + PATH_SEPARATOR + v1 + ":" + p; + for (Iterator iterator = to.iterator(); iterator.hasNext(); ) { + String s = iterator.next(); + if (ss.equals(s)) { + iterator.remove(); + return; + } + } + } + + /** + * 增加临时路径 + * @param v0 出发顶点 + * @param v1 到达顶点 + * @param p 前提路径 + */ + public void addTo(int v0, int v1, String p) { + String ss = v0 + PATH_SEPARATOR + v1 + ":" + p; + for (String s : to) { + if (ss.equals(s)) { + return; + } + } + to.add(ss); + + } + + /** + * 获取边的另外一条顶点 + * @param vertex + * @return + */ + public int getAnotherV(int vertex) { + if (vertex == v) { + return w; + } else { + return v; + } + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Edge c = (Edge) obj; + return this.id == c.id; + } + + @Override + public int hashCode() { + return id; + } + } + + /** + * 构造函数 + * @param vertexNum 顶点总数 + * @param edgeCount 边的总数 + */ + public Graph(int vertexNum, int edgeCount) { + this.vertexCount = vertexNum; + this.edgeCount = 0; + edgeList = new LinkedList[edgeCount]; + for (int i = 0; i < edgeCount; i++) { + edgeList[i] = new LinkedList<>(); + } + } + + public void addEdge(int v1, int v2) { + Edge c = new Edge(v2, v1); + edgeList[v1].add(c); + edgeList[v2].add(c); + edgeCount++; + } + + + public void addEdge(int[][] edgeArray) { + for (int i = 0; i < edgeArray.length; i++) { + addEdge(edgeArray[i][0], edgeArray[i][1]); + } + } + + public String toString() { + StringBuilder s = new StringBuilder(); + s.append(vertexCount + " vertices, " + edgeCount + " edges " + NEWLINE); + for (int v = 0; v < vertexCount; v++) { + s.append(v + ": "); + for (Edge w : edgeList[v]) { + s.append(w.getAnotherV(v) + " "); + } + s.append(NEWLINE); + } + return s.toString(); + } + + + /** + * 更新出现过路径的最长边数 + * @param a + */ + private void updateMax(int a) { + if (a > maxEdge) { + maxEdge = a; + } + } + + + public boolean isEuler() { + int start = 0; + Stack stack = new Stack<>(); + stack.push(start); + + //TODO 退出递归的条件 +// try { + search(start, start, stack, new Stack<>()); +// }catch (EmptyStackException e){ + +// } + + System.out.println("最长边数:" + maxEdge); + return maxEdge == edgeCount; + } + + + + + /** + * 正向搜索 + * 传进去一个节点,顺着一条没有搜索过的边找到下一个节点。当搜索到死路时,回滚 + * @param v 当前提点 + * @param stack 当前路径的节点顺序 + * @param sp 当前路径的路径顺序 + */ + public void search(int start, int v, Stack stack, Stack sp) { + + LinkedList list = edgeList[v]; + + boolean anotherWay = false; + for (Edge w : list) { + if (!w.isSearched && !w.isTheSameTo(v, w.getAnotherV(v), getPath(sp))) { + anotherWay = true; + w.isSearched = true; + stack.push(w.getAnotherV(v)); + updateMax(sp.size()); + sp.push(w); + search(start, w.getAnotherV(v), stack, sp); + } + } + + if (!anotherWay) { + System.out.println("最长:==============================="); + rollback(start, stack, sp); + } + + } + + + + /** + * 回滚,回滚当上一个节点,如果当前节点有可以使用的边,调用搜索,如果没有,递归继续回滚 + * 如果需要递归回滚,回滚到第二级之前,清空所有,当前路径下,从该点出发的方向 + * 被回滚的路径,需要保存路径方向,以及提前路径 + * @param stack 当前路径的节点顺序 + * @param sp 当前路径的路径顺序 + */ + public void rollback(int start, Stack stack, Stack sp) { + + String ss = getPath(sp); + String output = "顶点:" + stack.toString() + + NEWLINE + "路径:" + ss + + NEWLINE; + System.out.println(output); + +// if(stack.size() == 1){ +// return; +// } + + + Edge e = sp.pop(); //需要回滚的路径 + String pp = getPath(sp); //前提路径 + + int vz = stack.pop(); + int vy = stack.peek(); + + boolean rollbakc2 = true; + + LinkedList l = edgeList[vy]; + + //判断当前节点是否存在空闲路径,是否要回滚两级 + //空闲路径:没有被正向搜索,也没有被缓存当前前提路径下,从改节点出发的方向 + for (Edge w : l) { + if (!w.isSearched && !w.isTheSameTo(vy, w.getAnotherV(vy), pp)) { + rollbakc2 = false; + break; + } + } + + + //回滚当前路径,回滚一级 + int r = vy; + for (Edge w : l) { + if (w.equals(e)) { + w.addTo(vy, vz, pp); + w.isSearched = false; + break; + } + } + + if (rollbakc2) { + //回滚两级, 清空所有,当前路径下,从该点出发的方向 + + for (Edge w : l) { + if (!w.isSearched && w.isFrom(vy, pp)) { + w.removeTo(vy, w.getAnotherV(vy), pp); + } + } + rollback(start, stack, sp); + } + + search(start, r, stack, sp); + + } + + public String getPath(Stack stack) { + String s = ""; + for (Edge x : stack) { + s = s + x.id + PATH_SEPARATOR; + } + s = s.replaceAll(PATH_SEPARATOR + "$", ""); + return s; + } + + + public static void main(String[] args) { + int[][] aa = new int[][]{{0, 1}, {0, 1}, {0, 3}, {1, 3}, {1, 2}, {1, 2}, {2, 3}}; + Graph g = new Graph(4, aa.length); + g.addEdge(aa); + System.out.println(g.toString()); + System.out.println(g.isEuler()); + } +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java new file mode 100644 index 0000000000..4b8b3d4ce8 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java @@ -0,0 +1,27 @@ +package me.lzb.other.lock; + +/** + * Created by LZB on 2017/3/30. + */ +public class ReentrantTest1 implements Runnable{ + + public synchronized void get(){ + System.out.println(Thread.currentThread().getId()); + set(); + } + + public synchronized void set(){ + System.out.println(Thread.currentThread().getId()); + } + + @Override + public void run() { + get(); + } + public static void main(String[] args) { + ReentrantTest1 ss=new ReentrantTest1(); + new Thread(ss).start(); + new Thread(ss).start(); + new Thread(ss).start(); + } +} \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java new file mode 100644 index 0000000000..c630ea9e33 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java @@ -0,0 +1,35 @@ +package me.lzb.other.lock; + +import java.util.concurrent.locks.ReentrantLock; + +/** + * Created by LZB on 2017/3/30. + */ +public class ReentrantTest2 implements Runnable { + ReentrantLock lock = new ReentrantLock(); + + public void get() { + lock.lock(); + System.out.println(Thread.currentThread().getId()); + set(); + lock.unlock(); + } + + public void set() { + lock.lock(); + System.out.println(Thread.currentThread().getId()); + lock.unlock(); + } + + @Override + public void run() { + get(); + } + + public static void main(String[] args) { + ReentrantTest2 ss = new ReentrantTest2(); + new Thread(ss).start(); + new Thread(ss).start(); + new Thread(ss).start(); + } +} \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java new file mode 100644 index 0000000000..d89298c786 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java @@ -0,0 +1,52 @@ +package me.lzb.other.proxy; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * Created by LZB on 2017/3/29. + */ +public class MyInvocationHandler implements InvocationHandler { + + // 目标对象 + private Object target; + + /** + * 构造方法 + * + * @param target 目标对象 + */ + public MyInvocationHandler(Object target) { + super(); + this.target = target; + } + + + /** + * 执行目标对象的方法 + */ + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + + // 在目标对象的方法执行之前简单的打印一下 + System.out.println("------------------before------------------"); + + // 执行目标对象的方法 + Object result = method.invoke(target, args); + + // 在目标对象的方法执行之后简单的打印一下 + System.out.println("-------------------after------------------"); + + return result; + } + + /** + * 获取目标对象的代理对象 + * + * @return 代理对象 + */ + public Object getProxy() { + return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), + target.getClass().getInterfaces(), this); + } +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java new file mode 100644 index 0000000000..d57431acab --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java @@ -0,0 +1,11 @@ +package me.lzb.other.proxy; + +/** + * Created by LZB on 2017/3/29. + */ +public interface UserService { + /** + * 目标方法 + */ + void add(); +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java new file mode 100644 index 0000000000..614b60d9c9 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java @@ -0,0 +1,11 @@ +package me.lzb.other.proxy; + +/** + * Created by LZB on 2017/3/29. + */ +public class UserServiceImpl implements UserService { + + public void add() { + System.out.println("--------------------add---------------"); + } +} \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java b/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java new file mode 100644 index 0000000000..0a01679ad3 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java @@ -0,0 +1,25 @@ +package me.lzb.other.proxy; + +import org.junit.Test; + +/** + * Created by LZB on 2017/3/29. + */ +public class ProxyTest { + + @Test + public void testProxy() throws Throwable { + // 实例化目标对象 + UserService userService = new UserServiceImpl(); + + // 实例化InvocationHandler + MyInvocationHandler invocationHandler = new MyInvocationHandler(userService); + + // 根据目标对象生成代理对象 + UserService proxy = (UserService) invocationHandler.getProxy(); + + // 调用代理对象的方法 + proxy.add(); + + } +} diff --git a/group24/1148285693/learning2017/pom.xml b/group24/1148285693/learning2017/pom.xml index 9b8d948ff9..8dc41781ba 100644 --- a/group24/1148285693/learning2017/pom.xml +++ b/group24/1148285693/learning2017/pom.xml @@ -22,9 +22,10 @@ + common learning-basic mini-jvm - + other @@ -103,6 +104,11 @@ commons-codec 1.10 + + org.apache.commons + commons-collections4 + 4.1 + diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java deleted file mode 100644 index 4dae60e12b..0000000000 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.ipk2015.coding2017.basic; - -import java.util.EmptyStackException; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - if(isEmpty()){ - throw new EmptyStackException(); - } - Object data=elementData.remove(size()-1); - return data; - } - - public Object peek(){ - if(isEmpty()){ - throw new EmptyStackException(); - } - return elementData.get(size()-1); - } - public boolean isEmpty(){ - return size()==0; - } - public int size(){ - return elementData.size(); - } -} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/Stack.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/Stack.java new file mode 100644 index 0000000000..f9c5235e20 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/Stack.java @@ -0,0 +1,42 @@ +package com.github.ipk2015.coding2017.basic.stack; + +import java.util.EmptyStackException; + +import com.github.ipk2015.coding2017.basic.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + Object data=elementData.remove(size()-1); + return data; + } + + public Object peek(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return size()==0; + } + public int size(){ + return elementData.size(); + } + public String toString(){ + StringBuffer buffer=new StringBuffer(); + int size=elementData.size(); + for(int i=0;is.size()){ + throw new RuntimeException("len超出范围"); + } + Stack tempStack=new Stack(); + for(int i=0;i0){ + stack.push(flag); + }else if(flag<0){ + if(stack.size()==0){ + return false; + } + Integer peek = (Integer)stack.peek(); + if(peek+flag==0){ + stack.pop(); + }else{ + return false; + } + } + } + if(stack.size()>0){ + return false; + } + return true; + } + private static int switchChar(char c){ + int result=0; + switch(c){ + case '(': + result=1; + break; + case ')': + result=-1; + break; + case '[': + result=2; + break; + case ']': + result=-2; + break; + case '{': + result=3; + break; + case '}': + result=-3; + break; + default: + break; + } + return result; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/expr/InfixExpr.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..61bbe91b2f --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/expr/InfixExpr.java @@ -0,0 +1,83 @@ +package com.github.ipk2015.coding2017.basic.stack.expr; + +import com.github.ipk2015.coding2017.basic.stack.Stack; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + String[] elements = getElementArray(expr); + + Stack numStack = new Stack(); + Stack operStack = new Stack(); + + manageMultiAndDivOper(elements,numStack,operStack); + + return manageAddAndMinusOper(numStack,operStack); + } + + private void manageMultiAndDivOper(String[] elements,Stack numStack,Stack operStack){ + float preElement,nextElement; + for(int i = 0; i < elements.length;i++){ + if(i%2 == 0){ + numStack.push(Float.valueOf(elements[i])); + }else{ + + if(elements[i].equals("+") || elements[i].equals("-")){ + operStack.push(elements[i]); + }else{ + preElement = (Float)numStack.pop(); + i++; + nextElement = Float.valueOf(elements[i]); + numStack.push(doBaseOper(preElement,nextElement,elements[i-1])); + } + } + } + } + + private float manageAddAndMinusOper(Stack numStack,Stack operStack){ + float result = 0f;; + while(!operStack.isEmpty()){ + result = doBaseOper(result,(Float)numStack.pop(),(String)operStack.pop()); + } + result += (Float)numStack.pop(); + return result; + } + + private float doBaseOper(float preData,float nextData,String oper){ + switch(oper){ + case "+": + return preData+nextData; + case "-": + return preData-nextData; + case "*": + return preData*nextData; + case "/": + return preData/nextData; + default: + throw new RuntimeException("could not recognise oper:"+oper); + } + } + + public String[] getElementArray(String expression){ + char[] charArray = expression.toCharArray(); + StringBuffer stringBuffer = new StringBuffer(); + + for(int i = 0;i items = new ArrayList(); + + private static class LineNumberItem{ + int startPC; + int lineNum; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + public void addLineNumberItem(LineNumberItem item){ + this.items.add(item); + } + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + + } + + public static LineNumberTable parse(ByteCodeIterator iter){ + int attrNameIndex = iter.nextUNToInt(2); + int attrLength = iter.nextUNToInt(4); + LineNumberTable lineNumberTable = new LineNumberTable(attrNameIndex,attrLength); + int lineNumTableLen = iter.nextUNToInt(2); + for(int i = 0;i < lineNumTableLen;i++){ + LineNumberItem lineNumberItem = new LineNumberItem(); + lineNumberItem.setStartPC(iter.nextUNToInt(2)); + lineNumberItem.setLineNum(iter.nextUNToInt(2)); + lineNumberTable.addLineNumberItem(lineNumberItem); + } + return lineNumberTable; + } + + + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/LocalVariableItem.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..60f77e5616 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/LocalVariableItem.java @@ -0,0 +1,39 @@ +package com.github.ipk2015.coding2017.minijvm.attr; + +public class LocalVariableItem { + private int startPC; + private int length; + private int nameIndex; + private int descIndex; + private int index; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getNameIndex() { + return nameIndex; + } + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + public int getDescIndex() { + return descIndex; + } + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/LocalVariableTable.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..3b895ce32e --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/LocalVariableTable.java @@ -0,0 +1,41 @@ +package com.github.ipk2015.coding2017.minijvm.attr; + + +import java.util.ArrayList; +import java.util.List; + + +import com.github.ipk2015.coding2017.minijvm.loader.ByteCodeIterator; + + + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LocalVariableTable parse(ByteCodeIterator iter){ + int attrNameIndex = iter.nextUNToInt(2); + int attrLength = iter.nextUNToInt(4); + LocalVariableTable table = new LocalVariableTable(attrNameIndex,attrLength); + int tableLen = iter.nextUNToInt(2); + for(int i = 0;i < tableLen;i++){ + LocalVariableItem item = new LocalVariableItem(); + item.setStartPC(iter.nextUNToInt(2)); + item.setLength(iter.nextUNToInt(2)); + item.setNameIndex(iter.nextUNToInt(2)); + item.setDescIndex(iter.nextUNToInt(2)); + item.setIndex(iter.nextUNToInt(2)); + table.addLocalVariableItem(item); + } + return table; + } + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/StackMapTable.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/StackMapTable.java new file mode 100644 index 0000000000..b88218c52e --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/attr/StackMapTable.java @@ -0,0 +1,29 @@ +package com.github.ipk2015.coding2017.minijvm.attr; + +import com.github.ipk2015.coding2017.minijvm.loader.ByteCodeIterator; + +public class StackMapTable extends AttributeInfo{ + + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.nextUNToInt(2); + int len = iter.nextUNToInt(4); + StackMapTable t = new StackMapTable(index,len); + + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + String code = iter.nextUNToHexString(len); + t.setOriginalCode(code); + + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/AccessFlag.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/AccessFlag.java new file mode 100644 index 0000000000..138f12d2c7 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/AccessFlag.java @@ -0,0 +1,37 @@ +package com.github.ipk2015.coding2017.minijvm.clz; + + + +public class AccessFlag { + public static int ACC_PUBLIC = 0x0001; + public static int ACC_FINAL = 0x0002; + public static int ACC_SUPER = 0x0020; + public static int ACC_INTEERFACE = 0x0200; + public static int ACC_ABSTRACT = 0x0400; + public static int ACC_SYNTHETIC = 0x1000; + public static int ACC_ANNOTATION = 0x2000; + public static int ACC_ENUM = 0x4000; + + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & ACC_PUBLIC) != 0; + } + + public boolean isFinalClass(){ + return (this.flagValue & ACC_FINAL) != 0; + } + +} \ No newline at end of file diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassFile.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassFile.java new file mode 100644 index 0000000000..5cecfc3bff --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassFile.java @@ -0,0 +1,92 @@ +package com.github.ipk2015.coding2017.minijvm.clz; + +import java.util.ArrayList; +import java.util.List; + +import com.github.ipk2015.coding2017.minijvm.constant.ClassInfo; +import com.github.ipk2015.coding2017.minijvm.constant.ConstantPool; +import com.github.ipk2015.coding2017.minijvm.field.Field; +import com.github.ipk2015.coding2017.minijvm.method.Method; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + private List fields = new ArrayList(); + private List methods = new ArrayList(); + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public void addField(Field f){ + this.fields.add(f); + } + public List getFields(){ + return this.fields; + } + public void addMethod(Method m){ + this.methods.add(m); + } + public List getMethods() { + return methods; + } + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + + System.out.println("Super Class Name:"+ getSuperClassName()); + + + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassIndex.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassIndex.java new file mode 100644 index 0000000000..834863c9ee --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassIndex.java @@ -0,0 +1,21 @@ +package com.github.ipk2015.coding2017.minijvm.clz; + + + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ClassInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ClassInfo.java new file mode 100644 index 0000000000..e2e0f50f61 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ClassInfo.java @@ -0,0 +1,26 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + + + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..663c493b57 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantPool.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantPool.java new file mode 100644 index 0000000000..ea0e135a23 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantPool.java @@ -0,0 +1,29 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..b74fb427ba --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..cc5352db6e --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..d1cd005111 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..38eef91f32 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java new file mode 100644 index 0000000000..8f23231e72 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java new file mode 100644 index 0000000000..d94a267bbc --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/field/Field.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/field/Field.java new file mode 100644 index 0000000000..7d9b5e1249 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/field/Field.java @@ -0,0 +1,41 @@ +package com.github.ipk2015.coding2017.minijvm.field; + +import com.github.ipk2015.coding2017.minijvm.constant.ConstantPool; +import com.github.ipk2015.coding2017.minijvm.loader.ByteCodeIterator; + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + + + private ConstantPool pool; + + public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + + + public static Field parse(ConstantPool pool,ByteCodeIterator iter){ + int accessFlag = iter.nextUNToInt(2); + int nameIndex = iter.nextUNToInt(2); + int descriptorIndex = iter.nextUNToInt(2); + int attrCount = iter.nextUNToInt(2); + if(attrCount != 0){ + throw new RuntimeException("字段的属性不为0"); + } + return new Field(accessFlag,nameIndex,descriptorIndex,pool); + } + + public String toString(){ + + return pool.getUTF8String(nameIndex)+":"+pool.getUTF8String(descriptorIndex); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..141331fe4b --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java @@ -0,0 +1,31 @@ +package com.github.ipk2015.coding2017.minijvm.loader; + +import java.util.Arrays; + +import com.github.ipk2015.coding2017.minijvm.util.Util; + +public class ByteCodeIterator { + private byte[] byteArray; + int pos=0; + + public ByteCodeIterator(byte[] codes){ + this.byteArray=codes; + } + + public int nextUNToInt(int n){ + return Util.byteToInt(nextUNToArray(n)); + } + + public String nextUNToHexString(int n){ + return Util.byteToHexString(nextUNToArray(n)); + } + + public byte[] nextUNToArray(int n){ + byte[] bytes=Arrays.copyOfRange(byteArray, pos, pos+n); + pos=pos+n; + return bytes; + } + public void back(int n) { + this.pos -= n; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java index ab7f54a796..2f854174dd 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java @@ -6,67 +6,138 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.github.ipk2015.coding2017.minijvm.clz.ClassFile; + + + + + public class ClassFileLoader { private List clzPaths = new ArrayList(); - public byte[] readBinaryCode(String className) throws IOException { - className=getCompleteClassName(className); - File file=null; - for(String path:clzPaths){ - file=new File(path+"\\"+className); - if(file.exists()){ - break; - } + public byte[] readBinaryCode(String className) { + + className = className.replace('.', File.separatorChar) +".class"; + + for(String path : this.clzPaths){ + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } } - if(null==file){ - throw new FileNotFoundException(className); - } - ByteArrayOutputStream bos=new ByteArrayOutputStream((int)file.length()); - BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); - int size=1024; - byte[] buffer=new byte[size]; - int length=0; - while((length=in.read(buffer, 0, size))!=-1){ - bos.write(buffer,0,length); + + return null; + + + + } + + private byte[] loadClassFile(String clzFileName) { + + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; } - return bos.toByteArray(); } + public void addClassPath(String path) { - clzPaths.add(path); + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); + } public String getClassPath(){ - StringBuffer buffer=new StringBuffer(); - for(String path:clzPaths){ - buffer.append(path+";"); - } - buffer.deleteCharAt(buffer.length()-1); - return buffer.toString(); + return StringUtils.join(this.clzPaths,";"); } - private String getCompleteClassName(String name){ - if(!name.endsWith(".class")){ - name=name+".class"; + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + } + + + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i-1){ - name=name.substring(pointPos+1); + return buffer.toString(); + } + + private byte[] loadClassFile_V1(String clzFileName) { + + BufferedInputStream bis = null; + + try { + + File f = new File(clzFileName); + + + bis = new BufferedInputStream(new FileInputStream(f)); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + + byte[] buffer = new byte[1024]; + int length = -1; + + while((length = bis.read(buffer)) != -1){ + bos.write(buffer, 0, length); + } + + byte [] codes = bos.toByteArray(); + + return codes; + + } catch(IOException e){ + e.printStackTrace(); + + } finally{ + if(bis != null){ + try { + bis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } - return name; + return null; + } + -} +} \ No newline at end of file diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java new file mode 100644 index 0000000000..9879d5a596 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java @@ -0,0 +1,125 @@ +package com.github.ipk2015.coding2017.minijvm.loader; + + + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader1 { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws IOException { + className=getCompleteClassName(className); + File file=null; + for(String path:clzPaths){ + file=new File(path+"\\"+className); + if(file.exists()){ + break; + } + } + if(null==file){ + throw new FileNotFoundException(className); + } + ByteArrayOutputStream bos=new ByteArrayOutputStream((int)file.length()); + BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); + int size=1024; + byte[] buffer=new byte[size]; + int length=0; + while((length=in.read(buffer, 0, size))!=-1){ + bos.write(buffer,0,length); + } + return bos.toByteArray(); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuffer buffer=new StringBuffer(); + for(String path:clzPaths){ + buffer.append(path+";"); + } + buffer.deleteCharAt(buffer.length()-1); + return buffer.toString(); + } + + private String getCompleteClassName(String name){ + if(!name.endsWith(".class")){ + name=name+".class"; + } + int pointPos=name.lastIndexOf(".", name.length()-7); + if(pointPos>-1){ + name=name.substring(pointPos+1); + } + return name; + } + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i parseFields = parseFields(constantPool,iterator); + for(Field f:parseFields){ + classFile.addField(f); + } + + List parseMethods = parseMethods(classFile,iterator); + for(Method m:parseMethods){ + classFile.addMethod(m); + } + + return classFile; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + AccessFlag flag = new AccessFlag(iter.nextUNToInt(2)); + return flag; + } + + private ClassIndex parseClassInfex(ByteCodeIterator iter) { + ClassIndex classIndex = new ClassIndex(); + classIndex.setThisClassIndex(iter.nextUNToInt(2)); + classIndex.setSuperClassIndex(iter.nextUNToInt(2)); + return classIndex; + + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + + int poolSize = iter.nextUNToInt(2); + + ConstantPool pool = new ConstantPool(); + pool.addConstantInfo(new NullConstantInfo()); + + int tag; + for(int i = 1;i < poolSize; i++){ + tag = iter.nextUNToInt(1); + switch(tag){ + case ConstantInfo.CLASS_INFO: + meetClassInfo(pool,iter); + break; + case ConstantInfo.FIELD_INFO: + meetFieldInfo(pool,iter); + break; + case ConstantInfo.METHOD_INFO: + meetMethodInfo(pool,iter); + break; + case ConstantInfo.NAME_AND_TYPE_INFO: + meetNameAndTypeInfo(pool,iter); + break; + case ConstantInfo.STRING_INFO: + meetStringInfo(pool,iter); + break; + case ConstantInfo.UTF8_INFO: + meetUTF8Info(pool,iter); + break; + default: + throw new RuntimeException("还没有关于此的处理,tag:"+tag); + } + } + return pool; + } + + private void meetClassInfo(ConstantPool pool,ByteCodeIterator iter){ + ClassInfo info = new ClassInfo(pool); + info.setUtf8Index(iter.nextUNToInt(2)); + pool.addConstantInfo(info); + } + + private void meetFieldInfo(ConstantPool pool,ByteCodeIterator iter){ + FieldRefInfo info = new FieldRefInfo(pool); + info.setClassInfoIndex(iter.nextUNToInt(2)); + info.setNameAndTypeIndex(iter.nextUNToInt(2)); + pool.addConstantInfo(info); + } + + private void meetMethodInfo(ConstantPool pool,ByteCodeIterator iter){ + MethodRefInfo info = new MethodRefInfo(pool); + info.setClassInfoIndex(iter.nextUNToInt(2)); + info.setNameAndTypeIndex(iter.nextUNToInt(2)); + pool.addConstantInfo(info); + } + + private void meetNameAndTypeInfo(ConstantPool pool,ByteCodeIterator iter){ + NameAndTypeInfo info = new NameAndTypeInfo(pool); + info.setIndex1(iter.nextUNToInt(2)); + info.setIndex2(iter.nextUNToInt(2)); + pool.addConstantInfo(info); + } + + private void meetStringInfo(ConstantPool pool,ByteCodeIterator iter){ + StringInfo info = new StringInfo(pool); + info.setIndex(iter.nextUNToInt(2)); + pool.addConstantInfo(info); + } + + private void meetUTF8Info(ConstantPool pool,ByteCodeIterator iter){ + int length = iter.nextUNToInt(2); + byte[] data = iter.nextUNToArray(length); + String value = null; + try { + value=new String(data,"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + UTF8Info info = new UTF8Info(pool); + info.setLength(length); + info.setValue(value); + pool.addConstantInfo(info); + } + + private void parseInterfaces(ByteCodeIterator iter) { + int interfaceCount = iter.nextUNToInt(2); + + System.out.println("interfaceCount:" + interfaceCount); + + // TODO : 如果实现了interface, 这里需要解析 + } + + private List parseFields(ConstantPool pool,ByteCodeIterator iter){ + List list = new ArrayList(); + int count = iter.nextUNToInt(2); + for(int i = 0;i < count;i++){ + list.add(Field.parse(pool, iter)); + } + return list; + } + + private List parseMethods(ClassFile classFile,ByteCodeIterator iter){ + List list = new ArrayList(); + int count = iter.nextUNToInt(2); + for(int i = 0;i < count;i++){ + list.add(Method.parse(classFile, iter)); + } + return list; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/method/Method.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/method/Method.java new file mode 100644 index 0000000000..8c08991413 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/method/Method.java @@ -0,0 +1,70 @@ +package com.github.ipk2015.coding2017.minijvm.method; + +import com.github.ipk2015.coding2017.minijvm.attr.AttributeInfo; +import com.github.ipk2015.coding2017.minijvm.attr.CodeAttr; +import com.github.ipk2015.coding2017.minijvm.clz.ClassFile; +import com.github.ipk2015.coding2017.minijvm.loader.ByteCodeIterator; + +public class Method { + + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + + private ClassFile clzFile; + + + public ClassFile getClzFile() { + return clzFile; + } + + public int getNameIndex() { + return nameIndex; + } + public int getDescriptorIndex() { + return descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr code) { + this.codeAttr = code; + } + + public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { + this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + + + + + public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ + int accessFlag = iter.nextUNToInt(2); + int nameIndex = iter.nextUNToInt(2); + int descriptorIndex = iter.nextUNToInt(2); + Method method = new Method(clzFile,accessFlag,nameIndex,descriptorIndex); + int attrCount = iter.nextUNToInt(2); + for(int i = 0;i < attrCount;i++){ + addAttr(clzFile,method,iter); + } + return method; + } + private static void addAttr(ClassFile clzFile,Method method,ByteCodeIterator iter){ + int nameIndex = iter.nextUNToInt(2); + iter.back(2); + String attrName = clzFile.getConstantPool().getUTF8String(nameIndex); + if(AttributeInfo.CODE.equalsIgnoreCase(attrName)){ + method.setCodeAttr(CodeAttr.parse(clzFile, iter)); + }else{ + throw new RuntimeException("方法的此属性不存在:"+attrName); + } + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java index 61440e39c2..0a3979119d 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/test/ClassFileloaderTest.java @@ -3,13 +3,24 @@ import java.io.IOException; +import java.util.List; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.github.ipk2015.coding2017.minijvm.clz.ClassFile; +import com.github.ipk2015.coding2017.minijvm.clz.ClassIndex; +import com.github.ipk2015.coding2017.minijvm.constant.ClassInfo; +import com.github.ipk2015.coding2017.minijvm.constant.ConstantPool; +import com.github.ipk2015.coding2017.minijvm.constant.MethodRefInfo; +import com.github.ipk2015.coding2017.minijvm.constant.NameAndTypeInfo; +import com.github.ipk2015.coding2017.minijvm.constant.UTF8Info; +import com.github.ipk2015.coding2017.minijvm.field.Field; import com.github.ipk2015.coding2017.minijvm.loader.ClassFileLoader; +import com.github.ipk2015.coding2017.minijvm.loader.ClassFileLoader1; +import com.github.ipk2015.coding2017.minijvm.method.Method; @@ -23,6 +34,18 @@ public class ClassFileloaderTest { static String path2 = "C:\temp"; static String path3 = "E:\\javaImprove\\git\\group24\\121111914\\src\\com\\github\\ipk2015\\coding2017\\minijvm\\bin"; + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path3); +// String className = "com.coderising.jvm.test.EmployeeV1"; + String className = "EmployeeV1";//老师的class文件单独放在这里,只有类名 + + clzFile = loader.loadClass(className); + clzFile.print(); + } @Before @@ -36,7 +59,7 @@ public void tearDown() throws Exception { @Test public void testClassPath(){ - ClassFileLoader loader = new ClassFileLoader(); + ClassFileLoader1 loader = new ClassFileLoader1(); loader.addClassPath(path1); loader.addClassPath(path2); @@ -47,25 +70,26 @@ public void testClassPath(){ } @Test - public void testClassFileLength() throws IOException { + public void testClassFileLength() { ClassFileLoader loader = new ClassFileLoader(); loader.addClassPath(path3); - String className = "com.coderising.jvm.test.EmployeeV1"; + String className = "EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); - System.out.println( byteCodes.length+""); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 - Assert.assertEquals(835, byteCodes.length); + Assert.assertEquals(1056, byteCodes.length); } @Test - public void testMagicNumber() throws IOException{ + public void testMagicNumber(){ ClassFileLoader loader = new ClassFileLoader(); loader.addClassPath(path3); - String className = "com.coderising.jvm.test.EmployeeV1"; + String className = "EmployeeV1"; byte[] byteCodes = loader.readBinaryCode(className); byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; @@ -77,21 +101,183 @@ public void testMagicNumber() throws IOException{ + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + /** + * 下面是第三次JVM课应实现的测试用例 + */ + @Test + public void testReadFields(){ + + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + @Test + public void testMethods(){ + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab7000c2a2bb5000f2a1cb50011b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb5000fb1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50011b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b2001c1222b60024b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb000159122b101db7002d4c2bb6002fb1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } } diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java new file mode 100644 index 0000000000..b8eaef98ef --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java @@ -0,0 +1,26 @@ +package com.github.ipk2015.coding2017.minijvm.util; + + + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws IOException { + String clzFileName = ""; + byte[] byteCodes = null; + boolean classFound = false; + + for (String path : clzPaths) { + clzFileName = path + File.separatorChar + className.replace('.', File.separatorChar) + ".class"; + + if ((byteCodes = loadClassFile(clzFileName)) != null){ + classFound = true; + return byteCodes; + } + } + + if (classFound == false) { + throw new FileNotFoundException(clzFileName); + } + + return null; + } + + private byte[] loadClassFile(String clzFileName) throws IOException { + + File file = new File(clzFileName); + if(!file.exists()){ +// throw new FileNotFoundException(clzFileName); + return null; + } + + ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length()); + BufferedInputStream in = null; + + try { + in = new BufferedInputStream(new FileInputStream(file)); + + int buf_size = 1024; + byte[] buffer = new byte[buf_size]; + int len = 0; + + while ((len = in.read(buffer, 0, buf_size)) != -1) { + bos.write(buffer, 0, len); + } + + return bos.toByteArray(); + + } catch (IOException e) { + e.printStackTrace(); + throw e; + } finally { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + bos.close(); + } + } + + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath_V1(){ + + return null; + } + + public String getClassPath(){ + String classPath = ""; + for (int i = 0; i < clzPaths.size(); i++) { + classPath += clzPaths.get(i); + if (i != clzPaths.size() - 1) { + classPath += ";"; + } + } + return classPath; + } + + + +} diff --git a/group24/1525619747/homework_20170402/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group24/1525619747/homework_20170402/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..3192f6596c --- /dev/null +++ b/group24/1525619747/homework_20170402/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,86 @@ +package com.coderising.jvm.test; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + +public class ClassFileloaderTest { + + static String path1 = "F:\\Project\\Java_Project\\Java_SE\\coding2017\\group24\\1525619747\\homework_20170402\\bin"; + static String path2 = "C:\\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() throws IOException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); +// System.out.println(byteCodes.length); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + System.out.println(byteCodes[0] + " " + byteCodes[1] + " " + byteCodes[2] + " " +byteCodes[3]); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); +// System.out.println(acctualValue); + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i 0) { @@ -167,14 +168,6 @@ public Object[] toArray() { return Arrays.copyOf(elementData, size()); } - /** - * A version of rangeCheck used by add and addAll. - */ - private void rangeCheckForAdd(int index) { - if (index > size() - 1 || index < 0) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - } /** * Constructs an IndexOutOfBoundsException detail message. @@ -192,7 +185,7 @@ private String outOfBoundsMsg(int index) { * which throws an ArrayIndexOutOfBoundsException if index is negative. */ private void rangeCheck(int index) { - if (index >= size()) { + if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java deleted file mode 100644 index 01ba928128..0000000000 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,484 +0,0 @@ -package com.johnChnia.coding2017.basic; - -import java.util.NoSuchElementException; - -/** - * Created by john on 2017/3/9. - * - * @// TODO: 2017/4/1 支持Iterator - */ - -public class LinkedList implements List { - - private Node first = null; - private int size = 0; - - /** - * Constructs an empty list. - */ - public LinkedList() { - - } - - - private static class Node { - T element; - Node next; - Node prev; - } - - /** - * Appends the specified element to the end of this list. - * - * @param element element to be appended to this list - */ - public void add(E element) { - Node newNode = new Node<>(); - if (first == null) { - addWhenListIsEmpty(newNode, element); - return; - } - Node last = first; - while (last.next != null) - last = last.next; - last.next = newNode; - newNode.prev = last; - newNode.next = null; - newNode.element = element; - size++; - } - - private void addWhenListIsEmpty(Node newNode, E element) { - first = newNode; - first.element = element; - first.next = null; - first.prev = null; - size++; - } - - /** - * Inserts the specified element at the beginning of this list. - * - * @param element the element to add - */ - public void addFirst(E element) { - Node newNode = new Node<>(); - if (first == null) { - addWhenListIsEmpty(newNode, element); - return; - } - newNode.next = first; - newNode.prev = null; - newNode.element = element; - - first.prev = newNode; - first = newNode; - size++; - } - - /** - * Appends the specified element to the end of this list. - * - * @param element element to be appended to this list. - */ - public void addLast(E element) { - add(element); - } - - - /** - * Inserts the specified element at the specified position in this list. - * Shifts the element currently at that position (if any) and any - * subsequent elements to the right (adds one to their indices). - * - * @param index index at which the specified element is to be inserted. - * @param element element to be inserted. - * @throws RuntimeException if list size less than 2. - */ - public void add(int index, E element) { - if (size() < 2) - throw new RuntimeException("list size should greater than or equal to 2"); - isElementIndex(index); - if (index == 0) { - addFirst(element); - return; - } else { - Node temp = new Node<>(); - Node temp2 = first; - for (int i = 0; i < index; i++) { - temp2 = temp2.next; - } - temp2.prev.next = temp; - temp.prev = temp2.prev; - - temp.next = temp2; - temp2.prev = temp; - temp.element = element; - } - size++; - - } - - - /** - * remove last element in the list. - * - * @throws RuntimeException if the list is empty. - */ - public E removeLast() { - if (size == 0) - throw new RuntimeException("linkList size should greater than or equal to 1"); - E element; - Node next = first.next; - if (next == null) { - element = first.element; - - first = null; - } else { - Node last = first; - while (last.next != null) - last = last.next; - last.prev.next = null; - - element = last.element; - - last = null; // help GC - } - size--; - return element; - } - - - /** - * @param index - * @return - * @// TODO: 2018/3/14 if i am happy, i will implement it right now! - */ - public E remove(int index) { - return null; - } - - /** - * Removes and returns the first element from this list. - * - * @return the first element from this list - */ - public E removeFirst() { - Node f = first; - if (f == null) - throw new NoSuchElementException(); - E element = f.element; - Node next = first.next; - first.element = null; - first.next = null; // help GC - - first = next; - if (next != null) { - next.prev = null; - } - size--; - return element; - } - - - /** - * Returns the element at the specified position in this list. - * - * @param index index of the element to return - * @return the element at the specified position in this list - */ - public E get(int index) { - checkElementIndex(index); - Node node = first; - if (index == 0) { - return first.element; - } - for (int i = 0; i < index; i++) { - node = node.next; - } - return node.element; - } - - /** - * Returns the first element in this list. - * - * @return the first element in this list - * @throws NoSuchElementException if this list is empty - */ - public E getFirst() { - final Node f = first; - if (f == null) - throw new NoSuchElementException(); - return f.element; - } - - /** - * Returns the number of elements in this list. - * - * @return the number of elements in this list - */ - public int size() { - return size; - } - - private void checkElementIndex(int index) { - if (!isElementIndex(index)) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - } - - - /** - * Tells if the argument is the index of an existing element. - */ - private boolean isElementIndex(int index) { - return index >= 0 && index < size; - } - - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(first.element); - Node temp = first; - while (temp.next != null) { - temp = temp.next; - sb.append("→"); - sb.append(temp.element); - } - return sb.toString(); - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse() { - Node next; - Node current = first; - for (int i = 0; i < size; i++) { - next = current.next; - current.next = current.prev; - current.prev = next; - if (next != null) { - current = next; - } - } - first = current; - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf() { - int index = size() / 2; - Node current = first; - Node prev; - for (int i = 0; i < index; i++) { - prev = current; - current = current.next; - delete(prev); - } - current.prev = null; - first = current; - size = size - index; - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * - * @param i - * @param length - */ - public void remove(int i, int length) { - checkElementIndex(i); - if (length + i > size()) { - throw new IllegalArgumentException("Length + i should less than or equal " + size()); - } - Node head = first; - Node tail = first; - if (i == 0 && length == size()) { - first = null; - } else if (i == 0 && length < size()) { - for (int j = 0; j < length; j++) { - head = head.next; - } - head.prev = null; - first = head; - } else { - for (int j = 0; j < i; j++) { - head = head.next; - } - head = head.prev; - for (int j = 0; j < length + i; j++) { - tail = tail.next; - } - head.next = tail; - if (tail != null) { - tail.prev = head; - } - } - size = size - length; - } - - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * - * @param list - */ - public int[] getElements(LinkedList list) { - int[] newArray = new int[list.size()]; - Node mapNode = list.first; - Node valueNode = this.first; - int indexOfList = 0; - int indexOfArray = 0; - while (mapNode != null && valueNode != null) { - int mapValue = (int) mapNode.element; - if (mapValue == indexOfList) { - newArray[indexOfArray] = (int) valueNode.element; - mapNode = mapNode.next; - valueNode = valueNode.next; - indexOfArray++; - } else { - mapNode = mapNode.next; - } - indexOfList++; - } - return newArray; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在listB中出现的元素 - * - * @param list - */ - - public void subtract(LinkedList list) { - Node pNode = first; - Node qNode = list.first; - Node prev = null; - Node deletedNode; - while (pNode != null && qNode != null) { - if ((int) qNode.element < (int) pNode.element) { - qNode = qNode.next; - } else if ((int) qNode.element > (int) pNode.element) { - prev = pNode; - pNode = pNode.next; - } else { - if (prev == null) { // 头结点 - first = pNode.next; - } else { - prev.next = pNode.next; - } - deletedNode = pNode; - pNode = pNode.next; - qNode = qNode.next; - delete(deletedNode); - size--; - } - } - - } - - private void delete(Node node) { - node.element = null; - node.prev = null; - node.next = null; - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues() { - Node current = first; - Node next = current.next; - while (next != null) { - if (current.element == next.element) { - current.next = next.next; - if (next.next != null) { - next.next.prev = current; - } - delete(next); - next = current.next; - size--; - } else { - current = current.next; - next = next.next; - } - - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * - * @param min - * @param max - */ - public void removeRange(int min, int max) { - Node current = first; - Node prev = null; - Node deletedNode; - while (current != null) { - if ((int) current.element >= max) { - break; - } - if ((int) current.element > min && (int) current.element < max) { - if (prev == null) { // 头结点 - first = current.next; - } else { - prev.next = current.next; - } - deletedNode = current; - current = current.next; - delete(deletedNode); // help gc - size--; - } else { - prev = current; - current = current.next; - } - } - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * - * @param list - */ - public LinkedList intersection(LinkedList list) { - LinkedList l = new LinkedList(); - Node pNode = first; - Node qNode = list.first; - while (pNode != null && qNode != null) { - if ((int) pNode.element < (int) qNode.element) { - pNode = pNode.next; - } else if ((int) pNode.element > (int) qNode.element) { - qNode = qNode.next; - } else { - l.add(pNode.element); - pNode = pNode.next; - qNode = qNode.next; - } - } - return l; - } - - - /** - * Constructs an IndexOutOfBoundsException detail message. - * Of the many possible refactorings of the error handling code, - * this "outlining" performs best with both server and client VMs. - */ - private String outOfBoundsMsg(int index) { - return "Index: " + index + ", Size: " + size; - } -} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java deleted file mode 100644 index f43ea52397..0000000000 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.johnChnia.coding2017.basic; - -import java.util.EmptyStackException; - -/** - * Created by john on 2017/3/10. - */ -public class Stack { - private LinkedList linkList; - - /** - * Creates an empty Stack. - */ - public Stack() { - linkList = new LinkedList<>(); - } - - - /** - * Pushes an item onto the top of this stack. - * - * @param element the element to be pushed onto this stack. - */ - public void push(E element) { - linkList.addFirst(element); - } - - /** - * Removes the object at the top of this stack and returns that - * object as the value of this function. - * - * @return The object at the top of this stack. - * @throws EmptyStackException if this stack is empty. - */ - public E pop() { - if (empty()) { - throw new EmptyStackException(); - } - return linkList.removeFirst(); - } - - /** - * Looks at the object at the top of this stack without removing it - * from the stack. - * - * @return the object at the top of this stack. - * @throws EmptyStackException if this stack is empty. - */ - public E peek() { - if (empty()) { - throw new EmptyStackException(); - } - return linkList.getFirst(); - } - - /** - * Tests if this stack is empty. - * - * @return true if and only if this stack contains - * no elements; false otherwise. - */ - public boolean empty() { - return linkList.size() == 0; - } - - public String toString() { - return linkList.toString(); - } - - /** - * Returns the number of elements in this stack. - * - * @return the number of elements in this stack - */ - public int size() { - return linkList.size(); - } -} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LRUPageFrame.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..cdc327fb30 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LRUPageFrame.java @@ -0,0 +1,128 @@ +package com.johnChnia.coding2017.basic.linklist; + +/** + * Created by john on 2017/4/6. + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中的对象 + * + * @param pageNum 对象值 + */ + public void access(int pageNum) { + if (first == null) { + Node node = createNode(pageNum); + first = last = node; + capacity--; + } else if (getNode(pageNum) == null) { + if (capacity == 0) { + Node lastNode = first; + while (lastNode.next != null) { + lastNode = lastNode.next; + } + lastNode.prev.next = null; + last = lastNode.prev; + delete(lastNode); + capacity++; + } + Node node = createNode(pageNum); + node.next = first; + node.prev = null; + first.prev = node; + first = node; + capacity--; + + } else { + if (first.pageNum != pageNum) { + Node node = getNode(pageNum); + if (node.next != null) { + node.prev.next = node.next; + node.next.prev = node.prev; + } else { + node.prev.next = null; + last = node.prev; + } + node.next = first; + node.prev = null; + first.prev = node; + first = node; + } + } + + } + + /** + * 删除节点 + */ + private void delete(Node node) { + node.pageNum = 0; + node.next = null; + node.prev = null; + } + + /** + * @param pageNum 页号 + * @return 节点 + */ + private Node createNode(int pageNum) { + Node node = new Node(); + node.pageNum = pageNum; + node.next = null; + node.prev = null; + return node; + } + + + /** + * @param pageNum 页号 + * @return 如果LRUPageFrame包含该pageNum就返回该节点,否则返回null + */ + private Node getNode(int pageNum) { + for (Node node = first; node != null; node = node.next) { + if (node.pageNum == pageNum) { + return node; + } + } + return null; + } + + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LRUPageFrameTest.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..e71abd393d --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,30 @@ +package com.johnChnia.coding2017.basic.linklist; + +import org.junit.Assert; +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..10f7edd0a6 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java @@ -0,0 +1,486 @@ +package com.johnChnia.coding2017.basic.linklist; + +import com.johnChnia.coding2017.basic.List; + +import java.util.NoSuchElementException; + +/** + * Created by john on 2017/3/9. + * + * @// TODO: 2017/4/1 支持Iterator + */ + +public class LinkedList implements List { + + private Node first = null; + private int size = 0; + + /** + * Constructs an empty list. + */ + public LinkedList() { + + } + + + private static class Node { + T element; + Node next; + Node prev; + } + + /** + * Appends the specified element to the end of this list. + * + * @param element element to be appended to this list + */ + public void add(E element) { + Node newNode = new Node<>(); + if (first == null) { + addWhenListIsEmpty(newNode, element); + return; + } + Node last = first; + while (last.next != null) + last = last.next; + last.next = newNode; + newNode.prev = last; + newNode.next = null; + newNode.element = element; + size++; + } + + private void addWhenListIsEmpty(Node newNode, E element) { + first = newNode; + first.element = element; + first.next = null; + first.prev = null; + size++; + } + + /** + * Inserts the specified element at the beginning of this list. + * + * @param element the element to add + */ + public void addFirst(E element) { + Node newNode = new Node<>(); + if (first == null) { + addWhenListIsEmpty(newNode, element); + return; + } + newNode.next = first; + newNode.prev = null; + newNode.element = element; + + first.prev = newNode; + first = newNode; + size++; + } + + /** + * Appends the specified element to the end of this list. + * + * @param element element to be appended to this list. + */ + public void addLast(E element) { + add(element); + } + + + /** + * Inserts the specified element at the specified position in this list. + * Shifts the element currently at that position (if any) and any + * subsequent elements to the right (adds one to their indices). + * + * @param index index at which the specified element is to be inserted. + * @param element element to be inserted. + * @throws RuntimeException if list size less than 2. + */ + public void add(int index, E element) { + if (size() < 2) + throw new RuntimeException("list size should greater than or equal to 2"); + isElementIndex(index); + if (index == 0) { + addFirst(element); + return; + } else { + Node temp = new Node<>(); + Node temp2 = first; + for (int i = 0; i < index; i++) { + temp2 = temp2.next; + } + temp2.prev.next = temp; + temp.prev = temp2.prev; + + temp.next = temp2; + temp2.prev = temp; + temp.element = element; + } + size++; + + } + + + /** + * remove last element in the list. + * + * @throws RuntimeException if the list is empty. + */ + public E removeLast() { + if (size == 0) + throw new RuntimeException("linkList size should greater than or equal to 1"); + E element; + Node next = first.next; + if (next == null) { + element = first.element; + + first = null; + } else { + Node last = first; + while (last.next != null) + last = last.next; + last.prev.next = null; + + element = last.element; + + last = null; // help GC + } + size--; + return element; + } + + + /** + * @param index + * @return + * @// TODO: 2018/3/14 if i am happy, i will implement it right now! + */ + public E remove(int index) { + return null; + } + + /** + * Removes and returns the first element from this list. + * + * @return the first element from this list + */ + public E removeFirst() { + Node f = first; + if (f == null) + throw new NoSuchElementException(); + E element = f.element; + Node next = first.next; + first.element = null; + first.next = null; // help GC + + first = next; + if (next != null) { + next.prev = null; + } + size--; + return element; + } + + + /** + * Returns the element at the specified position in this list. + * + * @param index index of the element to return + * @return the element at the specified position in this list + */ + public E get(int index) { + checkElementIndex(index); + Node node = first; + if (index == 0) { + return first.element; + } + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.element; + } + + /** + * Returns the first element in this list. + * + * @return the first element in this list + * @throws NoSuchElementException if this list is empty + */ + public E getFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return f.element; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + + /** + * Tells if the argument is the index of an existing element. + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(first.element); + Node temp = first; + while (temp.next != null) { + temp = temp.next; + sb.append("→"); + sb.append(temp.element); + } + return sb.toString(); + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node next; + Node current = first; + for (int i = 0; i < size; i++) { + next = current.next; + current.next = current.prev; + current.prev = next; + if (next != null) { + current = next; + } + } + first = current; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int index = size() / 2; + Node current = first; + Node prev; + for (int i = 0; i < index; i++) { + prev = current; + current = current.next; + delete(prev); + } + current.prev = null; + first = current; + size = size - index; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + checkElementIndex(i); + if (length + i > size()) { + throw new IllegalArgumentException("Length + i should less than or equal " + size()); + } + Node head = first; + Node tail = first; + if (i == 0 && length == size()) { + first = null; + } else if (i == 0 && length < size()) { + for (int j = 0; j < length; j++) { + head = head.next; + } + head.prev = null; + first = head; + } else { + for (int j = 0; j < i; j++) { + head = head.next; + } + head = head.prev; + for (int j = 0; j < length + i; j++) { + tail = tail.next; + } + head.next = tail; + if (tail != null) { + tail.prev = head; + } + } + size = size - length; + } + + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + int[] newArray = new int[list.size()]; + Node mapNode = list.first; + Node valueNode = this.first; + int indexOfList = 0; + int indexOfArray = 0; + while (mapNode != null && valueNode != null) { + int mapValue = (int) mapNode.element; + if (mapValue == indexOfList) { + newArray[indexOfArray] = (int) valueNode.element; + mapNode = mapNode.next; + valueNode = valueNode.next; + indexOfArray++; + } else { + valueNode = valueNode.next; + } + indexOfList++; + } + return newArray; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + Node pNode = first; + Node qNode = list.first; + Node prev = null; + Node deletedNode; + while (pNode != null && qNode != null) { + if ((int) qNode.element < (int) pNode.element) { + qNode = qNode.next; + } else if ((int) qNode.element > (int) pNode.element) { + prev = pNode; + pNode = pNode.next; + } else { + if (prev == null) { // 头结点 + first = pNode.next; + } else { + prev.next = pNode.next; + } + deletedNode = pNode; + pNode = pNode.next; + qNode = qNode.next; + delete(deletedNode); + size--; + } + } + + } + + private void delete(Node node) { + node.element = null; + node.prev = null; + node.next = null; + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node current = first; + Node next = current.next; + while (next != null) { + if (current.element == next.element) { + current.next = next.next; + if (next.next != null) { + next.next.prev = current; + } + delete(next); + next = current.next; + size--; + } else { + current = current.next; + next = next.next; + } + + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Node current = first; + Node prev = null; + Node deletedNode; + while (current != null) { + if ((int) current.element >= max) { + break; + } + if ((int) current.element > min && (int) current.element < max) { + if (prev == null) { // 头结点 + first = current.next; + } else { + prev.next = current.next; + } + deletedNode = current; + current = current.next; + delete(deletedNode); // help gc + size--; + } else { + prev = current; + current = current.next; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList l = new LinkedList(); + Node pNode = first; + Node qNode = list.first; + while (pNode != null && qNode != null) { + if ((int) pNode.element < (int) qNode.element) { + pNode = pNode.next; + } else if ((int) pNode.element > (int) qNode.element) { + qNode = qNode.next; + } else { + l.add(pNode.element); + pNode = pNode.next; + qNode = qNode.next; + } + } + return l; + } + + + /** + * Constructs an IndexOutOfBoundsException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/Stack.java new file mode 100644 index 0000000000..c8f1ce7d15 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/Stack.java @@ -0,0 +1,80 @@ +package com.johnChnia.coding2017.basic.stack; + +import com.johnChnia.coding2017.basic.linklist.LinkedList; + +import java.util.EmptyStackException; + +/** + * Created by john on 2017/3/10. + */ +public class Stack { + private LinkedList linkList; + + /** + * Creates an empty Stack. + */ + public Stack() { + linkList = new LinkedList<>(); + } + + + /** + * Pushes an item onto the top of this stack. + * + * @param element the element to be pushed onto this stack. + */ + public void push(E element) { + linkList.addFirst(element); + } + + /** + * Removes the object at the top of this stack and returns that + * object as the value of this function. + * + * @return The object at the top of this stack. + * @throws EmptyStackException if this stack is empty. + */ + public E pop() { + if (empty()) { + throw new EmptyStackException(); + } + return linkList.removeFirst(); + } + + /** + * Looks at the object at the top of this stack without removing it + * from the stack. + * + * @return the object at the top of this stack. + * @throws EmptyStackException if this stack is empty. + */ + public E peek() { + if (empty()) { + throw new EmptyStackException(); + } + return linkList.getFirst(); + } + + /** + * Tests if this stack is empty. + * + * @return true if and only if this stack contains + * no elements; false otherwise. + */ + public boolean empty() { + return linkList.size() == 0; + } + + public String toString() { + return linkList.toString(); + } + + /** + * Returns the number of elements in this stack. + * + * @return the number of elements in this stack + */ + public int size() { + return linkList.size(); + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackUtil.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackUtil.java new file mode 100644 index 0000000000..4b7c4687d7 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackUtil.java @@ -0,0 +1,122 @@ +package com.johnChnia.coding2017.basic.stack; + +/** + * Created by john on 2017/4/7. + */ +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if (s.empty()) { + return; + } + E item = s.pop(); + reverse(s); + insertAtBottom(item, s); + } + + /** + * @param item 插入底部的元素 + * @param s 栈对象引用 + */ + private static void insertAtBottom(E item, Stack s) { + if (s.empty()) { + s.push(item); + } else { + E temp = s.pop(); + insertAtBottom(item, s); + s.push(temp); + } + } + + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s, Object o) { + if (s.empty()) { + return; + } + E item = s.pop(); + if (!o.equals(item)) { //没有考虑o为null的情况 + remove(s, o); + s.push(item); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param len + * @return + */ + public static Object[] getTop(Stack s, int len) { + if (len > s.size()) { + throw new IllegalArgumentException("Len: " + len + ", Size" + s.size()); + } + Object[] array = new Object[len]; + int index = 0; + getArray(s, array, index); + return array; + } + + /** + * 采用递归的方式把len个元素加到数组中,且保持原栈中元素不变。 + * + * @param s 栈 + * @param array Object数组 + * @param index 数组索引 + */ + private static void getArray(Stack s, Object[] array, int index) { + if (s.empty() || index == array.length) { + return; + } + E item = s.pop(); + array[index++] = item; + getArray(s, array, index); + s.push(item); + } + + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * + * @param s + * @return + */ + public static boolean isValidPairs(String s) { // last unclosed first closed + Stack stack = new Stack<>(); + for (int i = 0; i < s.length(); i++) { + String subStr = s.substring(i, i + 1); + if ("([{".contains(subStr)) { + stack.push(subStr); + } else if (")]}".contains(subStr)) { + if (stack.empty()) { + return false; + } + String left = stack.pop(); + if (subStr.equals(")")) { + if(!left.equals("(")) + return false; + }else if(subStr.equals("]")){ + if(!left.equals("[")) + return false; + }else if(subStr.equals("}")){ + if(!left.equals("{")) + return false; + } + } + } + return stack.empty(); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java index e0df250c37..4b8d986990 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java @@ -54,7 +54,6 @@ public void testRemoveElementByIndex() { arrayList4.add(i); } Object removed = arrayList4.remove(4); - System.out.println(arrayList4); assertThat(removed, equalTo(4)); assertThat(arrayList4.size(), equalTo(5)); } diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java index 9a72c0d54a..941d524987 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java @@ -2,7 +2,7 @@ import org.junit.Before; import org.junit.Test; - +import com.johnChnia.coding2017.basic.linklist.LinkedList; import java.util.Arrays; import static org.hamcrest.CoreMatchers.containsString; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java index 9d84f7367a..c6f4ec1b2c 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java @@ -1,5 +1,6 @@ package com.johnChnia.coding2017.basic; +import com.johnChnia.coding2017.basic.stack.Stack; import org.junit.Before; import org.junit.Test; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackUtilTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..0a2f277d49 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackUtilTest.java @@ -0,0 +1,66 @@ +package com.johnChnia.coding2017.basic.stack; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static com.johnChnia.coding2017.basic.stack.StackUtil.*; + + +/** + * Created by john on 2017/4/7. + */ +public class StackUtilTest { + + Stack s1; + Stack s2; + Stack s3; + + @Before + public void setUp() throws Exception { + s1 = new Stack<>(); + s2 = new Stack<>(); + s3 = new Stack<>(); + + } + + @Test + public void testReverse() throws Exception { + for (int i = 0; i < 4; i++) { + s1.push(i); + } + reverse(s1); + Assert.assertEquals("0→1→2→3", s1.toString()); + } + + @Test + public void testRemove() throws Exception { + for (int i = 0; i < 4; i++) { + s2.push(i); + } + remove(s2, 1); + Assert.assertEquals("3→2→0", s2.toString()); + } + + @Test + public void testGetTop() throws Exception { + for (int i = 0; i < 4; i++) { + s3.push(i); + } + Object[] array = getTop(s3, 2); + Assert.assertEquals(array.length, 2); + Assert.assertEquals(array[0], 3); + Assert.assertEquals(array[1], 2); + Assert.assertEquals("3→2→1→0", s3.toString()); + + } + + @Test + public void testIsValidPairs() throws Exception { + String s1 = "([e{d}f])"; + Assert.assertTrue(isValidPairs(s1)); + String s2 = "([b{x]y})"; + Assert.assertFalse(isValidPairs(s2)); + } + +} \ No newline at end of file diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index aafe3654ca..3e4053e1da 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -194,7 +194,7 @@ public void removeFirstHalf() { */ public void remove(int i, int length) { rangeCheck(i); - rangeCheck(i + length - 1); + rangeCheck(i + length - 1);//或者当length超出长度,直接认为删除i后面的所有部分。 if (i == 0) { head = getNode(length); size -= length; @@ -216,8 +216,13 @@ public int[] getElements(LinkedList list) throws Exception { if (list == null) { throw new Exception("传入链表为空?"); } + int[] res = new int[list.size]; for (int i = 0; i < list.size; i++) { + //这个list里的值不一定合法的。可以跳过那些不合法的值。 + if(i > size - 1){ + continue; + } res[i] = Integer.parseInt(get( Integer.parseInt(list.get(i).toString()) - 1).toString()); } @@ -298,6 +303,7 @@ public void removeRange(int min, int max) throws Exception { lastRemove = iter.position - 1; } } + //移动指针的时候,注意不要留下指空的指针。不然相关node会无法被gc if(hasmin && firstRemove == 0){ head = getNode(lastRemove); size -= lastRemove-firstRemove+1; @@ -318,44 +324,28 @@ public void removeRange(int min, int max) throws Exception { * @param list */ public LinkedList intersection(LinkedList list) { - if(0 == list.size){ - return this; - } - if(0 == size){ - return list; + if(0 == list.size || 0 == size){ + return new LinkedList(); } + LinkedList res = new LinkedList(); - Node a = head, b = list.head; - while(null != a && null != b){ - if(a.equals(b)){ - res.add(a.data); - a = a.next; - b = b.next; - continue; + Node node1 = this.head; + Node node2 = list.head; + while(node1 != null && node2 != null){ + if((int)node1.data<(int)node2.data){ + node1 = node1.next; + }else if((int)node1.data>(int)node2.data){ + node2 = node2.next; + }else{ + res.add(node1.data); + node1 = node1.next; + node2 = node2.next; } - if(Integer.parseInt(a.data.toString()) > Integer.parseInt(b.data.toString())){ - res.add(b.data); - b = b.next; - continue; - } - if(Integer.parseInt(a.data.toString()) < Integer.parseInt(b.data.toString())){ - res.add(a.data); - a = a.next; - continue; - } - } - while(null != a){ - res.add(a.data); - a = a.next; - } - while(null != b){ - res.add(b.data); - b = b.next; } return res; } - public String ToString() { + public String toString() { LinkedListIterator iter = this.iterator(); StringBuilder sb = new StringBuilder(); while (iter.hasNext()) { diff --git a/group24/330657387/src/main/week01/data_structure/LinkedListTest.java b/group24/330657387/src/main/week01/data_structure/LinkedListTest.java index 671cc20cd2..c0aa471f79 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedListTest.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedListTest.java @@ -71,14 +71,9 @@ public void testIterator() { public void testReverse() { LinkedList l = lists[2]; l.reverse(); - LinkedListIterator iter = l.iterator(); - StringBuilder sb = new StringBuilder(); - while (iter.hasNext()) { - sb.append(iter.next()); - } // assertEquals("", sb.toString()); - // assertEquals("A", sb.toString()); - assertEquals("EDCBA", sb.toString()); + // assertEquals("A", l.toString()); + assertEquals("E->D->C->B->A->null", l.toString()); } @@ -105,7 +100,7 @@ public void testRemoveByIndex() { try{ LinkedList l = lists[2]; l.remove(0, 1); - System.out.println(l.ToString()); + System.out.println(l.toString()); }catch(Exception e){ assertEquals(IndexOutOfBoundsException.class, e.getClass()); } @@ -155,7 +150,7 @@ public void testSubtract() { l.add(66); try{ list.subtract(l); - System.out.println(list.ToString()); + System.out.println(list.toString()); }catch(Exception e){ assertEquals(e.getMessage(), "传入链表为空?"); } @@ -171,7 +166,7 @@ public void testRemoveDuplicateValues() { list.removeDuplicateValues(); - System.out.println(list.ToString()); + System.out.println(list.toString()); } @Test @@ -185,11 +180,11 @@ public void testRemoveRange() throws Exception { list.add(77); list.add(88); list.add(99); - System.out.println(list.ToString()); + System.out.println(list.toString()); try{ list.removeRange(50, 80); - System.out.println(list.ToString()); + System.out.println(list.toString()); }catch(Exception e){ assertEquals(e.getMessage(), "输入有问题!"); } @@ -208,11 +203,11 @@ public void testIntersection() { // list.add(99); LinkedList l = new LinkedList(); - l.add(10); -// l.add(30); -// l.add(40); -// l.add(60); + l.add(11); + l.add(33); +// l.add(44); +// l.add(66); - System.out.println(list.intersection(l).ToString()); + System.out.println(list.intersection(l).toString()); } } diff --git a/group24/330657387/src/main/week03/download/DownloadThread.java b/group24/330657387/src/main/week03/download/DownloadThread.java index f18bbf234c..37602d1c25 100644 --- a/group24/330657387/src/main/week03/download/DownloadThread.java +++ b/group24/330657387/src/main/week03/download/DownloadThread.java @@ -1,5 +1,6 @@ package main.week03.download; +import java.io.File; import java.io.RandomAccessFile; import java.util.concurrent.CyclicBarrier; @@ -33,7 +34,7 @@ public void run() { + "]"); byte[] data = conn.read(startPos, endPos); - //设置文件的读取权限 + //设置文件的读取权限,每个线程都独立有这个实例,这样,多线程读写同一文件就没问题。 RandomAccessFile file = new RandomAccessFile(localFile, "rw"); file.seek(startPos); @@ -47,9 +48,9 @@ public void run() { barrier.await(); // 等待别的线程完成 } catch (Exception e) { + //如果线程出错了,无法await,怎么处理? e.printStackTrace(); - - } + } finally{}//这块里应该写close的 } } diff --git a/group24/330657387/src/main/week03/download/FileDownloader.java b/group24/330657387/src/main/week03/download/FileDownloader.java index 54d6c260ad..165dc4dfb2 100644 --- a/group24/330657387/src/main/week03/download/FileDownloader.java +++ b/group24/330657387/src/main/week03/download/FileDownloader.java @@ -58,7 +58,7 @@ public void run() { int length = conn.getContentLength(); - //确保文件里有足够的空间? + //确保文件里有足够的空间,就先创建空文件。 createPlaceHolderFile(this.filePath, length); //每个线程的读取区间 diff --git a/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java b/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java index e725ff15a1..e42087d663 100644 --- a/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java +++ b/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java @@ -12,7 +12,8 @@ import main.week03.download.api.Connection; import main.week03.download.api.ConnectionException; -public class ConnectionImpl implements Connection { +//包级可见,是保护措施 +class ConnectionImpl implements Connection { URL url; static final int BUFFER_SIZE = 1024; @@ -29,13 +30,15 @@ public ConnectionImpl(String _url) throws ConnectionException { @Override public byte[] read(int startPos, int endPos) throws IOException { int totalLen = endPos - startPos + 1; - + //是URLConnection的子类,负责http协议的链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - InputStream inputStream = conn.getInputStream(); + //客户端可以在请求里放置参数,设置接收数据区间 + //代替了is.skip(),但是is.skip里有read,所以是边读边移动下标的,和本程序意图相违背。 + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + byte[] buffer = new byte[BUFFER_SIZE]; //输出流 diff --git a/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java b/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java index 2253f4fa06..47d5dd22e1 100644 --- a/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java +++ b/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java @@ -4,6 +4,7 @@ import main.week03.download.api.ConnectionException; import main.week03.download.api.ConnectionManager; +//返回接口,是对实现的一种隐蔽 public class ConnectionManagerImpl implements ConnectionManager { @Override diff --git a/group24/330657387/src/main/week03/download/test.jpg b/group24/330657387/src/main/week03/download/test.jpg index 3a052212e5..a959a6ad20 100644 Binary files a/group24/330657387/src/main/week03/download/test.jpg and b/group24/330657387/src/main/week03/download/test.jpg differ diff --git a/group24/448641125/src/com/donaldy/basic/ArrayList.java b/group24/448641125/src/com/donaldy/basic/ArrayList.java index b4528176c1..859ebaf75f 100644 --- a/group24/448641125/src/com/donaldy/basic/ArrayList.java +++ b/group24/448641125/src/com/donaldy/basic/ArrayList.java @@ -27,7 +27,7 @@ public void add(int index, Object o){ private void ensureCupacity(int capacitySize){ if (capacitySize >= MAXNSIZE) - throw new RuntimeException(); + throw new RuntimeException("capacitySize : " + capacitySize); } public void clear() { diff --git a/group24/448641125/src/com/donaldy/basic/Stack.java b/group24/448641125/src/com/donaldy/basic/Stack.java index 22fcbbf4bd..ed500638d1 100644 --- a/group24/448641125/src/com/donaldy/basic/Stack.java +++ b/group24/448641125/src/com/donaldy/basic/Stack.java @@ -4,15 +4,15 @@ public class Stack { private ArrayList elementData = new ArrayList(); public void push(Object o) { - elementData.add(o); + this.elementData.add(o); } public Object pop() { - return elementData.remove(size() - 1); + return this.elementData.remove(size() - 1); } public Object peek() { - return elementData.get(size() - 1); + return this.elementData.get(size() - 1); } public boolean isEmpty() { @@ -20,6 +20,6 @@ public boolean isEmpty() { } public int size(){ - return elementData.size(); + return this.elementData.size(); } } diff --git a/group24/448641125/src/com/donaldy/basic/StackUtil.java b/group24/448641125/src/com/donaldy/basic/StackUtil.java new file mode 100644 index 0000000000..ca0014b34e --- /dev/null +++ b/group24/448641125/src/com/donaldy/basic/StackUtil.java @@ -0,0 +1,118 @@ +package com.donaldy.basic; + +import java.util.*; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + ArrayList arrayList = new ArrayList(); + while (!s.isEmpty()) { + Object element = s.pop(); + arrayList.add(element); + } + + for (int i = 0; i < arrayList.size(); ++i) { + s.push(arrayList.get(i)); + } + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + //若stack的值为唯一的。 + Stack stack = new Stack(); + while (!s.isEmpty()) { + Object element = s.pop(); + if (o == element) { + break; + } + stack.push(element); + } + + while (!stack.isEmpty()) { + Object element = stack.pop(); + s.push(element); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + if (len < 0 || len >= s.size()) + throw new IndexOutOfBoundsException("len : " + len); + + Object [] arr = new Object[len]; + + ArrayList arrayList = new ArrayList(); + + while (!s.isEmpty()) { + arrayList.add(s.pop()); + } + + for (int i = arrayList.size() - 1; i >= 0; --i) + s.push(arrayList.get(i)); + + for (int i = 0 ; i < len; ++i) + arr[i] = arrayList.get(i); + + return arr; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + char [] arr = s.toCharArray(); + Stack stack = new Stack(); + for (int i = 0; i < s.length(); ++i) { + if (arr[i] == '(' ) + stack.push(')'); + if ( arr[i] == '{' ) + stack.push('}'); + if ( arr[i] == '[') + stack.push(']'); + + if (arr[i] == ')' ) { + if (')' != (char)stack.peek()) + break; + stack.pop(); + } + + if (arr[i] == '}' ) { + if ('}' != (char)stack.peek()) + break; + stack.pop(); + } + + if (arr[i] == ']' ) { + if (']' != (char)stack.peek()) + break; + stack.pop(); + } + + } + + if (stack.isEmpty()) + return true; + + return false; + } + + +} diff --git a/group24/448641125/src/com/donaldy/basic/expr/InfixExpr.java b/group24/448641125/src/com/donaldy/basic/expr/InfixExpr.java new file mode 100644 index 0000000000..dfd901febb --- /dev/null +++ b/group24/448641125/src/com/donaldy/basic/expr/InfixExpr.java @@ -0,0 +1,140 @@ +package com.donaldy.basic.expr; + +import com.donaldy.basic.Stack; + +/** + * 针对最后一个用例,expr: 10 - 30 + 50; + * 负数,直接对后面的数进行取反(实际上计算机就是这样做的,组原有提。) + * 即:expr: 10 - 30 + 50 + * 处理后: 10 + -30 + 50 + */ +public class InfixExpr { + + String expr = null; + + Stack numStack = new Stack(); + Stack symbolStack = new Stack(); + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + if (!this.numStack.isEmpty()) + return (float) this.numStack.peek(); + + char [] arr = this.expr.toCharArray(); + + parseCharArray(arr); + + remainOperate(); + + return (float) this.numStack.peek(); + } + + private void parseCharArray(char [] arr) { + + for (int i = 0; i < arr.length; ) { + + i = parseNumberReturnIndex(i, arr); + + if (i >= arr.length) + break; + + i = parseSymbolReturnIndex(i, arr); + } + } + + private int parseNumberReturnIndex(int index, char [] arr) { + if (arr[index] <= '9' && arr[index] >= '0' ) { + + float value = arr[index ++] - '0'; + + while (index < arr.length && arr[index] <= '9' && arr[index] >= '0' ) { + value *= 10; + value += arr[index] - '0'; + index ++; + } + this.numStack.push(value); + } + + return index; + } + + private int parseSymbolReturnIndex(int index, char[] arr) { + + if ("+-*/".contains(arr[index] + "")) { + + char operator = arr[index ++]; + + if (operator == '+') { + this.symbolStack.push('+'); + } + + if (operator == '-') { + + this.symbolStack.push('+'); + + float value = arr[index ++] - '0'; + + while (index < arr.length && arr[index] <= '9' && arr[index] >= '0') { + value *= 10; + value += arr[index] - '0'; + index ++; + } + + this.numStack.push(-value); + } + + if (operator == '*' || operator == '/') { + + float value1 = (float) this.numStack.pop(); + float value2 = arr[index ++] - '0'; + + while (index < arr.length && arr[index] <= '9' && arr[index] >= '0') { + value2 *= 10; + value2 += arr[index] - '0'; + index ++; + } + + this.numStack.push(operate(value2, value1, operator)); + + } + + } + + return index; + } + + private void remainOperate() { + while (!this.symbolStack.isEmpty()) { + if (this.numStack.size() < 2 || this.symbolStack.size() < 1) + throw new IndexOutOfBoundsException("numStack.size : " + this.numStack.size() + + " symbolStack.size : " + this.symbolStack.size()); + + float value1 = (float) this.numStack.pop(); + float value2 = (float) this.numStack.pop(); + char cSymbol = (char) this.symbolStack.pop(); + + this.numStack.push(operate(value1, value2, cSymbol)); + } + } + + + private float operate (float value1, float value2, char operator) { + + if (operator == '+') { + return value2 + value1; + } else if (operator == '*') { + return value2 * value1; + } else if (operator == '/') { + return value2 / value1; + } else { + throw new RuntimeException("No this operator : " + operator); + } + + } + + +} diff --git a/group24/448641125/src/com/donaldy/basic/expr/InfixExprTest.java b/group24/448641125/src/com/donaldy/basic/expr/InfixExprTest.java new file mode 100644 index 0000000000..47afcec569 --- /dev/null +++ b/group24/448641125/src/com/donaldy/basic/expr/InfixExprTest.java @@ -0,0 +1,48 @@ +package com.donaldy.basic.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/group24/448641125/src/com/donaldy/jvm/attr/AttributeInfo.java b/group24/448641125/src/com/donaldy/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..db2cdc8f97 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/attr/AttributeInfo.java @@ -0,0 +1,19 @@ +package com.donaldy.jvm.attr; + +public abstract class AttributeInfo { + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + int attrNameIndex; + int attrLen ; + public AttributeInfo(int attrNameIndex, int attrLen) { + + this.attrNameIndex = attrNameIndex; + this.attrLen = attrLen; + } + + +} diff --git a/group24/448641125/src/com/donaldy/jvm/attr/CodeAttr.java b/group24/448641125/src/com/donaldy/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..6b699e6991 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/attr/CodeAttr.java @@ -0,0 +1,56 @@ +package com.donaldy.jvm.attr; + +import com.donaldy.jvm.clz.ClassFile; +import com.donaldy.jvm.constant.ConstantPool; +import com.donaldy.jvm.loader.ByteCodeIterator; + + +public class CodeAttr extends AttributeInfo { + private int maxStack ; + private int maxLocals ; + private int codeLen ; + private String code; + public String getCode() { + return code; + } + + //private ByteCodeCommand[] cmds ; + //public ByteCodeCommand[] getCmds() { + // return cmds; + //} + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + + public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code /*ByteCodeCommand[] cmds*/) { + super(attrNameIndex, attrLen); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.codeLen = codeLen; + this.code = code; + //this.cmds = cmds; + } + + public void setLineNumberTable(LineNumberTable t) { + this.lineNumTable = t; + } + + public void setLocalVariableTable(LocalVariableTable t) { + this.localVarTable = t; + } + + public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ + + + return null; + } + private void setStackMapTable(StackMapTable t) { + this.stackMapTable = t; + + } + + + + + +} diff --git a/group24/448641125/src/com/donaldy/jvm/attr/LineNumberTable.java b/group24/448641125/src/com/donaldy/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..9dc9ab1dc9 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/attr/LineNumberTable.java @@ -0,0 +1,57 @@ +package com.donaldy.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.donaldy.jvm.loader.ByteCodeIterator; + +public class LineNumberTable extends AttributeInfo { + List items = new ArrayList(); + + private static class LineNumberItem{ + int startPC; + int lineNum; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + public void addLineNumberItem(LineNumberItem item){ + this.items.add(item); + } + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + + } + + public static LineNumberTable parse(ByteCodeIterator iter){ + int attrNameIndex = iter.nextU2ToInt(); + int attributeLen = iter.nextU4ToInt(); + int lnTableLen = iter.nextU2ToInt(); + LineNumberTable lnTable = new LineNumberTable(attrNameIndex, attributeLen); + + System.out.println("LineNumberTable.lnTableLen : " + lnTableLen); + + for (int i = 0; i < lnTableLen; ++i) { + LineNumberItem lnItem = new LineNumberItem(); + + lnItem.setStartPC(iter.nextU2ToInt()); + lnItem.setLineNum(iter.nextU2ToInt()); + + lnTable.addLineNumberItem(lnItem); + } + + return lnTable; + } + + + +} diff --git a/group24/448641125/src/com/donaldy/jvm/attr/LocalVariableItem.java b/group24/448641125/src/com/donaldy/jvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..7f04ebbc22 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/attr/LocalVariableItem.java @@ -0,0 +1,39 @@ +package com.donaldy.jvm.attr; + +public class LocalVariableItem { + private int startPC; + private int length; + private int nameIndex; + private int descIndex; + private int index; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getNameIndex() { + return nameIndex; + } + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + public int getDescIndex() { + return descIndex; + } + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/attr/LocalVariableTable.java b/group24/448641125/src/com/donaldy/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..c207540570 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/attr/LocalVariableTable.java @@ -0,0 +1,45 @@ +package com.donaldy.jvm.attr; + + +import java.util.ArrayList; +import java.util.List; + +import com.donaldy.jvm.constant.ConstantPool; + +import com.donaldy.jvm.loader.ByteCodeIterator; + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LocalVariableTable parse(ByteCodeIterator iter){ + int attrNameIndex = iter.nextU2ToInt(); + int attrLen = iter.nextU4ToInt(); + int localVariableTableLen = iter.nextU2ToInt(); + + LocalVariableTable lvTable = new LocalVariableTable(attrNameIndex, attrLen); + + for (int i = 0 ; i < localVariableTableLen; ++i) { + LocalVariableItem lvItem = new LocalVariableItem(); + lvItem.setStartPC(iter.nextU2ToInt()); + lvItem.setLength(iter.nextU2ToInt()); + lvItem.setNameIndex(iter.nextU2ToInt()); + lvItem.setDescIndex(iter.nextU2ToInt()); + lvItem.setIndex(iter.nextU2ToInt()); + + lvTable.addLocalVariableItem(lvItem); + } + + + return lvTable; + } + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + + +} diff --git a/group24/448641125/src/com/donaldy/jvm/attr/StackMapTable.java b/group24/448641125/src/com/donaldy/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..41365dc049 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/attr/StackMapTable.java @@ -0,0 +1,30 @@ +package com.donaldy.jvm.attr; + + +import com.donaldy.jvm.loader.ByteCodeIterator; + +public class StackMapTable extends AttributeInfo{ + + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.nextU2ToInt(); + int len = iter.nextU4ToInt(); + StackMapTable t = new StackMapTable(index,len); + + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + String code = iter.nextUxToHexString(len); + t.setOriginalCode(code); + + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/clz/AccessFlag.java b/group24/448641125/src/com/donaldy/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..ea06ad07cb --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package com.donaldy.jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + +} \ No newline at end of file diff --git a/group24/448641125/src/com/donaldy/jvm/clz/ClassFile.java b/group24/448641125/src/com/donaldy/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..c36e1940b1 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/clz/ClassFile.java @@ -0,0 +1,92 @@ +package com.donaldy.jvm.clz; + +import com.donaldy.jvm.constant.ClassInfo; +import com.donaldy.jvm.constant.ConstantPool; +import com.donaldy.jvm.field.Field; +import com.donaldy.jvm.method.Method; + +import java.util.ArrayList; +import java.util.List; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + private List fields = new ArrayList(); + private List methods = new ArrayList(); + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public void addField(Field f){ + this.fields.add(f); + } + public List getFields(){ + return this.fields; + } + public void addMethod(Method m){ + this.methods.add(m); + } + public List getMethods() { + return methods; + } + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + + System.out.println("Super Class Name:"+ getSuperClassName()); + + + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/clz/ClassIndex.java b/group24/448641125/src/com/donaldy/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..d7e0524060 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package com.donaldy.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/group24/448641125/src/com/donaldy/jvm/constant/ClassInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..71f31261e4 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/ClassInfo.java @@ -0,0 +1,24 @@ +package com.donaldy.jvm.constant; + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/ConstantInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..20cad6fcd5 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.donaldy.jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/ConstantPool.java b/group24/448641125/src/com/donaldy/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..77bdfc8e67 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/ConstantPool.java @@ -0,0 +1,29 @@ +package com.donaldy.jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/FieldRefInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..6a6f72f5da --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.donaldy.jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/MethodRefInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..e65135279f --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.donaldy.jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/NameAndTypeInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..21ecd2b411 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.donaldy.jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/NullConstantInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..3a2cc11017 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.donaldy.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/StringInfo.java b/group24/448641125/src/com/donaldy/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..f59d34a497 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.donaldy.jvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/group24/448641125/src/com/donaldy/jvm/constant/UTF8Info.java b/group24/448641125/src/com/donaldy/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..7432e5b6c4 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.donaldy.jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/group24/448641125/src/com/donaldy/jvm/field/Field.java b/group24/448641125/src/com/donaldy/jvm/field/Field.java new file mode 100644 index 0000000000..18cad5669a --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/field/Field.java @@ -0,0 +1,46 @@ +package com.donaldy.jvm.field; + +import com.donaldy.jvm.constant.ConstantPool; +import com.donaldy.jvm.constant.UTF8Info; +import com.donaldy.jvm.loader.ByteCodeIterator; + + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + private ConstantPool pool; + + + public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + + + public static Field parse(ConstantPool pool,ByteCodeIterator iter){ + + int accessFlag = iter.nextU2ToInt(); + int nameIndex = iter.nextU2ToInt(); + int descriptorIndex = iter.nextU2ToInt(); + + //TODO : 因无static类型变量,所以这无 + int attributesCount = iter.nextU2ToInt(); + + Field file = new Field(accessFlag, nameIndex, descriptorIndex, pool); + + return file; + } + + public String toString() { + //System.out.println("name : " + this.nameIndex + ", desc : " + descriptorIndex); + String description = this.pool.getUTF8String(this.descriptorIndex); + String name = this.pool.getUTF8String(this.nameIndex); + return name + ":"+ description; + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/loader/ByteCodeIterator.java b/group24/448641125/src/com/donaldy/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..82eae449b9 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,104 @@ +package com.donaldy.jvm.loader; + +import com.donaldy.jvm.util.Util; + +import java.util.Arrays; + +public class ByteCodeIterator { + + byte[] codes; + int pos = 0; + + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1toInt() { + + return Util.byteToInt(new byte[] { codes[pos++] }); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++] }); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] }); + } + + public String nextU4ToHexString() { + return Util.byteToHexString((new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] })); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + + } + + public void back(int n) { + this.pos -= n; + } + + ///////////////////////Backup////////////////// + /*private byte[] codes; + + private int pointer = 0; + + public ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + public String nextU4ToHexString() { + byte [] byteCodes = nextLenByte(4); + + return Util.byteToHexString(byteCodes); + } + + public int nextU2ToInt() { + byte [] byteCodes = nextLenByte(2); + + return Util.byteToInt(byteCodes); + } + + public int nextU1toInt() { + byte [] byteCodes = nextLenByte(1); + + return Util.byteToInt(byteCodes); + } + + public byte[] getBytes(int len) { + byte [] byteCodes = nextLenByte(len); + + return byteCodes; + } + + private byte[] nextLenByte(int len) { + if (this.pointer + len >= this.codes.length) + throw new IndexOutOfBoundsException("codes.length : " + this.codes.length); + + byte [] byteCodes = new byte[len]; + + for (int i = 0 ; i < len; ++i) { + byteCodes[i] = this.codes[pointer ++]; + } + + return byteCodes; + }*/ +} diff --git a/group24/448641125/src/com/donaldy/jvm/loader/ClassFileLoader.java b/group24/448641125/src/com/donaldy/jvm/loader/ClassFileLoader.java index c4a64b26d6..e1132dd023 100644 --- a/group24/448641125/src/com/donaldy/jvm/loader/ClassFileLoader.java +++ b/group24/448641125/src/com/donaldy/jvm/loader/ClassFileLoader.java @@ -2,7 +2,10 @@ import java.io.*; import java.util.ArrayList; -import java.util.Iterator; + +import com.donaldy.jvm.clz.ClassFile; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import java.util.List; @@ -12,15 +15,69 @@ public class ClassFileLoader { private List clzPaths = new ArrayList(); static final int BUFFER_SIZE = 1024; - + public byte[] readBinaryCode(String className) { + className = className.replace(".", File.separator) + ".class"; + + for (String path : this.clzPaths) { + + String clzFileName = path + File.separator + className; + byte [] codes = loadClassFile(clzFileName); + + if (codes != null) { + return codes; + } + } + + return null; + } + + private byte[] loadClassFile(String clzFileName) { + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + public void addClassPath(String path) { + if (this.clzPaths.contains(path)) + return; + + this.clzPaths.add(path); + } + + public String getClassPath() { + return StringUtils.join(this.clzPaths, ";"); + } + + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + + } + + + + + + ////////////////////////////////Backup/////////////////////////////// + public byte[] readBinaryCode_V1(String className) { + for (String clzPath : clzPaths) { File file = new File(clzPath + className.replace(".", "\\") + ".class"); - + if (!file.exists()) continue; @@ -52,24 +109,17 @@ public byte[] readBinaryCode(String className) { } - - public void addClassPath(String path) { - if (path == null) - return; - - clzPaths.add(path); - } - - - - public String getClassPath(){ + public String getClassPath_V1(){ StringBuilder sb = new StringBuilder(); - int length = clzPaths.size(); + + int length = this.clzPaths.size(); + for (int i = 0 ; i < length; ++i) { - sb.append(clzPaths.get(i)); + sb.append(this.clzPaths.get(i)); if (i + 1 < length) sb.append(";"); } + return sb.toString(); } diff --git a/group24/448641125/src/com/donaldy/jvm/loader/ClassFileParser.java b/group24/448641125/src/com/donaldy/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..b0229c7435 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/loader/ClassFileParser.java @@ -0,0 +1,166 @@ +package com.donaldy.jvm.loader; + +import com.donaldy.jvm.clz.AccessFlag; +import com.donaldy.jvm.clz.ClassFile; +import com.donaldy.jvm.clz.ClassIndex; +import com.donaldy.jvm.constant.*; +import com.donaldy.jvm.field.Field; +import com.donaldy.jvm.method.Method; + +import java.io.UnsupportedEncodingException; + + +public class ClassFileParser { + + public ClassFile parse(byte[] codes) { + + ByteCodeIterator iter = new ByteCodeIterator(codes); + ClassFile clzFile = new ClassFile(); + + String magicNumber = iter.nextU4ToHexString(); + + if (!"cafebabe".equals(magicNumber)) { + return null; + } + + clzFile.setMinorVersion(iter.nextU2ToInt()); + clzFile.setMajorVersion(iter.nextU2ToInt()); + + ConstantPool pool = parseConstantPool(iter); + clzFile.setConstPool(pool); + + AccessFlag flag = parseAccessFlag(iter); + clzFile.setAccessFlag(flag); + + ClassIndex clzIndex = parseClassIndex(iter); + clzFile.setClassIndex(clzIndex); + + ////////////Third times JVM homework//////////// + parseInterfaces(iter); //本次作业无interface + + parseFileds(clzFile, iter); + + parseMethods(clzFile, iter); + + return clzFile; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + AccessFlag flag = new AccessFlag(iter.nextU2ToInt()); + return flag; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + + int thisClassIndex = iter.nextU2ToInt(); + + int superClassIndex = iter.nextU2ToInt(); + + ClassIndex clzIndex = new ClassIndex(); + + clzIndex.setThisClassIndex(thisClassIndex); + + clzIndex.setSuperClassIndex(superClassIndex); + + return clzIndex; + + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + + int constPoolCount = iter.nextU2ToInt(); + + System.out.println("Constant Pool Count : " + constPoolCount); + + ConstantPool pool = new ConstantPool(); + + pool.addConstantInfo(new NullConstantInfo()); + + for (int i = 1; i <= constPoolCount - 1; ++i) { + + int tag = iter.nextU1toInt(); + + if (tag == 7) { + //Class Info + int utf8Index = iter.nextU2ToInt(); + ClassInfo clzInfo = new ClassInfo(pool); + clzInfo.setUtf8Index(utf8Index); + + pool.addConstantInfo(clzInfo); + } else if (tag == 1) { + //UTF-8 String + int len = iter.nextU2ToInt(); + byte [] data = iter.getBytes(len); + String value = null; + try { + value = new String(data, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + UTF8Info utf8Info = new UTF8Info(pool); + utf8Info.setLength(len); + utf8Info.setValue(value); + pool.addConstantInfo(utf8Info); + } else if (tag == 8) { + StringInfo info = new StringInfo(pool); + info.setIndex(iter.nextU2ToInt()); + pool.addConstantInfo(info); + } else if (tag == 9) { + FieldRefInfo field = new FieldRefInfo(pool); + field.setClassInfoIndex(iter.nextU2ToInt()); + field.setNameAndTypeIndex(iter.nextU2ToInt()); + pool.addConstantInfo(field); + + } else if (tag == 10) { + //MethodRef + MethodRefInfo method = new MethodRefInfo(pool); + method.setClassInfoIndex(iter.nextU2ToInt()); + method.setNameAndTypeIndex(iter.nextU2ToInt()); + pool.addConstantInfo(method); + } else if (tag == 12) { + // Name and Type Info + NameAndTypeInfo nameType = new NameAndTypeInfo(pool); + nameType.setIndex1(iter.nextU2ToInt()); + nameType.setIndex2(iter.nextU2ToInt()); + pool.addConstantInfo(nameType); + } else { + throw new RuntimeException("the constant pool tag" + tag); + } + } + + return pool; + } + + private void parseInterfaces(ByteCodeIterator iter) { + int interfaceCount = iter.nextU2ToInt(); + + System.out.println("interfaceCount:" + interfaceCount); + + // TODO : 如果实现了interface, 这里需要解析 + } + + private void parseFileds(ClassFile clzFile, ByteCodeIterator iter) { + int fieldCount = iter.nextU2ToInt(); + + //System.out.println("fileCount : " + fieldCount); + + for (int i = 1; i <= fieldCount; i++) { + Field f = Field.parse(clzFile.getConstantPool(), iter); + clzFile.addField(f); + } + + } + + private void parseMethods(ClassFile clzFile, ByteCodeIterator iter) { + + int methodCount = iter.nextU2ToInt(); + + for (int i = 1; i <= methodCount; i++) { + Method m = Method.parse(clzFile, iter); + clzFile.addMethod(m); + } + + } + +} diff --git a/group24/448641125/src/com/donaldy/jvm/method/Method.java b/group24/448641125/src/com/donaldy/jvm/method/Method.java new file mode 100644 index 0000000000..a69ff696b8 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/method/Method.java @@ -0,0 +1,89 @@ +package com.donaldy.jvm.method; + +import com.donaldy.jvm.attr.*; +import com.donaldy.jvm.clz.ClassFile; +import com.donaldy.jvm.constant.ConstantPool; +import com.donaldy.jvm.constant.UTF8Info; +import com.donaldy.jvm.loader.ByteCodeIterator; + + + +public class Method { + + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + + private ClassFile clzFile; + + + public ClassFile getClzFile() { + return clzFile; + } + + public int getNameIndex() { + return nameIndex; + } + public int getDescriptorIndex() { + return descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr code) { + this.codeAttr = code; + } + + public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { + this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + + public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ + + int accessFlag = iter.nextU2ToInt(); + int nameIndex = iter.nextU2ToInt(); + int descriptorIndex = iter.nextU2ToInt(); + + int attributeCount = iter.nextU2ToInt(); + System.out.println("attributeCount : " + attributeCount); + + int attrNameIndex = iter.nextU2ToInt(); + if (!"Code".equals(clzFile.getConstantPool().getUTF8String(attrNameIndex))) + throw new RuntimeException("attributeInfo : " + attrNameIndex); + + int attrLen = iter.nextU4ToInt(); + int maxStack = iter.nextU2ToInt(); + int maxLocals = iter.nextU2ToInt(); + int codeLen = iter.nextU4ToInt(); + String code = iter.nextUxToHexString(codeLen); + + CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code); + + int exceptionLen = iter.nextU2ToInt(); + System.out.println("execptionLen : " + exceptionLen); + + int attributesCount = iter.nextU2ToInt(); + System.out.println("attributeCount : " + attributesCount); + + LineNumberTable lnTable = LineNumberTable.parse(iter); + codeAttr.setLineNumberTable(lnTable); + + LocalVariableTable lvTable = LocalVariableTable.parse(iter); + codeAttr.setLocalVariableTable(lvTable); + + + Method method = new Method(clzFile, accessFlag, nameIndex, descriptorIndex); + + method.setCodeAttr(codeAttr); + return method; + + } +} diff --git a/group24/448641125/src/com/donaldy/jvm/test/ClassFileloaderTest.java b/group24/448641125/src/com/donaldy/jvm/test/ClassFileloaderTest.java index 2ed50aa8b5..8610fda4c9 100644 --- a/group24/448641125/src/com/donaldy/jvm/test/ClassFileloaderTest.java +++ b/group24/448641125/src/com/donaldy/jvm/test/ClassFileloaderTest.java @@ -1,5 +1,10 @@ package com.donaldy.jvm.test; +import com.donaldy.jvm.clz.ClassFile; +import com.donaldy.jvm.clz.ClassIndex; +import com.donaldy.jvm.constant.*; +import com.donaldy.jvm.field.Field; +import com.donaldy.jvm.method.Method; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -7,17 +12,17 @@ import com.donaldy.jvm.loader.ClassFileLoader; - - +import java.util.List; public class ClassFileloaderTest { - + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + static String path1 = "D:\\tools\\Code\\Y_Repository\\coding2017\\group24\\448641125\\out\\production\\448641125\\"; static String path2 = "C:\\temp"; - - + + static ClassFile clzFile = null; @Before public void setUp() throws Exception { @@ -69,10 +74,7 @@ public void testMagicNumber(){ Assert.assertEquals("cafebabe", acctualValue); } - - - - + private String byteToHexString(byte[] codes ){ @@ -89,4 +91,189 @@ private String byteToHexString(byte[] codes ){ return buffer.toString(); } + + /** + * ---------------------------------------------------------------------- + */ + + + @Test + public void testVersion(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(this.path1); + clzFile = loader.loadClass("com.donaldy.jvm.test.EmployeeV1"); + + Assert.assertEquals(0, clzFile.getMinorVersion()); + Assert.assertEquals(52, clzFile.getMajorVersion()); + + } + + @Test + public void testConstantPool(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(this.path1); + clzFile = loader.loadClass("com.donaldy.jvm.test.EmployeeV1"); + + ConstantPool pool = clzFile.getConstantPool(); + + Assert.assertEquals(53, pool.getSize()); + + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); + Assert.assertEquals(2, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); + } + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); + Assert.assertEquals(4, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); + Assert.assertEquals("java/lang/Object", utf8Info.getValue()); + } + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); + Assert.assertEquals("name", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(6); + Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(7); + Assert.assertEquals("age", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(8); + Assert.assertEquals("I", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(9); + Assert.assertEquals("", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(this.path1); + clzFile = loader.loadClass("com.donaldy.jvm.test.EmployeeV1"); + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + /** + * 下面是第三次JVM课应实现的测试用例 + */ + @Test + public void testReadFields(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(this.path1); + clzFile = loader.loadClass("com.donaldy.jvm.test.EmployeeV1"); + + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + @Test + public void testMethods(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(this.path1); + clzFile = loader.loadClass("com.donaldy.jvm.test.EmployeeV1"); + + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab7000c2a2bb5000f2a1cb50011b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb5000fb1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50011b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b2001c1222b60024b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb000159122b101db7002d4c2bb6002fb1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } + } diff --git a/group24/448641125/src/com/donaldy/jvm/util/Util.java b/group24/448641125/src/com/donaldy/jvm/util/Util.java new file mode 100644 index 0000000000..f316117565 --- /dev/null +++ b/group24/448641125/src/com/donaldy/jvm/util/Util.java @@ -0,0 +1,24 @@ +package com.donaldy.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i= 0 ; --i) + stack.push(intArr[i]); + StackUtil.reverse(stack); + for (int i = 4 ; i >= 0; --i) { + Assert.assertEquals((int)stack.pop(), (int)intArr[i]); + } + } + + @Test + public void testRemove() { + Stack stack = new Stack(); + Integer [] intArr = {5, 4, 3, 2, 1}; + for (int i = 4; i >= 0 ; --i) + stack.push(intArr[i]); + + StackUtil.remove(stack, 2); + for (int i = 0; i < 5; ++i) { + if (i == 3) + continue; + System.out.println("stack: " + stack.peek() + " i : " + (int) intArr[i]); + Assert.assertEquals((int)stack.pop(), (int)intArr[i]); + } + } + + @Test + public void testGetTop() { + Stack stack = new Stack(); + Integer [] intArr = {5, 4, 3, 2, 1}; + for (int i = 4; i >= 0 ; --i) + stack.push(intArr[i]); + + int len = 3; + Object [] arr = StackUtil.getTop(stack, len); + + for (int i = 0 ; i < arr.length ; ++i) { + Assert.assertEquals((int)arr[i], (int)intArr[i]); + } + + for (int i = 0; i < 5; ++i) { + Assert.assertEquals((int)intArr[i], (int)stack.pop()); + } + } + + @Test + public void testIsValidPairs() { + + String str1 = "([e{d}f])"; + Assert.assertEquals(true, StackUtil.isValidPairs(str1)); + + String str2 = "([b{x]y})"; + Assert.assertEquals(false, StackUtil.isValidPairs(str2)); + + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/AccessFlag.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..0dedb245ee --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + +} \ No newline at end of file diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/ClassFile.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..0d2525e416 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/ClassFile.java @@ -0,0 +1,76 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.clz; + + +import com.coding.mini_jvm.src.com.coderising.jvm.constant.ClassInfo; +import com.coding.mini_jvm.src.com.coderising.jvm.constant.ConstantPool; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + + System.out.println("Super Class Name:"+ getSuperClassName()); + + + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/ClassIndex.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..4dc110fe5f --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ClassInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..227bb010a1 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ClassInfo.java @@ -0,0 +1,24 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ConstantInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..e7c849ca59 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ConstantPool.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..d4210647be --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/ConstantPool.java @@ -0,0 +1,36 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } + + @Override + public String toString() { + return "ConstantPool{" + + "constantInfos=" + constantInfos + + '}'; + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/FieldRefInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..1808a35c65 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/MethodRefInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..dc62d438e6 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/NameAndTypeInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..b8b9da7353 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/NullConstantInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..bf19f681d8 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/StringInfo.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..05a4afad6a --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/UTF8Info.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..61f20ed8f1 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..27d0c63af0 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,52 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.loader; + +import com.coding.mini_jvm.src.com.coderising.jvm.util.Util; + +public class ByteCodeIterator { + private static final int U1 = 1; + private static final int U2 = 2; + private static final int U4 = 4; + private static final int U8 = 8; + private byte[] bytes; + private int cursor; + + public ByteCodeIterator(byte[] bytes) { + this.bytes = bytes; + } + + public String readTwoBytesToString() { + String ret = Util.byte2String(bytes, cursor, U2); + cursor += U2; + return ret; + } + + public String readBytesToString(int len) { + String ret = Util.byte2String(bytes, cursor, len); + cursor += len; + return ret; + } + + + public int readTwoBytesToInt() { + int ret = Util.bytes2Int(bytes, cursor, U2); + cursor += U2; + return ret; + } + + public int readByteToInt() { + int ret = Util.bytes2Int(bytes, cursor, U1); + cursor += U1; + return ret; + } + + + + + public int skip(int len) { + if (cursor + len < 0 || cursor + len > bytes.length - 1) { + throw new IndexOutOfBoundsException(); + } + cursor += len; + return cursor; + } +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileLoader.java index 1ca7cc8ece..452f6bce7d 100644 --- a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileLoader.java +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -1,6 +1,11 @@ package com.coding.mini_jvm.src.com.coderising.jvm.loader; -import java.io.*; +import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassFile; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -14,29 +19,35 @@ public class ClassFileLoader { public byte[] readBinaryCode(String className) { String classPath = getClassPath(); String[] paths = classPath.split(File.pathSeparator); - className = className.replaceAll("\\.", "\\"+File.separator) ; + className = className.replace('.', File.separatorChar) ; for (String path : paths) { - String filename = path + File.separator + className + CLASS_FILE_SUFFIX; - File file = new File(filename); - if (file.exists()) { - try { - FileInputStream fis = new FileInputStream(file); - BufferedInputStream bis = new BufferedInputStream(fis); - byte[] data = new byte[bis.available()]; - bis.read(data); - return data; - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } + String clzFilename = path + File.separator + className + CLASS_FILE_SUFFIX; + byte[] data = loadClassFile(clzFilename); + if (data != null) { + return data; } } return null; } - + + private byte[] loadClassFile(String clzFileName) { + File file = new File(clzFileName); + BufferedInputStream bis = null; + try { + bis = new BufferedInputStream(new FileInputStream(file)); + byte[] data = new byte[bis.available()]; + bis.read(data); + return data; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } public void addClassPath(String path) { + if (this.clzPaths.contains(path)) { + return; + } clzPaths.add(path); } @@ -52,8 +63,10 @@ public String getClassPath(){ return path.substring(0, path.lastIndexOf(";")); } - - - + public ClassFile loadClass(String className) { + byte[] data = readBinaryCode(className); + ClassFileParser classFileParser = new ClassFileParser(); + return classFileParser.parse(data); + } } diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileParser.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..542968c809 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/loader/ClassFileParser.java @@ -0,0 +1,103 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.loader; + +import com.coding.mini_jvm.src.com.coderising.jvm.clz.AccessFlag; +import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassFile; +import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassIndex; +import com.coding.mini_jvm.src.com.coderising.jvm.constant.*; + + +public class ClassFileParser { + + public ClassFile parse(byte[] codes) { + ClassFile classFile = new ClassFile(); + ByteCodeIterator iterator = new ByteCodeIterator(codes); + //跳过魔数 + iterator.skip(4); + //次版本号 + classFile.setMinorVersion(iterator.readTwoBytesToInt()); + //主版本号 + classFile.setMajorVersion(iterator.readTwoBytesToInt()); + //解析常量池 + classFile.setConstPool(parseConstantPool(iterator)); + //访问限制符 + classFile.setAccessFlag(parseAccessFlag(iterator)); + //当前类/父类 + classFile.setClassIndex(parseClassIndex(iterator)); + return classFile; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + return new AccessFlag(iter.readTwoBytesToInt()); + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + ClassIndex classIndex = new ClassIndex(); + classIndex.setThisClassIndex(iter.readTwoBytesToInt()); + classIndex.setSuperClassIndex(iter.readTwoBytesToInt()); + return classIndex; + } + + private ConstantPool parseConstantPool(ByteCodeIterator iterator) { + ConstantPool constantPool = new ConstantPool(); + //读取常量个数 + int constantPoolCount = iterator.readTwoBytesToInt(); + constantPool.addConstantInfo(new NullConstantInfo()); + for (int i = 1; i < constantPoolCount; i++) { + int flag = iterator.readByteToInt(); + int utf8Index; + int clzIndex; + int nameAndTypeIndex; + switch (flag) { + case 1: + int length = iterator.readTwoBytesToInt(); + String val = iterator.readBytesToString(length); + UTF8Info utf8Info = new UTF8Info(constantPool); + utf8Info.setLength(length); + utf8Info.setValue(val); + constantPool.addConstantInfo(utf8Info); + break; + case 7: + utf8Index = iterator.readTwoBytesToInt(); + ClassInfo classInfo = new ClassInfo(constantPool); + classInfo.setUtf8Index(utf8Index); + constantPool.addConstantInfo(classInfo); + break; + case 8: + utf8Index = iterator.readTwoBytesToInt(); + StringInfo stringInfo = new StringInfo(constantPool); + stringInfo.setIndex(utf8Index); + constantPool.addConstantInfo(stringInfo); + break; + case 9: + clzIndex = iterator.readTwoBytesToInt(); + nameAndTypeIndex = iterator.readTwoBytesToInt(); + FieldRefInfo fieldRefInfo = new FieldRefInfo(constantPool); + fieldRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + fieldRefInfo.setClassInfoIndex(clzIndex); + constantPool.addConstantInfo(fieldRefInfo); + break; + case 10: + clzIndex = iterator.readTwoBytesToInt(); + nameAndTypeIndex = iterator.readTwoBytesToInt(); + MethodRefInfo methodRefInfo = new MethodRefInfo(constantPool); + methodRefInfo.setClassInfoIndex(clzIndex); + methodRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + constantPool.addConstantInfo(methodRefInfo); + break; + case 12: + utf8Index = iterator.readTwoBytesToInt(); + int utf8Index1 = iterator.readTwoBytesToInt(); + NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(constantPool); + nameAndTypeInfo.setIndex1(utf8Index); + nameAndTypeInfo.setIndex2(utf8Index1); + constantPool.addConstantInfo(nameAndTypeInfo); + break; + default: + throw new RuntimeException("flag "+ flag +" is not exists"); + } + } + return constantPool; + } + + +} diff --git a/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/util/Util.java b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/util/Util.java new file mode 100644 index 0000000000..8811e3541c --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/mini_jvm/src/com/coderising/jvm/util/Util.java @@ -0,0 +1,40 @@ +package com.coding.mini_jvm.src.com.coderising.jvm.util; + +public class Util { + + + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + public static int bytes2Int(byte[] b, int start, int len) { + int sum = 0; + int end = start + len; + for (int i = start; i < end; i++) { + int n = ((int) b[i]) & 0xff; + n <<= (--len) * 8; + sum = n + sum; + } + return sum; + } + + + public static String byte2String(byte[] b, int start, int len) { + return new String(b, start, len); + } + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i= 0; i--) { + sb.append(elementData.get(i).toString()); + sb.append(","); + } + return sb.substring(0, sb.lastIndexOf(",")); + } } diff --git a/group24/494800949/src/main/java/com/coding/week5/stack/StackUtil.java b/group24/494800949/src/main/java/com/coding/week5/stack/StackUtil.java new file mode 100644 index 0000000000..7b750b3549 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week5/stack/StackUtil.java @@ -0,0 +1,103 @@ +package com.coding.week5.stack; + +import com.coding.weak1.Stack; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + Stack stack = new Stack(); + Stack stack1 = new Stack(); + while (!s.isEmpty()) { + stack.push(s.pop()); + } + while (!stack.isEmpty()) { + stack1.push(stack.pop()); + } + while (!stack1.isEmpty()) { + s.push(stack1.pop()); + } + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + Stack stack = new Stack(); + while (!s.isEmpty()){ + Object o1 = s.pop(); + if (!o.equals(o1)) { + stack.push(o1); + } + } + while (!stack.isEmpty()) { + s.push(stack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + if (len > s.size() || len <= 0) { + throw new IllegalArgumentException(len+""); + } + + Object[] objects = new Object[len]; + for (int i = 0; i < len; i++) { + objects[i] = s.pop(); + } + for (int i = len - 1; i >= 0 ; i--) { + s.push(objects[i]); + } + return objects; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + Stack stack = new Stack(); + char[] chars = s.toCharArray(); + for (char c : chars) { + if (c == '(' || c == '{' || c == '[') { + stack.push(c); + } else if (c == ')' || c == '}' || c == ']'){ + if (stack.isEmpty()) { + return false; + } + if (!isPair((char)stack.pop(), c)){ + return false; + } + } + } + return stack.isEmpty(); + } + + private static boolean isPair(char left, char right) { + switch (left) { + case '{': + return right == '}'; + case '[': + return right == ']'; + case '(': + return right == ')'; + default: + return false; + } + } + +} diff --git a/group24/494800949/src/test/java/com/coding/mini_jvm/test/ClassFileloaderTest.java b/group24/494800949/src/test/java/com/coding/mini_jvm/test/ClassFileloaderTest.java index 830d0b7efe..a03f29c0c3 100644 --- a/group24/494800949/src/test/java/com/coding/mini_jvm/test/ClassFileloaderTest.java +++ b/group24/494800949/src/test/java/com/coding/mini_jvm/test/ClassFileloaderTest.java @@ -1,6 +1,10 @@ package com.coding.mini_jvm.test; +import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassFile; +import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassIndex; +import com.coding.mini_jvm.src.com.coderising.jvm.constant.*; import com.coding.mini_jvm.src.com.coderising.jvm.loader.ClassFileLoader; +import com.coding.mini_jvm.src.com.coderising.jvm.util.Util; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -16,9 +20,19 @@ public class ClassFileloaderTest { static String path1 = "H:\\sourceCode\\coding2017\\group24\\494800949\\build\\classes\\test"; static String path2 = "C:\temp"; - - - + + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coding.mini_jvm.test.EmployeeV1"; + + clzFile = loader.loadClass(className); +// clzFile.print(); + } + @Before public void setUp() throws Exception { } @@ -65,28 +79,95 @@ public void testMagicNumber(){ byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - String acctualValue = this.byteToHexString(codes); + String acctualValue = Util.byteToHexString(codes); Assert.assertEquals("cafebabe", acctualValue); } - - - - - - - private String byteToHexString(byte[] codes ){ - StringBuffer buffer = new StringBuffer(); - for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); } - return buffer.toString(); } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } } diff --git a/group24/494800949/src/test/java/com/coding/week5/stack/StackUtilTest.java b/group24/494800949/src/test/java/com/coding/week5/stack/StackUtilTest.java new file mode 100644 index 0000000000..8e14b7cf00 --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week5/stack/StackUtilTest.java @@ -0,0 +1,56 @@ +package com.coding.week5.stack; + +import com.coding.weak1.Stack; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Administrator on 2017/4/9 0009. + */ +public class StackUtilTest { + + private Stack stack; + + @Before + public void setup() { + stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + } + @Test + public void reverse() throws Exception { + Assert.assertEquals("4,3,2,1",stack.toString()); + StackUtil.reverse(stack); + Assert.assertEquals("1,2,3,4",stack.toString()); + } + + @Test + public void remove() throws Exception { + StackUtil.remove(stack, 3); + Assert.assertEquals(stack.toString(), "4,2,1"); + } + + @Test + public void getTop() throws Exception { + Object[] objects = StackUtil.getTop(stack, 2); + Assert.assertEquals(objects[0], 4); + Assert.assertEquals(objects[1], 3); + } + + @Test + public void isValidPairs() throws Exception { + String str = "[abdd]}"; + Assert.assertEquals(StackUtil.isValidPairs(str), false); + str = "{add{ad[ddd]}}"; + Assert.assertEquals(StackUtil.isValidPairs(str), true); + str = "{add{ad[d(dd]}}"; + Assert.assertEquals(StackUtil.isValidPairs(str), false); + str = "{add{ad[d(d)d]}dfd}"; + Assert.assertEquals(StackUtil.isValidPairs(str), true); + } + +} \ No newline at end of file diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/StackUtil.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/StackUtil.java new file mode 100644 index 0000000000..1e43329fcc --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/StackUtil.java @@ -0,0 +1,132 @@ +package com.github.wdn.coding2017.basic.stack; + +import com.github.wdn.coding2017.basic.Stack; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + Stack stack = new Stack(); + Stack stack1 = new Stack(); + while (!s.isEmpty()){ + stack.push(s.pop()); + } + while (!stack.isEmpty()){ + stack1.push(stack.pop()); + } + while (!stack1.isEmpty()){ + s.push(stack1.pop()); + } + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + Stack stack = new Stack(); + while (!s.isEmpty()) { + Object popObject = s.pop(); + if(popObject.equals(o)){ + break; + } + stack.push(popObject); + } + while (!stack.isEmpty()){ + s.push(stack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + if (len > s.size() || len < 0) { + throw new IndexOutOfBoundsException(); + } + Object[] result = new Object[len]; + Stack stack = new Stack(); + for (int i = 0; i < len; i++) { + Object o = s.pop(); + result[i]=o; + stack.push(o); + } + while (!stack.isEmpty()){ + s.push(stack.pop()); + } + return result; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + if(s.length()<2){ + return false; + } + Stack s1 = new Stack(); + Stack s2 = new Stack(); + char[] chars = s.toCharArray(); + int charsLength = chars.length; + if(charsLength%2==1 && isBrackets(chars[charsLength / 2])){ + return false; + } + for (int i = 0; i < charsLength/2; i++) { + char c = chars[i]; + if (isBrackets(c)) { + s1.push(c); + } + } + for (int i = charsLength-1; i > charsLength/2; i--) { + char c = chars[i]; + if (isBrackets(c)) { + s2.push(c); + } + } + if (s1.size() != s2.size()) { + return false; + } + for (int i = 0; i < s1.size(); i++) { + if (!isPairing((Character) s1.pop(), (Character) s2.pop())) { + return false; + } + } + return true; + } + // parenthesis 圆括号 + // square brackets 方括号 + // braces 大括号 + // 这里用bracket表示统称 + private static boolean isBrackets(char c){ + if('['==c||']'==c|| + '('==c||')'==c|| + '{'==c||'}'==c){ + return true; + } + return false; + } + + private static boolean isPairing(char left, char right) { + if(left=='(' && right==')'){ + return true; + }else if(left=='[' && right==']'){ + return true; + }else if(left=='{' && right=='}'){ + return true; + }else { + return false; + } + } +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/expr/InfixExpr.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..3a8f013b78 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/expr/InfixExpr.java @@ -0,0 +1,90 @@ +package com.github.wdn.coding2017.basic.stack.expr; + +import com.github.wdn.coding2017.basic.Stack; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Administrator on 2017/4/13 0013. + */ +public class InfixExpr { + private String expr; + private static Map priorityMap = new HashMap(); + static{ + priorityMap.put("+",1); + priorityMap.put("-",1); + priorityMap.put("*",2); + priorityMap.put("/",2); + } + public InfixExpr(String expr) { + this.expr = expr; + } + public float calculate(float a,float b,String operator) throws IllegalAccessException { + float result; + switch (operator) { + case "+": + result = a+b; + break; + case "-": + result = a-b; + break; + case "*": + result = a*b; + break; + case "/": + result = a/b; + break; + default: + throw new IllegalAccessException(); + } + return result; + } + public float evaluate() { + try { + String[] numArr = expr.split("[+|\\-|*|/]"); + String[] operatorArr = expr.split("\\d+\\d*"); + Object[] operators = Arrays.stream(operatorArr).filter(x -> !"".equals(x.trim())).toArray(); + Stack numStack = new Stack(); + Stack operatorStack = new Stack(); + numStack.push(numArr[0]); + for (int i = 0; i < operators.length; i++) { + int number = Integer.parseInt(numArr[i + 1]); + String operator = operators[i].toString(); + if (!operatorStack.isEmpty() && priorityMap.get(operatorStack.peek()) < priorityMap.get(operator)) { + float currentResult = calculate(Integer.parseInt(numStack.pop().toString()), number, operator); + numStack.push(currentResult); + } else if(!operatorStack.isEmpty() && priorityMap.get(operatorStack.peek()) >= priorityMap.get(operator)){ + float b = Float.parseFloat(numStack.pop().toString()); + float a = Float.parseFloat(numStack.pop().toString()); + String currentOperator = operatorStack.pop().toString(); + float result = calculate(a, b, currentOperator); + numStack.push(result); + numStack.push(number); + operatorStack.push(operator); + }else { + numStack.push(number); + operatorStack.push(operator); + } + } + while (!operatorStack.isEmpty()) { + float b = Float.parseFloat(numStack.pop().toString()); + float a = Float.parseFloat(numStack.pop().toString()); + String operator = operatorStack.pop().toString(); + float result = calculate(a, b, operator); + numStack.push(result); + } + return Float.parseFloat(numStack.pop().toString()); + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + + public static void main(String[] args) { + InfixExpr infixExpr = new InfixExpr("2+3*4+5"); + float r = infixExpr.evaluate(); + System.out.println(r); + } +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/expr/InfixExprTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..a05f9fd578 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,50 @@ +package com.github.wdn.coding2017.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Administrator on 2017/4/13 0013. + */ +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/group24/626451284/data-structure/src/main/test/com/github/wdn/coding2017/basic/StackUtilTest.java b/group24/626451284/data-structure/src/main/test/com/github/wdn/coding2017/basic/StackUtilTest.java new file mode 100644 index 0000000000..7e56bbf345 --- /dev/null +++ b/group24/626451284/data-structure/src/main/test/com/github/wdn/coding2017/basic/StackUtilTest.java @@ -0,0 +1,57 @@ +package com.github.wdn.coding2017.basic; + +import com.github.wdn.coding2017.basic.stack.StackUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public class StackUtilTest { + + @Test + public void testReverse(){ + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + StackUtil.reverse(stack); + while (!stack.isEmpty()){ + System.out.println(stack.pop()); + } + } + @Test + public void testRemove(){ + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + StackUtil.remove(stack,4); + while (!stack.isEmpty()){ + System.out.println(stack.pop()); + } + } + @Test + public void testGetTop(){ + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + Object[] o = StackUtil.getTop(stack,0); + System.out.println(Arrays.toString(o)); + while (!stack.isEmpty()){ + System.out.println(stack.pop()); + } + } + @Test + public void testIsValidPairs(){ + Assert.assertEquals(true,StackUtil.isValidPairs("([e{d}f])")); + Assert.assertEquals(false,StackUtil.isValidPairs("([b{x]y})")); + Assert.assertEquals(false,StackUtil.isValidPairs("([({e}f])")); + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/AttributeInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..8ec8464918 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/AttributeInfo.java @@ -0,0 +1,20 @@ +package com.github.wdn.coding2017.jvm.attr; + +/** + * Created by Administrator on 2017/4/12 0012. + */ +public class AttributeInfo { + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + + private int attributeNameIndex;// u2 + private int attributeLength; //u4 + public AttributeInfo(int attributeNameIndex,int attributeLength){ + this.attributeNameIndex = attributeNameIndex; + this.attributeLength = attributeLength; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/CodeAttr.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..9400b629b2 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/CodeAttr.java @@ -0,0 +1,58 @@ +package com.github.wdn.coding2017.jvm.attr; + +/** + * Created by Administrator on 2017/4/12 0012. + */ +public class CodeAttr extends AttributeInfo { + private int maxStack; + private int maxLocals; + private String code; + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + public CodeAttr(int attributeNameIndex, int attributeLength, int maxStack, int maxLocals, String code) { + super(attributeNameIndex, attributeLength); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.code = code; + } + @Override + public String toString(){ + return code; + } + public int getMaxStack() { + return maxStack; + } + + public void setMaxStack(int maxStack) { + this.maxStack = maxStack; + } + + public int getMaxLocals() { + return maxLocals; + } + + public void setMaxLocals(int maxLocals) { + this.maxLocals = maxLocals; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public void setLineNumTable(LineNumberTable lineNumTable) { + this.lineNumTable = lineNumTable; + } + + public void setLocalVarTable(LocalVariableTable localVarTable) { + this.localVarTable = localVarTable; + } + + public void setStackMapTable(StackMapTable stackMapTable) { + this.stackMapTable = stackMapTable; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LineNumberTable.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..594e574114 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LineNumberTable.java @@ -0,0 +1,48 @@ +package com.github.wdn.coding2017.jvm.attr; + +import com.github.wdn.coding2017.jvm.constant.ConstantPool; +import com.github.wdn.coding2017.jvm.loader.ByteCodeIterator; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/4/12 0012. + */ +public class LineNumberTable extends AttributeInfo{ + public LineNumberTable(int attributeNameIndex, int attributeLength) { + super(attributeNameIndex, attributeLength); + } + List lineNumberItems = new ArrayList<>(); + private static class LineNumberItem{ + int startPC; + int lineNum; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + public void addLineNumberItem(LineNumberItem item){ + this.lineNumberItems.add(item); + } + + public static LineNumberTable parse(ByteCodeIterator iter){ + LineNumberTable lineNumberTable = new LineNumberTable(0, iter.readU4ToInt()); + int lineNumberTableCount = iter.readU2ToInt(); + for (int l = 0; l < lineNumberTableCount; l++) { + LineNumberItem lineNumberItem = new LineNumberItem(); + lineNumberItem.setStartPC(iter.readU2ToInt()); + lineNumberItem.setLineNum(iter.readU2ToInt()); + lineNumberTable.addLineNumberItem(lineNumberItem); + } + return lineNumberTable; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LocalVariableTable.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..f84a96e2d9 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LocalVariableTable.java @@ -0,0 +1,80 @@ +package com.github.wdn.coding2017.jvm.attr; + +import com.github.wdn.coding2017.jvm.loader.ByteCodeIterator; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/4/12 0012. + */ +public class LocalVariableTable extends AttributeInfo{ + List localVariableTableItems = new ArrayList<>(); + public LocalVariableTable(int attributeNameIndex, int attributeLength) { + super(attributeNameIndex, attributeLength); + } + private static class LocalVariableTableItem{ + int startPC; + int length; + int nameIndex; + int descriptorIndex; + int index; + + public int getStartPC() { + return startPC; + } + + public void setStartPC(int startPC) { + this.startPC = startPC; + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getDescriptorIndex() { + return descriptorIndex; + } + + public void setDescriptorIndex(int descriptorIndex) { + this.descriptorIndex = descriptorIndex; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } + } + private void addLocalVariableItem(LocalVariableTableItem item){ + this.localVariableTableItems.add(item); + } + public static LocalVariableTable parse(ByteCodeIterator iter) { + LocalVariableTable localVariableTable = new LocalVariableTable(iter.readU2ToInt(),iter.readU2ToInt()); + int LocalVariableTableCount = iter.readU2ToInt(); + for (int l = 0; l < LocalVariableTableCount; l++) { + LocalVariableTableItem item = new LocalVariableTableItem(); + item.setStartPC(iter.readU2ToInt()); + item.setLength(iter.readU2ToInt()); + item.setNameIndex(iter.readU2ToInt()); + item.setDescriptorIndex(iter.readU2ToInt()); + item.setIndex(iter.readU2ToInt()); + localVariableTable.addLocalVariableItem(item); + } + return localVariableTable; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LocalVariableTypeTable.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LocalVariableTypeTable.java new file mode 100644 index 0000000000..cb05a8074d --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/LocalVariableTypeTable.java @@ -0,0 +1,7 @@ +package com.github.wdn.coding2017.jvm.attr; + +/** + * Created by Administrator on 2017/4/12 0012. + */ +public class LocalVariableTypeTable { +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/StackMapTable.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..7c607031be --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/attr/StackMapTable.java @@ -0,0 +1,29 @@ +package com.github.wdn.coding2017.jvm.attr; + +import com.github.wdn.coding2017.jvm.loader.ByteCodeIterator; + +/** + * Created by Administrator on 2017/4/12 0012. + */ +public class StackMapTable extends AttributeInfo{ + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.readU2ToInt(); + int len = iter.readU4ToInt(); + StackMapTable t = new StackMapTable(index,len); + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + String code = iter.readCustomToString(len); + t.setOriginalCode(code); + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/AccessFlag.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..a0fcba07f6 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/AccessFlag.java @@ -0,0 +1,37 @@ +package com.github.wdn.coding2017.jvm.clz; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public class AccessFlag { + /* + enum { + ACC_PUBLIC,ACC_FINAL,ACC_SUPER,ACC_INTERFACE,ACC_ABSTRACT,ACC_SYNTHETIC + } + private int ACC_PUBLIC =0x0001; //可以被包的类外访问。 + private int ACC_FINAL =0x0010; //不允许有子类。 + private int ACC_SUPER =0x0020;//当用到invokespecial指令时,需要特殊处理③的父类方法。 + private int ACC_INTERFACE= 0x0200; //标识定义的是接口而不是类。 + private int ACC_ABSTRACT= 0x0400; //不能被实例化。 + private int ACC_SYNTHETIC= 0x1000; //标识并非Java源码生成的代码 + */ + private int flagValue; + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/ClassFile.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..813c050a53 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/ClassFile.java @@ -0,0 +1,80 @@ +package com.github.wdn.coding2017.jvm.clz; + +import com.github.wdn.coding2017.jvm.constant.ConstantPool; + +import java.util.List; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public class ClassFile { + private int minorVersion; + private int majorVersion; + private ConstantPool constantPool; + private AccessFlag accessFlag; + private ClassIndex classIndex; + private List fields; + private List methods; + public void print() { + } + + public int getMinorVersion() { + return minorVersion; + } + + public int getMajorVersion() { + return majorVersion; + } + + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + + public ConstantPool getConstantPool() { + return constantPool; + } + + public void setConstantPool(ConstantPool constantPool) { + this.constantPool = constantPool; + } + + public AccessFlag getAccessFlag() { + return accessFlag; + } + + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + public ClassIndex getClassIndex() { + return classIndex; + } + + public void setClassIndex(ClassIndex classIndex) { + this.classIndex = classIndex; + } + + public ClassIndex getClzIndex() { + return null; + } + + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + } + + public List getMethods() { + return methods; + } + + public void setMethods(List methods) { + this.methods = methods; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/ClassIndex.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..f65a403d0a --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/ClassIndex.java @@ -0,0 +1,20 @@ +package com.github.wdn.coding2017.jvm.clz; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + public ClassIndex(int thisClassIndex,int superClassIndex){ + this.thisClassIndex = thisClassIndex; + this.superClassIndex = superClassIndex; + } + public int getThisClassIndex() { + return thisClassIndex; + } + + public int getSuperClassIndex() { + return superClassIndex; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/Field.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/Field.java new file mode 100644 index 0000000000..8d557b10de --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/Field.java @@ -0,0 +1,74 @@ +package com.github.wdn.coding2017.jvm.clz; + +import com.github.wdn.coding2017.jvm.constant.ConstantPool; +import com.github.wdn.coding2017.jvm.loader.ByteCodeIterator; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/4/10 0010. + */ +public class Field { + private AccessFlag accessFlag; // 例如是public , private 等等 + private int nameIndex; // 指向常量池的入口 + private int descriptorIndex; //指向常量池的入口 + private int attributesCount; // 该字段的属性有多少个 + private ConstantPool pool; + // attribute_info attributes[attributes_count]; //属性信息 + private Field(){ + } + public Field(ConstantPool pool){ + this.pool = pool; + } + public static Field parse(ByteCodeIterator iter){ + Field field = new Field(); + field.setAccessFlags(new AccessFlag(iter.readU2ToInt())); + field.setNameIndex(iter.readU2ToInt()); + field.setDescriptorIndex(iter.readU2ToInt()); + int attCount = iter.readU2ToInt(); + if(attCount>0){ + throw new RuntimeException("字段属性数量大于0"); + } + field.setAttributesCount(attCount); + return field; + } + public String toString(){ + return pool.getConstantInfo(nameIndex).getValue()+pool.getConstantInfo(descriptorIndex).getValue(); + } + public AccessFlag getAccessFlags() { + return accessFlag; + } + + public void setAccessFlags(AccessFlag accessFlags) { + this.accessFlag = accessFlags; + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getDescriptorIndex() { + return descriptorIndex; + } + + public void setDescriptorIndex(int descriptorIndex) { + this.descriptorIndex = descriptorIndex; + } + + public int getAttributesCount() { + return attributesCount; + } + + public void setAttributesCount(int attributesCount) { + this.attributesCount = attributesCount; + } + + public void setPool(ConstantPool pool) { + this.pool = pool; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/Method.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/Method.java new file mode 100644 index 0000000000..b6ace8ebce --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/clz/Method.java @@ -0,0 +1,99 @@ +package com.github.wdn.coding2017.jvm.clz; + +import com.github.wdn.coding2017.jvm.attr.*; +import com.github.wdn.coding2017.jvm.constant.ConstantPool; +import com.github.wdn.coding2017.jvm.loader.ByteCodeIterator; + +/** + * Created by Administrator on 2017/4/10 0010. + */ +public class Method { + private AccessFlag accessFlags; + private int nameIndex; + private int descriptorIndex; + private CodeAttr code; + //attributes[attributes_count]; + private ConstantPool pool; + public static Method parse(ConstantPool pool, ByteCodeIterator iter) { + Method method = new Method(); + method.setAccessFlags(new AccessFlag(iter.readU2ToInt())); + method.setNameIndex(iter.readU2ToInt()); + method.setDescriptorIndex(iter.readU2ToInt()); + int methodAttributesCount = iter.readU2ToInt(); + for (int j = 0; j < methodAttributesCount; j++) { + int methodAttributeNameIndex = iter.readU2ToInt(); + String methodAttributeType = pool.getConstantInfo(methodAttributeNameIndex).getValue(); + if (methodAttributeType.equals(AttributeInfo.CODE)) { + CodeAttr codeAttr = new CodeAttr(methodAttributeNameIndex, iter.readU4ToInt(), iter.readU2ToInt(), iter.readU2ToInt(), iter.readCustomToString(iter.readU4ToInt())); + int ExceptionCount = iter.readU2ToInt(); + if (ExceptionCount > 0) { + throw new RuntimeException("方法有异常待解析"); + } + int codeAttributesCount = iter.readU2ToInt(); + for (int k = 0; k < codeAttributesCount; k++) { + int codeAttributeNameIndex = iter.readU2ToInt(); + String codeAttributeType = pool.getConstantInfo(codeAttributeNameIndex).getValue(); + if ("LineNumberTable".equals(codeAttributeType)) { + LineNumberTable lineNumberTable = LineNumberTable.parse(iter); + codeAttr.setLineNumTable(lineNumberTable); + } else if ("LocalVariableTable".equals(codeAttributeType)) { + LocalVariableTable localVariableTable = LocalVariableTable.parse(iter); + codeAttr.setLocalVarTable(localVariableTable); + }else if ("StackMapTable".equals(codeAttributeType)) { + StackMapTable stackMapTable = StackMapTable.parse(iter); + codeAttr.setStackMapTable(stackMapTable); + } else { + throw new RuntimeException("未知的Code附加属性类型" + codeAttributeType); + } + } + method.setCode(codeAttr); + } else { + throw new RuntimeException("未知的方法属性类型" + methodAttributeType); + } + } + return method; + } + @Override + public String toString(){ + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(pool.getConstantInfo(nameIndex).getValue()); + stringBuffer.append(pool.getConstantInfo(descriptorIndex).getValue()); + stringBuffer.append(code); + return stringBuffer.toString(); + } + public AccessFlag getAccessFlags() { + return accessFlags; + } + + public void setAccessFlags(AccessFlag accessFlags) { + this.accessFlags = accessFlags; + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getDescriptorIndex() { + return descriptorIndex; + } + + public void setDescriptorIndex(int descriptorIndex) { + this.descriptorIndex = descriptorIndex; + } + + public CodeAttr getCode() { + return code; + } + + public void setCode(CodeAttr code) { + this.code = code; + } + + public void setPool(ConstantPool pool) { + this.pool = pool; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ClassInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..e4565e93b1 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ClassInfo.java @@ -0,0 +1,35 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public class ClassInfo extends ConstantInfo{ + private int nameIndex; + public ClassInfo(ConstantPool constantPool){ + super(constantPool); + } + @Override + public int getType() { + return CLASS_INFO; + } + @Override + public String getValue() { + return getConstantPool().getConstantInfo(nameIndex).getValue(); + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getUtf8Index() { + return nameIndex; + } + + public String getClassName() { + return getConstantPool().getConstantInfo(nameIndex).getValue(); + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ConstantInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..24975d30c8 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ConstantInfo.java @@ -0,0 +1,30 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + public ConstantInfo(){ + + } + public ConstantInfo(ConstantPool constantPool){ + this.constantPool = constantPool; + } + public abstract int getType(); + public abstract String getValue(); + public ConstantPool getConstantPool(){ + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return constantPool.getConstantInfo(index); + } + +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ConstantPool.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..3a3f4e03e4 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/ConstantPool.java @@ -0,0 +1,32 @@ +package com.github.wdn.coding2017.jvm.constant; + + +import java.util.ArrayList; + +/** + * Created by Administrator on 2017/4/6 0006. + */ +public class ConstantPool { + public static ArrayList constantPool = new ArrayList(); + static{ + constantPool.add(new NullConstantInfo()); + } + public void put(ConstantInfo info){ + constantPool.add(info); + } + public int getSize() { + return constantPool.size()-1; + } + + public ConstantInfo getConstantInfo(int i) { + return constantPool.get(i); + } + @Override + public String toString(){ + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 1; i < constantPool.size(); i++) { + stringBuffer.append("#"+i+"=>"+constantPool.get(i).getValue()); + } + return stringBuffer.toString(); + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/FieldRefInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..86c89dc9c3 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/FieldRefInfo.java @@ -0,0 +1,39 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/8 0008. + */ +public class FieldRefInfo extends ConstantInfo { + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool constantPool) { + super(constantPool); + } + + @Override + public int getType() { + return FIELD_INFO; + } + + @Override + public String getValue() { + return getConstantPool().getConstantInfo(classInfoIndex).getValue()+getConstantPool().getConstantInfo(nameAndTypeIndex).getValue(); + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/MethodRefInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..e4d6412cd1 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/MethodRefInfo.java @@ -0,0 +1,37 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/8 0008. + */ +public class MethodRefInfo extends ConstantInfo { + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool constantPool) { + super(constantPool); + } + @Override + public int getType() { + return METHOD_INFO; + } + @Override + public String getValue() { + return getConstantPool().getConstantInfo(classInfoIndex).getValue()+getConstantPool().getConstantInfo(nameAndTypeIndex).getValue(); + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/NameAndTypeInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..3a3f1bf4c8 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,38 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/8 0008. + */ +public class NameAndTypeInfo extends ConstantInfo{ + private int nameIndex; + private int descriptorIndex; + + public NameAndTypeInfo(ConstantPool constantPool) { + super(constantPool); + } + + @Override + public int getType() { + return 0; + } + @Override + public String getValue() { + return getConstantPool().getConstantInfo(nameIndex).getValue()+getConstantPool().getConstantInfo(descriptorIndex).getValue(); + } + + public int getNameIndex() { + return nameIndex; + } + + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + + public int getDescriptorIndex() { + return descriptorIndex; + } + + public void setDescriptorIndex(int descriptorIndex) { + this.descriptorIndex = descriptorIndex; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/NullConstantInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..bea4eb973f --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/NullConstantInfo.java @@ -0,0 +1,14 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/8 0008. + */ +public class NullConstantInfo extends ConstantInfo { + public int getType() { + return 0; + } + + public String getValue() { + return null; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/StringInfo.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..a4cbd7bb5d --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/StringInfo.java @@ -0,0 +1,30 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/8 0008. + */ +public class StringInfo extends ConstantInfo { + public StringInfo(ConstantPool constantPool) { + super(constantPool); + } + + private int stringIndex; + + @Override + public int getType() { + return 0; + } + + @Override + public String getValue() { + return getConstantPool().getConstantInfo(stringIndex).getValue(); + } + + public int getStringIndex() { + return stringIndex; + } + + public void setStringIndex(int stringIndex) { + this.stringIndex = stringIndex; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/UTF8Info.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..bf5efd842a --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/constant/UTF8Info.java @@ -0,0 +1,23 @@ +package com.github.wdn.coding2017.jvm.constant; + +/** + * Created by Administrator on 2017/4/8 0008. + */ +public class UTF8Info extends ConstantInfo{ + private String value; + public UTF8Info(ConstantPool constantPool){ + super(constantPool); + } + @Override + public int getType() { + return UTF8_INFO; + } + @Override + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ByteCodeIterator.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..dc4c1c0b54 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,42 @@ +package com.github.wdn.coding2017.jvm.loader; + + +import com.github.wdn.coding2017.jvm.util.Util; + +import java.nio.charset.Charset; + +public class ByteCodeIterator { + private byte[] bytes; + private int index; + + public ByteCodeIterator(byte[] bytes){ + this.bytes = bytes; + } + + public byte[] read(){ + return new byte[]{bytes[index++]}; + } + public int readToInt(){ + return Util.byteToInt(new byte[]{bytes[index++]}); + } + public int readU2ToInt(){ + return Util.byteToInt(new byte[]{bytes[index++],bytes[index++]}); + } + public String readU2ToString(){ + return Util.byteToHexString(new byte[]{bytes[index++],bytes[index++]}); + } + public int readU4ToInt(){ + return Util.byteToInt(new byte[]{bytes[index++],bytes[index++],bytes[index++],bytes[index++]}); + } + public String readU4ToString(){ + return Util.byteToHexString(new byte[]{bytes[index++],bytes[index++],bytes[index++],bytes[index++]}); + } + public String readCustomToString(int len){ + byte[] b = new byte[len]; + for (int i = 0; i < len; i++) { + b[i] = bytes[index++]; + } + return new String(b); + //return Util.byteToHexString(b); + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java index 20c8183f3a..6f29b692d4 100644 --- a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileLoader.java @@ -1,5 +1,6 @@ package com.github.wdn.coding2017.jvm.loader; +import com.github.wdn.coding2017.jvm.clz.ClassFile; import org.apache.commons.io.FileUtils; import java.io.File; @@ -38,7 +39,7 @@ public byte[] readBinaryCode(String className) { int offset=0; // for循环使用inputStream api读取 一次读完。。 for(offset = 0; offset < fileLength && (len = inputStream.read(fileBytes, offset, (int)fileLength - offset)) != -1; offset += len) { - System.out.println("dd"); + ; } // while循环使用System.arraycopy读取 /*while ((len = inputStream.read(bytes))>-1){ @@ -73,8 +74,10 @@ public String getClassPath(){ return stringBuffer.toString(); } - - - + public ClassFile loadClass(String className) { + ClassFileParser classFileParser = new ClassFileParser(); + ClassFile classFile = classFileParser.parse(readBinaryCode(className)); + return classFile; + } } diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileParser.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..22d2e5bcd7 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/loader/ClassFileParser.java @@ -0,0 +1,105 @@ +package com.github.wdn.coding2017.jvm.loader; + +import com.github.wdn.coding2017.jvm.clz.*; +import com.github.wdn.coding2017.jvm.constant.*; + +import java.util.ArrayList; +import java.util.List; + +public class ClassFileParser { + ConstantPool pool = new ConstantPool(); + public ClassFile parse(byte[] codes) { + ByteCodeIterator iter = new ByteCodeIterator(codes); + String magic = iter.readU4ToString(); + if(!"cafebabe".equals(magic)){ + throw new Error(); + } + ClassFile classFile = new ClassFile(); + classFile.setMinorVersion(iter.readU2ToInt()); + classFile.setMajorVersion(iter.readU2ToInt()); + classFile.setConstantPool(parseConstantPool(iter)); + classFile.setAccessFlag(parseAccessFlag(iter)); + classFile.setClassIndex(parseClassIndex(iter)); + parseInterface(iter); + classFile.setFields(parseField(iter)); + classFile.setMethods(parseMethod(iter)); + return classFile; + } + + private List parseMethod(ByteCodeIterator iter) { + int methodCount = iter.readU2ToInt(); + List methods = new ArrayList<>(methodCount); + for (int i = 0; i < methodCount; i++) { + Method method = Method.parse(pool, iter); + method.setPool(pool); + methods.add(method); + } + return methods; + } + + private List parseField(ByteCodeIterator iter) { + int fieldsCount = iter.readU2ToInt(); + List fields = new ArrayList<>(fieldsCount); + for (int i = 0; i < fieldsCount; i++) { + Field field = Field.parse(iter); + field.setPool(pool); + fields.add(field); + } + return fields; + } + + private void parseInterface(ByteCodeIterator iter){ + int interfaceNum = iter.readU2ToInt(); + if (interfaceNum > 0) { + throw new RuntimeException("接口数量>0"); + } + } + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + return new AccessFlag(iter.readU2ToInt()); + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + return new ClassIndex(iter.readU2ToInt(),iter.readU2ToInt()); + } + + public ConstantPool parseConstantPool(ByteCodeIterator iter) { + + int constantPoolNum = iter.readU2ToInt(); + for (int i = 0; i < constantPoolNum-1; i++) { + int type = iter.readToInt(); + if(type==7){// class + ClassInfo classInfo = new ClassInfo(pool); + classInfo.setNameIndex(iter.readU2ToInt()); + pool.put(classInfo); + }else if(type==9){// Fieldref + FieldRefInfo fieldRefInfo = new FieldRefInfo(pool); + fieldRefInfo.setClassInfoIndex(iter.readU2ToInt()); + fieldRefInfo.setNameAndTypeIndex(iter.readU2ToInt()); + pool.put(fieldRefInfo); + }else if(type==10){// Methodref + MethodRefInfo methodRefInfo = new MethodRefInfo(pool); + methodRefInfo.setClassInfoIndex(iter.readU2ToInt()); + methodRefInfo.setNameAndTypeIndex(iter.readU2ToInt()); + pool.put(methodRefInfo); + }else if(type==1){// Utf8 + int length = iter.readU2ToInt(); + String value = iter.readCustomToString(length); + UTF8Info utf8Info = new UTF8Info(pool); + utf8Info.setValue(value); + pool.put(utf8Info); + }else if(type==8){// String + StringInfo stringInfo = new StringInfo(pool); + stringInfo.setStringIndex(iter.readU2ToInt()); + pool.put(stringInfo); + }else if(type==12){// NameAndType + NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(pool); + nameAndTypeInfo.setNameIndex(iter.readU2ToInt()); + nameAndTypeInfo.setDescriptorIndex(iter.readU2ToInt()); + pool.put(nameAndTypeInfo); + }else{ + throw new RuntimeException("未知类型"+type); + } + } + return pool; + } +} diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java index 346159abb5..9d2b468236 100644 --- a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/test/ClassFileloaderTest.java @@ -1,6 +1,10 @@ package com.github.wdn.coding2017.jvm.test; +import com.github.wdn.coding2017.jvm.clz.ClassFile; +import com.github.wdn.coding2017.jvm.clz.ClassIndex; +import com.github.wdn.coding2017.jvm.constant.*; import com.github.wdn.coding2017.jvm.loader.ClassFileLoader; +import com.github.wdn.coding2017.jvm.util.Util; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -16,8 +20,17 @@ public class ClassFileloaderTest { static String path1 = "E:\\softdata\\ideaworkspace\\self\\coding2017\\group24\\626451284\\mini-jvm\\target\\classes"; static String path2 = "E:\\temp"; - - + + static ClassFile clzFile = null; + static final String FULL_QUALIFIED_CLASS_NAME="com/coderising/jvm/test/EmployeeV1"; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.github.wdn.coding2017.jvm.test.EmployeeV1"; + + clzFile = loader.loadClass(className); + clzFile.print(); + } @Before public void setUp() throws Exception { @@ -55,7 +68,6 @@ public void testClassFileLength() { } - @Test public void testMagicNumber(){ ClassFileLoader loader = new ClassFileLoader(); @@ -65,27 +77,93 @@ public void testMagicNumber(){ byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - String acctualValue = this.byteToHexString(codes); + String acctualValue = Util.byteToHexString(codes); Assert.assertEquals("cafebabe", acctualValue); } - - - - - - - private String byteToHexString(byte[] codes ){ - StringBuffer buffer = new StringBuffer(); - for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getNameIndex()); + Assert.assertEquals(14, nameAndType.getDescriptorIndex()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); } - return buffer.toString(); } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } } diff --git a/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/util/Util.java b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/util/Util.java new file mode 100644 index 0000000000..1e54f24a79 --- /dev/null +++ b/group24/626451284/mini-jvm/src/main/java/com/github/wdn/coding2017/jvm/util/Util.java @@ -0,0 +1,24 @@ +package com.github.wdn.coding2017.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i + + 4.0.0 + + me.wyc + learning2017 + 1.0-SNAPSHOT + + + 1.7 + 1.7 + UTF-8 + UTF-8 + UTF-8 + + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + true + never + + + false + + + + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + true + + + false + + + + + + + + + junit + junit + 4.12 + + + + dom4j + dom4j + 1.6.1 + + + + + jaxen + jaxen + 1.1.6 + + + + \ No newline at end of file diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayList.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayList.java new file mode 100644 index 0000000000..8ae862da33 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayList.java @@ -0,0 +1,115 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/2. + */ +public class ArrayList implements List { + private int size = 10; + //每次扩容的长度,默认为10 + private int extendSize = 10; + + private Object[] data = new Object[size]; + + public ArrayList(Object o) { + this.add(o); + } + + public ArrayList(){} + + public void add(Object o) { + if (this.size() == this.size) { + this.size += extendSize; + Object[] newData = new Object[this.size]; + System.arraycopy(this.data, 0, newData, 0, this.data.length); + this.data = newData; + } + + for (int i = 0; i < this.data.length; i++) { + if (data[i] == null) { + data[i] = o; + break; + } else continue; + } + } + + public void add(int index, Object o) { + if (index > this.size() || index < 0) { + throw new IndexOutOfBoundsException(); + } + + if(this.size() == this.size){ + this.size += extendSize; + } + + Object[] newData = new Object[this.size]; + + System.arraycopy(this.data, 0, newData, 0, index); + newData[index] = o; + System.arraycopy(this.data, index, newData, index + 1, this.size() - index); + + this.data = newData; + } + + public Object get(int index) { + if(index > this.size() || index < 0){ + throw new IndexOutOfBoundsException(); + } + for(int i = 0; i < this.size(); i ++){ + if(index == i){ + return this.data[i]; + } + } + + return null; + } + + public Object remove(int index) { + if(index > this.size() || index < 0){ + throw new IndexOutOfBoundsException(); + } + + Object[] newData = new Object[this.size]; + Object removed = this.get(index); + + System.arraycopy(this.data, 0, newData, 0, index); + System.arraycopy(this.data, index + 1, newData, index, this.size() - index); + this.data = newData; + return removed; + } + + public int size() { + int size = 0; + for(Object obj : this.data){ + if(obj != null){ + size += 1; + } + } + + return size; + } + + public boolean contains(Object obj){ + for(int i = 0; i < this.size(); i++){ + if(obj == this.get(i)){ + return true; + } + } + + return false; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for(Object obj : data){ + if(obj != null){ + sb.append(obj.toString()).append(","); + }else { +// sb.append("null,"); + continue; + } + } + + return sb.toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayUtil.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayUtil.java new file mode 100644 index 0000000000..22bccaaf5b --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayUtil.java @@ -0,0 +1,245 @@ +package basic.dataStructure; + +/** + * @author : 温友朝 + * @date : 2017/4/5 + */ +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int length = origin.length; + int[] reversed = new int[length]; + for (int i = length - 1; i >= 0; i--) { + reversed[length - i - 1] = origin[i]; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int length = oldArray.length; + int[] arr = new int[length]; + int index = 0; + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0) { + arr[index] = oldArray[i]; + index++; + } + } + //非0的数据个数 + int[] newArr = new int[index]; + System.arraycopy(arr, 0, newArr, 0, index); + return newArr; + } + + public static Object[] remove(Object[] oldArray, Object value){ + int length = oldArray.length; + Object[] arr = new Object[length]; + int index = 0; + for (int i = 0; i < length; i++) { + if (oldArray[i] != value) { + arr[index] = oldArray[i]; + index++; + } + } + Object[] newArr = new Object[index]; + System.arraycopy(arr, 0, newArr, 0, index); + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int length1 = array1.length; + int length2 = array2.length; + int[] arr = new int[length1 + length2]; + + System.arraycopy(array1, 0, arr, 0, length1); + System.arraycopy(array2, 0, arr, length1, length2); + + //去重 + for(int i = 0; i < arr.length; i++){ + for(int j = 0; j < arr.length; j++){ + if(i != j && arr[i] == arr[j]){ + arr[j] = 0; + } + } + } + + + int[] data = removeZero(arr); + int length = data.length; + + //排序 + for (int i = 0; i < length; i++) { + for(int j = 0; j < length; j++){ + if(data[i] < data[j]){ + int tmp = data[i]; + data[i] = data[j]; + data[j] = tmp; + } + } + } + return data; + } + + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] arr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, arr, 0, oldArray.length); + return arr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*) + * @param max + * @return + */ + public int[] fibonacci(int max) { + int[] empty = {}; + int[] arr2 = {1, 1}; + + switch (max){ + case 0 : return empty; + case 1 : return empty; + case 2 : return arr2; + default: { + int[] data = arr2; + int d = data[0] + data[1]; + while (d < max){ + int length = data.length; + d = data[length - 1] + data[length - 2]; + if(d > max){ + return data; + } + int[] temp = new int[data.length + 1]; + System.arraycopy(data, 0, temp, 0, length); + temp[length] = d; + + data = temp; + } + } + } + + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] data = new int[max]; + int index = 0; + for(int i = 1; i < max; i++){ + int divided = 0; + for(int j = i; j >= 1; j--){ + if(i % j == 0){ + divided++; + } + if(divided > 2){ + break; + }else if(j == 1 && divided == 2){ + data[index] = i; + index ++; + } + } + } + + int[] result = new int[index]; + System.arraycopy(data, 0, result, 0, index); + return result; + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] perfd = new int[max]; + int perfIndex = 0; + for(int i = 1; i <= max; i++){ + int index = 0; + int[] data = new int[i]; + for(int j = i - 1; j >= 1; j--){ + if(i % j == 0){ + data[index] = j; + index ++; + } + + if(j == 1 && getSum(data) == i){ + perfd[perfIndex] = i; + perfIndex++; + } + } + } + + return removeZero(perfd); + } + + private int getSum(int[] arr){ + int sum = 0; + for(int i : arr){ + sum += i; + } + return sum; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer sb = new StringBuffer(); + for(int i : array){ + sb.append(i).append(seperator); + } + return sb.substring(0, sb.length() - 1).toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/BinaryTreeNode.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..5050ae3c95 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/BinaryTreeNode.java @@ -0,0 +1,58 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/4. + */ +public class BinaryTreeNode { + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + private BinaryTreeNode(){} + + public BinaryTreeNode(int data){ + this.data = data; + this.left = null; + this.right = null; + } + + public void setData(int data){ + BinaryTreeNode node = new BinaryTreeNode(data); + if(compareTo(data)){ + if(this.left == null){ + this.left = node; + }else{ + this.left.setData(data); + } + }else{ + if(this.right == null){ + this.right = node; + }else{ + this.right.setData(data); + } + } + } + + public int getData(){ + return data; + } + + private boolean compareTo(int d) { + System.out.println("data=" + this.data + ", d=" + d); + return this.data > d; + } + + private StringBuffer dataStr = new StringBuffer(); + private int index = 0; +// public String toString(BinaryTreeNode node) { +// while (node.left != null || node.right != null){ +// dataStr.append(index + "层,数据=").append(node.data).append("|"); +// if(node.left != null){ +// dataStr.append(node.left.data) +// } +// index ++; +// } +// +// return dataStr.toString(); +// } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/LinkedList.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/LinkedList.java new file mode 100644 index 0000000000..3ac85ad37b --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/LinkedList.java @@ -0,0 +1,341 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/3. + */ +public class LinkedList implements List { + private Node head; + + public LinkedList() { + this.head = new Node(); + } + + public void add(Object o) { + if (this.head.data == null) { + this.head = new Node(o, null); + } else { + Node temp = this.head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = new Node(o, null); + } + } + + public void add(int index, Object o) { + if (index > this.size() || index < 0) { + throw new IndexOutOfBoundsException(); + } + + if(index == 0){ + Node newNode = new Node(o, this.head); + this.head = newNode; + return; + } + + if(index == this.size()){ + this.add(o); + return; + } + + Node before = getNode(index - 1); + Node next = getNode(index); + Node newNode = new Node(o, next); + before.next = newNode; + + } + + private Node getNode(int index) { + int i = 0; + Node temp = this.head; + while (temp.data != null) { + if (index == i) { + return temp; + } + + if (temp.next != null) { + temp = temp.next; + } else break; + + i++; + } + + return null; + } + + public Object get(int index) { + if (index > this.size() || index < 0) { + throw new IndexOutOfBoundsException(); + } + + return this.getNode(index).data; + } + + public Object remove(int index) { + if(index > this.size() || index < 0){ + throw new IndexOutOfBoundsException(); + } + + Object removed = get(index); + + Node before = getNode(index - 1); + Node next = getNode(index + 1); + before.next = next; + + return removed; + } + + public int size() { + int size = 0; + Node temp = this.head; + while (temp.data != null) { + size++; + if (temp.next != null) { + temp = temp.next; + } else break; + } + + return size; + } + + public void asList(Object[] array){ + LinkedList list = new LinkedList(); + for(int i = 0; i < array.length; i++){ + list.add(array[i]); + } + + this.head = list.head; + } + + public Object[] toArray(LinkedList list){ + int size = list.size(); + Object[] arr = new Object[size]; + for(int i = 0; i < size; i++){ + arr[i] = list.get(i); + } + + return arr; + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + Node temp = this.head; + while (temp.data != null) { + sb.append(temp.data.toString()).append(","); + if (temp.next != null) { + temp = temp.next; + } else break; + } + + return sb.toString(); + } + + private static class Node { + Object data; + Node next; + + public Node() {} + + public Node(Object obj, Node next) { + this.data = obj; + this.next = next; + } + + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + int size = this.size(); + + if(size == 1){ + return; + } + + Object[] data = new Object[size]; + for(int i = 0; i < size; i++){ + data[i] = this.get(i); + } + + this.head = new Node(); + + for(int i = size - 1; i >= 0; i--){ + this.add(data[i]); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int size = this.size(); + int index = this.size()/2; + ArrayList al = new ArrayList(); + for(int i = index; i < size; i++){ + al.add(this.get(i)); + } + + this.head = new Node(); + + for(int i = 0; i < al.size(); i++){ + this.add(al.get(i)); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + for(int j = i; j < i + length; j++){ + this.remove(i); + } + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int size = list.size(); + int[] arr = new int[size]; + for(int i = 0; i < size; i++){ + int index = (Integer) list.get(i); + arr[i] = (Integer) this.get(index); + } + + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + Object[] arr1 = toArray(this); + Object[] arr2 = toArray(list); + for(int i = 0; i < arr2.length; i++){ + arr1 = ArrayUtil.remove(arr1, arr2[i]); + } + + asList(arr1); + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + int size = this.size(); + ArrayList indexList = new ArrayList(); + ArrayList valueList = new ArrayList(); + for(int i = 0; i < size; i ++){ + int valueI = (Integer)this.get(i); + int index = 0; + for(int j = i + 1; j < size; j++){ + if(valueList.contains(valueI)){ + continue; + } + int valueJ = (Integer) this.get(j); + if(valueJ == valueI){ + index++; + } + + if(index > 0){ + indexList.add(j); + valueList.add(valueJ); + } + } + } + + Object[] arr = new Object[size]; + for(int i = 0; i < size; i++){ + arr[i] = indexList.contains(i) ? false : this.get(i); + } + + ArrayUtil au = new ArrayUtil(); + arr = au.remove(arr, false); + + asList(arr); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int size = this.size(); + int[] range = new int[max - min]; + int index = 0; + for(int i = 0; i < size; i++){ + int value = (Integer) this.get(i); + if(value > min && value < max){ + range[index] = value; + index++; + } + } + + Object[] arr = new Object[size]; + for(int i = 0; i < size; i++){ + arr[i] = this.get(i); + } + + for(int i = 0; i < range.length; i++){ + arr = ArrayUtil.remove(arr, range[i]); + } + + asList(arr); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + //组合成新的链表 + int listSize = list.size(); + for(int i = 0 ; i < listSize; i ++){ + this.add(list.get(i)); + } + + //转化成数组 + int size = this.size(); + int[] arr = new int[size]; + for(int i = 0; i < size; i++){ + arr[i] = (Integer)this.get(i); + } + //排序 + for(int i = 0; i < size - 1; i ++){ + for(int j = 0; j < size - i - 1; j ++){ + if(arr[j] >= arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + //组装 + LinkedList li = new LinkedList(); + for(int i = 0; i < size; i ++){ + li.add(arr[i]); + } + return li; + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/List.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/List.java new file mode 100644 index 0000000000..dc2a62aab3 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/List.java @@ -0,0 +1,12 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/2. + */ +public interface List { + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/Queue.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Queue.java new file mode 100644 index 0000000000..36ca7e9647 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Queue.java @@ -0,0 +1,72 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/4. + */ +public class Queue { + private Object[] data; + + private int size = 10; + + private int extendedSize = 10; + + public Queue(){ + this.data = new Object[size]; + } + + public Queue(Object o){ + this.data = new Object[size]; + data[0] = o; + } + + public void enQueue(Object o){ + //被添加的位置 + int index = this.size(); + if(this.size() == this.size){ + this.size += extendedSize; + Object[] newData = new Object[this.size]; + System.arraycopy(this.data, 0, newData, 0, index); + newData[index] = o; + this.data = newData; + }else{ + this.data[index] = o; + } + } + + public Object deQueue(){ + Object[] newData = new Object[this.size]; + Object d = this.data[0]; + System.arraycopy(this.data, 1, newData, 0, this.size - 1); + this.data = newData; + + return d; + } + + public Object peek(){ + return this.data[0]; + } + + public boolean isEmpty(){ + return peek() == null; + } + + public int size(){ + int size = 0; + for(Object obj : this.data){ + size += obj == null ? 0 : 1; + } + + return size; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for(Object obj : this.data){ + if(obj != null){ + sb.append(obj.toString()).append(","); + }else break; + } + return sb.toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/Stack.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Stack.java new file mode 100644 index 0000000000..bea16033fa --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Stack.java @@ -0,0 +1,41 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/4. + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + this.elementData.add(o); + } + + public Object pop(){ + int index = elementData.size() - 1; + Object obj = elementData.remove(index); + + return obj; + } + + public Object peek(){ + int index = elementData.size() - 1; + return elementData.get(index); + } + public boolean isEmpty(){ + return peek() == null; + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for(int i = this.size() - 1; i >= 0; i--){ + sb.append(elementData.get(i).toString()).append(","); + } + + return sb.toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/LoginAction.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/LoginAction.java new file mode 100644 index 0000000000..14b8aba8e2 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/LoginAction.java @@ -0,0 +1,39 @@ +package basic.liteStruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/ReadXML.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/ReadXML.java new file mode 100644 index 0000000000..458b247e18 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/ReadXML.java @@ -0,0 +1,28 @@ +package basic.liteStruts; + +import org.dom4j.Document; +import org.dom4j.io.SAXReader; + +import java.io.File; + +/** + * @author : 温友朝 + * @date : 2017/4/10 + */ +public class ReadXML { + Document document; + + public ReadXML(String filePath) throws Exception{ + SAXReader reader = new SAXReader(); + document = reader.read(new File(ReadXML.class.getResource("/").getFile()) + filePath); + } + + public String getActionClass(String actionName){ + return document.selectSingleNode("//action[@name='" + actionName + "']").valueOf("@class"); + } + + public String getJspPage(String actionName, String result){ + return document.selectSingleNode("//action[@name='" + actionName + "']") + .selectSingleNode("//result[@name='" + result + "']").getText(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/Struts.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/Struts.java new file mode 100644 index 0000000000..3b28f2bf60 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/Struts.java @@ -0,0 +1,78 @@ +package basic.liteStruts; + +import java.lang.reflect.Method; +import java.util.Map; + + +public class Struts { + /** + * 0. 读取配置文件struts.xml + *

+ * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + * ("name"="test" , "password"="1234") , + * 那就应该调用 setName和setPassword方法 + *

+ * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + *

+ * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + * 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + * 放到View对象的parameters + *

+ * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + public static View runAction(String actionName, Map parameters) { + try{ + //0. 读取配置文件struts.xml + ReadXML read = new ReadXML("/resources/struts.xml"); + //1. 找到对应的class + String className = read.getActionClass(actionName); + Class clz = Class.forName(className); + //得到对象 + Object la = clz.newInstance(); + setNameAndPassword(clz, la, parameters); + //2. 调用execute方法 + String result = invokeExecute(clz, la); + //3. 找到对象的所有getter方法 + getResultMap(clz, la, parameters); + //4. 确定使用哪一个jsp + String viewName = read.getJspPage(actionName, result); + View view = new View(); + view.setJsp(viewName); + view.setParameters(parameters); + return view; + }catch(Exception e){ + e.printStackTrace(); + return null; + } + } + + private static void setNameAndPassword(Class clz, Object la, Map parameters) throws Exception { + Method setName = clz.getDeclaredMethod("setName", String.class); + setName.invoke(la, parameters.get("name")); + + Method setPassword = clz.getDeclaredMethod("setPassword", String.class); + setPassword.invoke(la, parameters.get("password")); + } + + private static String invokeExecute(Class clz, Object la)throws Exception{ + Method execute = clz.getDeclaredMethod("execute", null); + Method getMessage = clz.getDeclaredMethod("getMessage", null); + execute.invoke(la, null); + return getMessage.invoke(la, null).toString(); + } + + private static void getResultMap(Class clz, Object la, Map parameters) throws Exception{ + Method[] methods = clz.getMethods(); + for(Method me : methods){ + if(me.getName().startsWith("get")){ + String info = me.invoke(la, null).toString(); + String method= me.getName(); + String key = method.substring(3, method.length()).toLowerCase(); + parameters.put(key, info); + }else continue; + } + + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/View.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/View.java new file mode 100644 index 0000000000..777380b8f9 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/View.java @@ -0,0 +1,23 @@ +package basic.liteStruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/75939388/learning2017/src/main/java/miniJVM/Demo.java b/group24/75939388/learning2017/src/main/java/miniJVM/Demo.java new file mode 100644 index 0000000000..565983ab06 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/miniJVM/Demo.java @@ -0,0 +1,7 @@ +package miniJVM; + +/** + * Created by macvi on 2017/4/11. + */ +public class Demo { +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/DownloadThread.java b/group24/75939388/learning2017/src/main/java/thread/download/DownloadThread.java new file mode 100644 index 0000000000..190cae6423 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/DownloadThread.java @@ -0,0 +1,39 @@ +package thread.download; + + +import thread.download.api.Connection; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + String localFile = ""; + + CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos, String localFileName, CyclicBarrier barrier){ + this.localFile = localFileName; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.barrier = barrier; + } + public void run(){ + try{ + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + byte[] buffer = this.conn.read(this.startPos, this.endPos); + file.seek(startPos); + file.write(buffer); + file.close(); + this.conn.close(); + barrier.await(); + }catch(Exception e){ + e.printStackTrace(); + } + } +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/FileDownloader.java b/group24/75939388/learning2017/src/main/java/thread/download/FileDownloader.java new file mode 100644 index 0000000000..f50560b8be --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/FileDownloader.java @@ -0,0 +1,106 @@ +package thread.download; + +import thread.download.api.Connection; +import thread.download.api.ConnectionManager; +import thread.download.api.DownloadListener; + +import java.io.File; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class FileDownloader { + + String url; + String fileLocation; + DownloadListener listener; + ConnectionManager cm; + + RandomAccessFile file; + + int length; + + private static final int MAX_THREAD_NUM = 5; + + + public FileDownloader(String _url, String fileLocation) { + this.url = _url; + this.fileLocation = fileLocation; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + + try { + CyclicBarrier barrier = new CyclicBarrier(MAX_THREAD_NUM, new Runnable() { + public void run() { + listener.notifyFinished(); + } + }); + + conn = cm.open(this.url); + this.length = conn.getContentLength(); + + file = getEmptyFile(); + + int divided = length/MAX_THREAD_NUM; + int[] pos = new int[MAX_THREAD_NUM + 1]; + for(int i = 0; i < MAX_THREAD_NUM; i++){ + pos[i] = i == 0 ? 0 : divided * i; + } + pos[MAX_THREAD_NUM] = length; + + for(int i = 0; i < MAX_THREAD_NUM; i++){ + new DownloadThread(conn, pos[i], pos[i + 1] - 1, this.fileLocation, barrier).start(); + } + + + File file2 = new File(fileLocation); + System.out.println("file.length=" + file2.length()); + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + } + + public RandomAccessFile getEmptyFile(){ + try{ + RandomAccessFile file = new RandomAccessFile(this.fileLocation, "rw"); + byte[] empty = new byte[this.length]; + file.write(empty); + file.close(); + return file; + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/api/Connection.java b/group24/75939388/learning2017/src/main/java/thread/download/api/Connection.java new file mode 100644 index 0000000000..08a85b35dd --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/Connection.java @@ -0,0 +1,23 @@ +package thread.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionException.java b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionException.java new file mode 100644 index 0000000000..e8fac91d1e --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionException.java @@ -0,0 +1,8 @@ +package thread.download.api; + +public class ConnectionException extends Exception { + + public ConnectionException(String msg){ + super(msg); + } +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionManager.java b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionManager.java new file mode 100644 index 0000000000..f5f6c2ff70 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package thread.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + Connection open(String url) throws ConnectionException; +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/api/DownloadListener.java b/group24/75939388/learning2017/src/main/java/thread/download/api/DownloadListener.java new file mode 100644 index 0000000000..16393c4dd9 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package thread.download.api; + +public interface DownloadListener { + void notifyFinished(); +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionImpl.java b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2e2544ab27 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionImpl.java @@ -0,0 +1,93 @@ +package thread.download.impl; + +import thread.download.api.Connection; +import thread.download.api.ConnectionException; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.ConnectException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionImpl implements Connection { + + private int length = 0; + + private URL url; + + private HttpURLConnection conn; + + private InputStream is; + + private ByteArrayOutputStream baos; + + private ConnectionImpl() {} + + public ConnectionImpl(URL url) { + this.url = url; + try { + this.conn = (HttpURLConnection) url.openConnection(); + this.conn.setRequestMethod("GET"); + this.conn.setReadTimeout(5000); + int responseCode = this.conn.getResponseCode(); + System.out.println("连接状态=" + responseCode); + if (responseCode != 200) { + throw new ConnectionException("连接到" + url.toURI() + "失败"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public byte[] read(int startPos, int endPos) throws IOException { + try { + //设置读取段落 + this.conn = (HttpURLConnection) url.openConnection(); + this.conn.setRequestMethod("GET"); + this.conn.setReadTimeout(5000); + this.conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + //获取返回值 + int response = conn.getResponseCode(); + if(response != 200 && response != 206){ + throw new ConnectException("没有连接上" + url.toURI() + ", 状态码为" + response); + } + //开始读取 + int length = endPos - startPos + 1; + this.is = conn.getInputStream(); + byte[] buffer = new byte[1024]; + baos = new ByteArrayOutputStream(length); + while(-1 != is.read(buffer)){ + baos.write(buffer); + } + System.out.println(startPos + "-" + endPos + "文件段读取完成"); + return baos.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + this.close(); + } + } + + public int getContentLength() { + try { + this.length = this.conn.getContentLength(); + System.out.println("获取的文件长度=" + length); + return this.length; + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + + public void close() { + try { + if(is != null){ + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionManagerImpl.java b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ede8ccac00 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,20 @@ +package thread.download.impl; + +import thread.download.api.Connection; +import thread.download.api.ConnectionException; +import thread.download.api.ConnectionManager; + +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + public Connection open(String url) throws ConnectionException { + Connection conn = null; + try{ + conn = new ConnectionImpl(new URL(url)); + }catch(Exception e){ + e.printStackTrace(); + } + return conn; + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/ArrayListTest.java b/group24/75939388/learning2017/src/test/java/data_structure/ArrayListTest.java new file mode 100644 index 0000000000..ee8ee6b0d0 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/ArrayListTest.java @@ -0,0 +1,69 @@ +package data_structure; + +import org.junit.Test; +import basic.dataStructure.ArrayList; + +/** + * Created by macvi on 2017/4/2. + */ +public class ArrayListTest { + + @Test + public void TestAdd(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 32; i++){ + al.add(i + ""); + } + + System.out.println("ArrayList.content-->" + al.toString()); + } + + + @Test + public void testIndexAdd(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 17; i ++){ + al.add(i + ""); + } + + al.add(3, "xxoo"); + al.add(11, "abcd"); + al.add(0, "efgh"); + al.add(al.size(), "ijkl"); + + System.out.println("al.toString-->" + al.toString()); + System.out.println("size-->" + al.size()); + } + + @Test + public void testGet(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 18; i ++){ + al.add(i + "zxcd"); + } + + System.out.println("get-->" + al.get(13)); + } + + @Test + public void testRemove(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 18; i ++){ + al.add(i + ""); + } + System.out.println("size1-->" + al.size()); + System.out.println("al.toString1-->" + al.toString()); + String re = (String)al.remove(12); + System.out.println("remove index=12 :"); + System.out.println("re-->" + re); + System.out.println("size2-->" + al.size()); + System.out.println("al.toString2-->" + al.toString()); + + String re1 = (String)al.remove(1); + System.out.println("remove index=1 :"); + System.out.println("re-->" + re1); + System.out.println("size2-->" + al.size()); + System.out.println("al.toString2-->" + al.toString()); + } + +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/ArrayUtilTest.java b/group24/75939388/learning2017/src/test/java/data_structure/ArrayUtilTest.java new file mode 100644 index 0000000000..1dc1a6f263 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/ArrayUtilTest.java @@ -0,0 +1,67 @@ +package data_structure; + +import org.junit.Test; +import basic.dataStructure.ArrayUtil; + +import java.util.Arrays; + +/** + * @author : 温友朝 + * @date : 2017/4/5 + */ +public class ArrayUtilTest { + ArrayUtil au = new ArrayUtil(); + + @Test + public void testReverse(){ + int[] arr = {1, 2, 3, 4, 5}; + this.au.reverseArray(arr); + } + + @Test + public void testTrim(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arr = this.au.removeZero(oldArr); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testMerge(){ + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + + int[] arr = this.au.merge(a1, a2); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testGrow(){ + int[] arr = {1, 2, 3, 4, 5}; + int[] arr2 = this.au.grow(arr, 4); + System.out.println(Arrays.toString(arr2)); + } + + @Test + public void testFibonacci(){ + int[] arr = this.au.fibonacci(100); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testPrimes(){ + int[] arr = this.au.getPrimes(100000); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testPerfectNumbers(){ + int[] arr = this.au.getPerfectNumbers(10000); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testJoin(){ + int[] arr = this.au.getPerfectNumbers(10000); + System.out.println(this.au.join(arr, "-")); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/BinaryNodeTreeTest.java b/group24/75939388/learning2017/src/test/java/data_structure/BinaryNodeTreeTest.java new file mode 100644 index 0000000000..df976147e3 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/BinaryNodeTreeTest.java @@ -0,0 +1,29 @@ +package data_structure; + +import org.junit.Test; +import basic.dataStructure.BinaryTreeNode; + +/** + * @author : 温友朝 + * @date : 2017/4/5 + */ +public class BinaryNodeTreeTest { + +// @Test +// public BinaryTreeNode getTree(){ +// BinaryTreeNode btn = new BinaryTreeNode(5); +// btn.setData(3); +// +// return btn; +// } + + @Test + public void testAdd(){ + BinaryTreeNode btn = new BinaryTreeNode(5); + btn.setData(3); + btn.setData(7); + btn.setData(10); + btn.setData(6); + btn.setData(4); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/LinkedListTest.java b/group24/75939388/learning2017/src/test/java/data_structure/LinkedListTest.java new file mode 100644 index 0000000000..c98a305623 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/LinkedListTest.java @@ -0,0 +1,151 @@ +package data_structure; + +import basic.dataStructure.LinkedList; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by macvi on 2017/4/3. + */ +public class LinkedListTest { + + LinkedList ll = new LinkedList(); + + @Before + public void init(){ + for(int i = 0; i < 10; i++){ + ll.add(i); + } + } + + @After + public void print(){ + + } + + + @Test + public void testLinkedListAdd(){ + LinkedList ll = new LinkedList(); + ll.add("123"); + ll.add("456"); + ll.add("asdf"); + ll.add("zxcv"); + + + System.out.println("ll.toString-->" + ll); + System.out.println("ll.size--->" + ll.size()); + } + + @Test + public void testLinkedListIndexAdd(){ + System.out.println("12345"); + } + + @Test + public void testGet(){ + LinkedList ll = new LinkedList(); + for(int i = 0; i < 10; i ++){ + ll.add(i + ""); + } + + System.out.println("get-->" + ll.get(9)); + System.out.println("ll.toString-->" + ll.toString() + "\nsize-->" + ll.size()); + } + + @Test + public void testIndexAdd(){ + LinkedList ll = new LinkedList(); + for(int i = 0; i < 5; i ++){ + ll.add(i + ""); + } + + ll.add(5, "xxoo"); + System.out.println("index get-->" + ll.get(0)); + System.out.println("ll.toString2-->" + ll.toString() + "\nsize-->" + ll.size()); + } + + @Test + public void testRemove(){ + LinkedList ll = new LinkedList(); + for(int i = 0; i < 6; i ++){ + ll.add(i + ""); + } + + Object removed = ll.remove(-1); + System.out.println("ll.toString-->" + ll.toString() + "\nsize-->" + ll.size()); + System.out.println("removed-->" + removed.toString()); + } + + @Test + public void testReverse(){ + ll.reverse(); + System.out.println("ll.reverse-->" + ll.toString()); + } + + @Test + public void testRemoveFirstHalf(){ + ll.removeFirstHalf(); + System.out.println("ll.removeFirstHalf-->" + ll.toString()); + } + + @Test + public void testRemoveL(){ + ll.remove(2, 5); + System.out.println("ll.toString-->" + ll.toString()); + } + + @Test + public void testGetElements(){ + LinkedList l2 = new LinkedList(); + l2.add(3); + l2.add(5); + l2.add(9); + l2.add(0); + + int[] arr = ll.getElements(l2); + System.out.println("arr->" + Arrays.toString(arr)); + } + + @Test + public void testRemoveDuplicate(){ + ll.add(1); + ll.add(3); + ll.add(4); + ll.add(10); + ll.add(11); + ll.removeDuplicateValues(); + System.out.println("ll.toString-->" + ll.toString()); + } + + @Test + public void testRemoveRange(){ + ll.removeRange(2, 6); + System.out.println("ll.toString-->" + ll.toString()); + } + + @Test + public void testSubtract(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(5); + + ll.subtract(list); + System.out.println("ll.toString-->" + ll); + } + + @Test + public void testIntersection(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(5); + + LinkedList list2 = ll.intersection(list); + System.out.println(list2); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/QueueTest.java b/group24/75939388/learning2017/src/test/java/data_structure/QueueTest.java new file mode 100644 index 0000000000..3db6d82e49 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/QueueTest.java @@ -0,0 +1,50 @@ +package data_structure; + +import org.junit.Assert; +import org.junit.Test; +import basic.dataStructure.Queue; + +/** + * Created by macvi on 2017/4/4. + */ +public class QueueTest { + + private Queue newQueue(){ + Queue q = new Queue(); + for(int i = 0; i < 13; i++){ + q.enQueue(i + ""); + } + + return q; + } + + @Test + public void testEnqueue(){ + Queue q = newQueue(); + q.enQueue(10 + ""); + q.enQueue( "xxoo"); + System.out.println("queue-->" + q.toString()); + } + + @Test + public void testSize(){ + Queue q = newQueue(); + + Assert.assertEquals(13, q.size()); + } + + @Test + public void testDequeue(){ + Queue q = newQueue(); + Object obj = q.deQueue(); + + Assert.assertEquals("0", obj); + } + + @Test + public void testIsEmpty(){ + Queue q = newQueue(); + + Assert.assertEquals(false, q.isEmpty()); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/StackTest.java b/group24/75939388/learning2017/src/test/java/data_structure/StackTest.java new file mode 100644 index 0000000000..b933b8b63e --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/StackTest.java @@ -0,0 +1,49 @@ +package data_structure; + +import org.junit.Assert; +import org.junit.Test; +import basic.dataStructure.Stack; + +/** + * Created by macvi on 2017/4/4. + */ +public class StackTest { + + private Stack getStack(){ + Stack s = new Stack(); + for(int i = 0; i < 14; i ++){ + s.push(i + ""); + } + + return s; + } + + @Test + public void pushTest(){ + Stack s = getStack(); + + System.out.println("stack-->" + s.toString()); + } + + @Test + public void testSize(){ + Stack s = getStack(); + + Assert.assertEquals(14, s.size()); + } + + @Test + public void testPeek(){ + Stack s = getStack(); + + Assert.assertEquals("13", s.peek()); + } + + @Test + public void testPop(){ + Stack s = getStack(); + + Assert.assertEquals("13", s.pop()); + Assert.assertEquals(13, s.size()); + } +} diff --git a/group24/75939388/learning2017/src/test/java/download/FileDownloaderTest.java b/group24/75939388/learning2017/src/test/java/download/FileDownloaderTest.java new file mode 100644 index 0000000000..0dc8536269 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/download/FileDownloaderTest.java @@ -0,0 +1,54 @@ +package download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import thread.download.FileDownloader; +import thread.download.api.ConnectionManager; +import thread.download.api.DownloadListener; +import thread.download.impl.ConnectionManagerImpl; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://www.baidu.com/img/bd_logo1.png"; + String fileLocation = "D:\\Tee\\JavaLearnin\\test.png"; + FileDownloader downloader = new FileDownloader(url, fileLocation); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + public void notifyFinished() { + downloadFinished = true; + } + + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } + +} diff --git a/group24/75939388/learning2017/src/test/java/liteStruts/StrutsTest.java b/group24/75939388/learning2017/src/test/java/liteStruts/StrutsTest.java new file mode 100644 index 0000000000..4477c5f51b --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/liteStruts/StrutsTest.java @@ -0,0 +1,42 @@ +package liteStruts; + +import org.junit.Assert; +import org.junit.Test; +import basic.liteStruts.Struts; +import basic.liteStruts.View; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml b/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml deleted file mode 100644 index 54550a4174..0000000000 --- a/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - diff --git a/group24/798277403/src/basic/ArrayList.java b/group24/798277403/src/basic/ArrayList.java new file mode 100644 index 0000000000..436ca1f9f2 --- /dev/null +++ b/group24/798277403/src/basic/ArrayList.java @@ -0,0 +1,147 @@ +package basic; + +import java.util.Arrays; + +/** + java泛型: + ? 表示不确定的java类型。 + T 表示java类型。 + K V 分别代表java键值中的Key Value。 + E 代表Element。 + + * 自己实现的ArrayList + * Created by zhouliang on 2017-03-10. + */ +class ArrayList implements List { + + private Object[] elementData; + + private int size; + + public ArrayList(int size){ + this.elementData = new Object[size]; + } + + //默认大小为10 + public ArrayList(){ + this.elementData = new Object[10]; + } + + //判断是否需要扩展容量 ((旧容量 * 3) / 2) + 1 + private void checkcapacity(int index){ + if(index>elementData.length){ + int length = (elementData.length*3)/2+1; + /* + Object[] temp = new Object[length]; + for(int i=0; i= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private ArrayList list = null; + private int position = 0; + + private ArrayListIterator(ArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size()) { + return false; + } + return true; + } + + @Override + public E next() { + return list.get(position++); + } + } +} diff --git a/group24/798277403/src/basic/ArrayListTest.java b/group24/798277403/src/basic/ArrayListTest.java new file mode 100644 index 0000000000..c50af7befa --- /dev/null +++ b/group24/798277403/src/basic/ArrayListTest.java @@ -0,0 +1,55 @@ +package basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp(){ + for(int i=0; i<100; i++){ + arrayList.add(i); + } + } + + @Test + public void add() throws Exception { + for(int i=100; i<1000; i++){ + arrayList.add(i); + } + Assert.assertEquals(1000,arrayList.size()); + } + + @Test + public void add1() throws Exception { + java.util.LinkedList l = new java.util.LinkedList(); + } + + @Test + public void get() throws Exception { + System.out.println(arrayList.get(99)); + } + + @Test + public void remove() throws Exception { + System.out.println(arrayList.size()); + arrayList.remove(arrayList.size()-1); + System.out.println(arrayList.size()); + //Assert.assertEquals((Integer)99,(Integer)arrayList.size()); + } + + @Test + public void iterator() throws Exception { + Iterator iterator = arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git a/group24/798277403/src/basic/BinaryTree.java b/group24/798277403/src/basic/BinaryTree.java new file mode 100644 index 0000000000..c2d5bb2c24 --- /dev/null +++ b/group24/798277403/src/basic/BinaryTree.java @@ -0,0 +1,124 @@ +package basic; + +/** + * Created by zhouliang on 2017-03-11. + */ +class BinaryTree { + private TreeNode root; + + class TreeNode { + int val = 0; + TreeNode left = null; + TreeNode right = null; + public TreeNode(int val) { + this.val = val; + } + } + + /** + * 递归创建二叉树 + * @param node + * @param data + */ + public void buildTree(TreeNode node,int data){ + if(root == null){ + root = new TreeNode(data); + }else{ + if(data < node.val){ + if(node.left == null){ + node.left = new TreeNode(data); + }else{ + buildTree(node.left,data); + } + }else{ + if(node.right == null){ + node.right = new TreeNode(data); + }else{ + buildTree(node.right,data); + } + } + } + } + + /** + * 前序遍历 + * @param node + */ + public void preOrder(TreeNode node){ + if(node != null){ + System.out.println(node.val); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * @param node + */ + public void inOrder(TreeNode node){ + if(node != null){ + inOrder(node.left); + System.out.println(node.val); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * @param node 一般是传入根节点 + */ + public void postOrder(TreeNode node){ + if(node != null){ + postOrder(node.left); + postOrder(node.right); + System.out.println(node.val); + } + } + + + /** + * 分层打印 + * @param root 树的根节点 + * @return 分层后的数组 + */ + public int[][] printTree(TreeNode root) { + if(root == null){ + return null; + } + java.util.LinkedList queue = new java.util.LinkedList(); + TreeNode last = root; + TreeNode nlast = null ; + TreeNode currentNode = null; + java.util.ArrayList> lists = new java.util.ArrayList>(); + java.util.ArrayList list = new java.util.ArrayList(); + queue.add(last); + while(!queue.isEmpty()){ + currentNode = queue.poll(); + list.add(currentNode.val); + + if(currentNode.left!=null){ + queue.add(currentNode.left); + nlast = currentNode.left; + } + if(currentNode.right!=null){ + queue.add(currentNode.right); + nlast = currentNode.right; + } + if(currentNode == last){ + lists.add(list); + last = nlast; + list = new java.util.ArrayList(); + } + } + + int[][] result = new int[lists.size()][]; + for(int i = 0 ; i < lists.size() ; i++){ + result[i] = new int[lists.get(i).size()]; + for(int j = 0 ; j < lists.get(i).size() ; j++){ + result[i][j] = lists.get(i).get(j); + } + } + return result; + } +} diff --git a/group24/798277403/src/basic/BinaryTreeNode.java b/group24/798277403/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..599b2b1ca8 --- /dev/null +++ b/group24/798277403/src/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package basic; + +/** + * 自己实现的BinaryTreeNode + * Created by zhouliang on 2017-03-10. + */ +class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if(data==null && left==null && right==null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + return this; + }else{ + BinaryTreeNode temp = this; + BinaryTreeNode node = new BinaryTreeNode(); + while(true){ + if((Integer)o > (Integer)temp.getData()){ + if(temp.getRight() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + temp.setRight(node); + return this; + }else{ + temp = temp.getRight(); + } + }else{ + if(temp.getLeft() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + temp.setLeft(node); + return this; + }else{ + temp = temp.getLeft(); + } + } + } + } + } +} diff --git a/group24/798277403/src/basic/Iterator.java b/group24/798277403/src/basic/Iterator.java new file mode 100644 index 0000000000..4c0fedf988 --- /dev/null +++ b/group24/798277403/src/basic/Iterator.java @@ -0,0 +1,10 @@ +package basic; + +/** + * 自己实现的Iterator + * Created by zhouliang on 2017-03-10. + */ +interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group24/798277403/src/basic/LRU/LRUPageFrame.java b/group24/798277403/src/basic/LRU/LRUPageFrame.java new file mode 100644 index 0000000000..e69e051321 --- /dev/null +++ b/group24/798277403/src/basic/LRU/LRUPageFrame.java @@ -0,0 +1,130 @@ +package basic.LRU; + +/** + * Created by zhouliang on 2017-04-04. + */ +public class LRUPageFrame { + private static class Node { + Node prev; + Node next; + int pageNum; + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * @param pageNum + * @return + */ + public void access(int pageNum) { + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + moveExistingNodeToHead(node); + } else{ + node = new Node(); + node.pageNum = pageNum; + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + } + addNewNodetoHead(node); + } + } + + private void addNewNodetoHead(Node node) { + if(isEmpty()){ + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + + } + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + if (node == first) { + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + Node nextNode = node.next; + nextNode.prev = prevNode; + } + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group24/798277403/src/basic/LRU/LRUPageFrameTest.java b/group24/798277403/src/basic/LRU/LRUPageFrameTest.java new file mode 100644 index 0000000000..4993f42e75 --- /dev/null +++ b/group24/798277403/src/basic/LRU/LRUPageFrameTest.java @@ -0,0 +1,33 @@ +package basic.LRU; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-04-04. + */ +public class LRUPageFrameTest { + @Test + public void testAccess() { + MyLRUPageFrame frame = new MyLRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } +} diff --git a/group24/798277403/src/week4/LRU/MyLRUPageFrame.java b/group24/798277403/src/basic/LRU/MyLRUPageFrame.java similarity index 99% rename from group24/798277403/src/week4/LRU/MyLRUPageFrame.java rename to group24/798277403/src/basic/LRU/MyLRUPageFrame.java index 9e720d8589..b2387dbd81 100644 --- a/group24/798277403/src/week4/LRU/MyLRUPageFrame.java +++ b/group24/798277403/src/basic/LRU/MyLRUPageFrame.java @@ -1,4 +1,4 @@ -package week4.LRU; +package basic.LRU; /** * Created by zhouliang on 2017-04-04. diff --git a/group24/798277403/src/basic/LinkedList.java b/group24/798277403/src/basic/LinkedList.java new file mode 100644 index 0000000000..30d0b4a099 --- /dev/null +++ b/group24/798277403/src/basic/LinkedList.java @@ -0,0 +1,234 @@ +package basic; + +/** + * 自己实现的LinkedList + * Created by zhouliang on 2017-03-10. + */ +class LinkedList implements List { + + private int size; + private Node first; + private Node last; + + public LinkedList(){ + } + + @Override + public void add(E e) { + Node temp = new Node(e); + if(first != null){ + last.next = temp; + last = temp; + }else{ + first = temp; + last = temp; + } + size++; + } + + /** + * 指定下标添加元素 + * @param index 可以在链表末尾加,就是可以的等于size,不能大于size + * @param e 代表Element + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + + Node temp = new Node(e); + if(index == size){ + last.next = temp; + last = temp; + }else{ + Node begin = first; + index--; + while(index>0){ + begin = begin.next; + index--; + } + Node next = begin.next; + begin.next = temp; + temp.next = next; + } + size++; + } + + @Override + public E get(int index) { + checkElementIndex(index); + + Node temp = first; + while(index>0){ + temp = temp.next; + index--; + } + return temp.value; + } + + @Override + public E remove(int index) { + checkElementIndex(index); + + Node temp; + if(index == 0){ + temp = first; + first = first.next; + size--; + return temp.value; + }else{ + temp = first; + index--; + //找到要删除节点的前一个节点 + while(index>0){ + temp = temp.next; + index--; + } + Node removeNode = temp.next; + temp.next = removeNode.next; + size--; + return removeNode.value; + + } + + } + + public E removeLast(){ + return remove(size-1); + } + + public void addFirst(E e){ + Node temp = new Node(e); + if(first == null){ + first = temp; + last = temp; + }else{ + temp.next = first; + first = temp; + } + size++; + } + + public void addLast(E e){ + Node temp = new Node(); + if(first == null){ + first = temp; + last = temp; + }else{ + last.next = temp; + last = temp; + } + size++; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + @Override + public int size() { + return size; + } + + private static class Node{ + E value; + Node next; + + Node(){ + + } + + Node(E e){ + this.value = e; + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * @param list + */ + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + return null; + } +} diff --git a/group24/798277403/src/basic/LinkedListTest.java b/group24/798277403/src/basic/LinkedListTest.java new file mode 100644 index 0000000000..0611a1ddb7 --- /dev/null +++ b/group24/798277403/src/basic/LinkedListTest.java @@ -0,0 +1,53 @@ +package basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class LinkedListTest { + + private LinkedList myLinkedList = new LinkedList<>(); + + private java.util.LinkedList systemLinkedList = new java.util.LinkedList<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + myLinkedList.add(i); + systemLinkedList.add(i); + } + } + @Test + public void add() throws Exception { + for(int i=0; i<10; i++){ + System.out.println(myLinkedList.get(i)); + } + } + + @Test + public void remove() throws Exception { + myLinkedList.remove(5); + for(int i=0; i { + void add(E e); + void add(int index, E e); + E get(int index); + E remove(int index); + int size(); +} diff --git a/group24/798277403/src/basic/Queue.java b/group24/798277403/src/basic/Queue.java new file mode 100644 index 0000000000..371f2f9340 --- /dev/null +++ b/group24/798277403/src/basic/Queue.java @@ -0,0 +1,33 @@ +package basic; + +/** + * 自己实现的Queue,用自己的LinkedList实现 + * Created by zhouliang on 2017-03-10. + */ +class Queue { + + private LinkedList linkedList; + + public Queue(){ + this.linkedList = new LinkedList(); + } + /** + * 从队列头部添加元素 + * @param e 代表Element + */ + public void enQueue(E e){ + linkedList.addFirst(e); + } + + public E deQueue(){ + return linkedList.removeLast(); + } + + public boolean isEmpty(){ + return linkedList.size() > 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group24/798277403/src/basic/QueueTest.java b/group24/798277403/src/basic/QueueTest.java new file mode 100644 index 0000000000..8af2de5ada --- /dev/null +++ b/group24/798277403/src/basic/QueueTest.java @@ -0,0 +1,36 @@ +package basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * 自己实现的队列,先进先出 + * Created by zhouliang on 2017-03-10. + */ +public class QueueTest { + private Queue queue = new Queue<>(); + + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + queue.enQueue(i); + } + } + + @Test + public void enQueue() throws Exception { + System.out.println(queue.size()); + } + + @Test + public void deQueue() throws Exception { + System.out.println(queue.deQueue()+" "+queue.size()); + } + + @Test + public void isEmpty() throws Exception { + + } + +} \ No newline at end of file diff --git a/group24/798277403/src/basic/Stack.java b/group24/798277403/src/basic/Stack.java new file mode 100644 index 0000000000..e6351da277 --- /dev/null +++ b/group24/798277403/src/basic/Stack.java @@ -0,0 +1,34 @@ +package basic; + +/** + * 自己实现的Stack + * Created by zhouliang on 2017-03-10. + */ +class Stack { + + private ArrayList elementData; + + public Stack(){ + this.elementData = new ArrayList(); + } + + public void push(E e){ + elementData.add(e); + } + + public E pop(){ + return elementData.remove(elementData.size()-1); + } + + public E peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() > 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group24/798277403/src/basic/StackTest.java b/group24/798277403/src/basic/StackTest.java new file mode 100644 index 0000000000..921560a0f0 --- /dev/null +++ b/group24/798277403/src/basic/StackTest.java @@ -0,0 +1,39 @@ +package basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class StackTest { + private Stack stack = new Stack<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + stack.push(i); + } + } + + @Test + public void pop() throws Exception { + System.out.println("size "+stack.size()); + while(stack.size()>0){ + System.out.println(stack.pop()); + } + } + + @Test + public void peek() throws Exception { + System.out.println(stack.size()); + int i = stack.peek(); + System.out.println(i+" "+stack.size()); + } + + @Test + public void isEmpty() throws Exception { + + } + +} \ No newline at end of file diff --git a/group24/798277403/src/basic/array/ArrayUtil.java b/group24/798277403/src/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..e29a4845bf --- /dev/null +++ b/group24/798277403/src/basic/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package basic.array; + +import java.util.Arrays; + + +class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin 原数组 + */ + void reverseArray(int[] origin){ + if(origin!=null && origin.length>0){ + int i, n; + for(i=0, n=origin.length-1; i0) { + int index = 0; + int length = 0; + int[] temp = new int[oldArray.length]; + for(int i=0; iarray2[j]){ + merges[index] = array2[j]; + j++; + }else{ + merges[index] = array2[j]; + i++; + j++; + split++; + } + } + while(i0) { + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + stringBuffer.append(array[i]); + if (i != array.length - 1) { + stringBuffer.append(seperator); + } + } + return stringBuffer.toString(); + }else{ + return null; + } + } + +} diff --git a/group24/798277403/src/basic/array/ArrayUtilTest.java b/group24/798277403/src/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..792c778e75 --- /dev/null +++ b/group24/798277403/src/basic/array/ArrayUtilTest.java @@ -0,0 +1,92 @@ +package basic.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Random; + +/** + * Created by zhouliang on 2017-03-13. + */ +public class ArrayUtilTest { + private int[] array; + private ArrayUtil arrayUtil ; + private int SIZE = 11; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + array = new int[SIZE]; + Random random = new Random(); + + for(int i=0; i implements List{ + + private int size; + private Node first; + private Node last; + + public LinkedList(){ + } + + @Override + public void add(E e) { + Node temp = new Node(e); + if(first != null){ + last.next = temp; + last = temp; + }else{ + first = temp; + last = temp; + } + size++; + } + + /** + * 指定下标添加元素 + * @param index 可以在链表末尾加,就是可以的等于size,不能大于size + * @param e 代表Element + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + + Node temp = new Node(e); + if(index == size){ + last.next = temp; + last = temp; + }else{ + Node begin = first; + index--; + while(index>0){ + begin = begin.next; + index--; + } + Node next = begin.next; + begin.next = temp; + temp.next = next; + } + size++; + } + + @Override + public E get(int index) { + checkElementIndex(index); + + Node temp = first; + while(index>0){ + temp = temp.next; + index--; + } + return temp.value; + } + + @Override + public E remove(int index) { + checkElementIndex(index); + + Node temp; + if(index == 0){ + temp = first; + first = first.next; + size--; + return temp.value; + }else{ + temp = first; + index--; + //找到要删除节点的前一个节点 + while(index>0){ + temp = temp.next; + index--; + } + Node removeNode = temp.next; + temp.next = removeNode.next; + size--; + return removeNode.value; + + } + + } + + public E removeLast(){ + return remove(size-1); + } + + public void addFirst(E e){ + Node temp = new Node(e); + if(first == null){ + first = temp; + last = temp; + }else{ + temp.next = first; + first = temp; + } + size++; + } + + public void addLast(E e){ + Node temp = new Node(); + if(first == null){ + first = temp; + last = temp; + }else{ + last.next = temp; + last = temp; + } + size++; + } + + //检查index是否是合法的get下标 + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + //检查index是否是合法的add下标 + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + @Override + public int size() { + return size; + } + + private static class Node{ + E value; + Node next; + Node(){ + } + Node(E e){ + this.value = e; + } + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node preNode = first; + + //头尾结点互换位置 + Node node = last; + last = first; + first = node; + + node = preNode.next; + Node nextNode; + + while (node != null) { + nextNode = node.next; + node.next = preNode; + preNode = node; + node = nextNode; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + int num = this.size/2; + this.size = this.size - num; + while(num>0){ + //Node temp = first.next; + first = first.next; + num--; + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + checkPositionIndex(i); + if(length+i>size-1){ + throw new IndexOutOfBoundsException("Index: " + (i+length) + ", Size: " + + size); + } + int temp = 0; + Node newFirst = first; + Node beginNode = newFirst; + while(temp < i){ + beginNode = beginNode.next; + temp++; + } + Node endNode = beginNode.next; + size = size - length; + while(length>0){ + endNode = endNode.next; + length--; + } + first = newFirst; + beginNode.next = endNode; + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(list==null || list.size()==0){ + return null; + }else{ + int[] result = new int[list.size()]; + int index = 0; + int length = 0; + Node temp = first; + for (int i=0; imin && (Integer)temp.value myLinkedList = new LinkedList<>(); + + private java.util.LinkedList systemLinkedList = new java.util.LinkedList<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + myLinkedList.add(i); + systemLinkedList.add(i); + } + } + @Test + public void add() throws Exception { + for(int i=0; i<10; i++){ + System.out.println(myLinkedList.get(i)); + } + } + + @Test + public void reverse(){ + myLinkedList.reverse(); + for(int i=0; i<10; i++){ + System.out.println(myLinkedList.get(i)); + } + } + + @Test + public void removeFirstHalf(){ + myLinkedList.removeFirstHalf(); + System.out.println(myLinkedList.size()); + for(int i=0; i list = new LinkedList(); + list.add(0); + list.add(7); + list.add(9); + int[] reuslt = myLinkedList.getElements(list); + System.out.println(reuslt.length); + for(int i=0; i list = new LinkedList(); + list.add(0); + list.add(7); + list.add(9); + myLinkedList.subtract(list); + for(int i=0; i list = new LinkedList(); + list.add(0); + list.add(2); + list.add(9); + list.add(9); + list.add(9); + list.add(9); + list.add(9); + list.add(9); + list.add(9); + + + + LinkedList result = myLinkedList.intersection(list); + for(int i=0; i { + void add(E e); + void add(int index, E e); + E get(int index); + E remove(int index); + int size(); +} diff --git a/group24/798277403/src/basic/stack/StackUtil.java b/group24/798277403/src/basic/stack/StackUtil.java new file mode 100644 index 0000000000..7e5c157306 --- /dev/null +++ b/group24/798277403/src/basic/stack/StackUtil.java @@ -0,0 +1,116 @@ +package basic.stack; + +import java.util.Stack; + +/** + * Created by zhouliang on 2017-04-08. + */ +class StackUtil { + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s.empty()){ + return; + } + int i = getAndRemoveBottom(s); // 依次返回1、2、3 + reverse(s); + s.push(i); + } + + //移除并返回当前的栈底元素 + private static int getAndRemoveBottom(Stack s){ + int result = s.pop(); + if(s.empty()){ + return result; + }else{ + int bottom = getAndRemoveBottom(s); + s.push(result); + return bottom; + } + } + + + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param o + */ + public static void remove(Stack s,Object o) { + if(s.empty()){ + return; + } + Object temp = s.pop(); + if(temp == o){ + return; + } + remove(s,o); + s.push(temp); + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + Object[] result = new Object[len]; + int index = 0; + while(index s, int index){ + Object temp = s.pop(); + index--; + if(0 == index){ + s.push(temp); + return temp; + } + Object result = getIndex(s,index); + s.push(temp); + return result; + } + + + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + char[] chars = s.toCharArray(); + Stack stack = new Stack(); + for(char c : chars){ + if(c=='(' || c=='[' || c=='{'){ + stack.push(c); + }else if(c==')'){ + char top = stack.peek(); + if(top == '('){ + stack.pop(); + } + }else if(c==']'){ + char top = stack.peek(); + if(top == '['){ + stack.pop(); + } + }else if(c=='}'){ + char top = stack.peek(); + if(top == '{'){ + stack.pop(); + } + } + } + + return stack.empty(); + } +} diff --git a/group24/798277403/src/basic/stack/StackUtilTest.java b/group24/798277403/src/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..c5ecd1fcbe --- /dev/null +++ b/group24/798277403/src/basic/stack/StackUtilTest.java @@ -0,0 +1,77 @@ +package basic.stack; + +import org.junit.Test; + +import java.util.Stack; + +/** + * Created by zhouliang on 2017-04-08. + */ +public class StackUtilTest { + + @Test + public void testReverse(){ + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + while(!s.isEmpty()){ + System.out.println(s.pop()); + } + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + StackUtil.reverse(s); + while(!s.isEmpty()){ + System.out.println(s.pop()); + } + } + + @Test + public void remove(){ + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + + StackUtil.remove(s,3); + while(!s.isEmpty()){ + System.out.println(s.pop()); + } + } + + @Test + public void getTop(){ + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + + Object[] result = StackUtil.getTop(s,2); + while(!s.isEmpty()){ + System.out.println(s.pop()); + } + + for(Object o : result){ + System.out.println(o); + } + } + + @Test + public void isValidPairs(){ + String s = "([e{d}f])"; + String s1 = "([b{x]y})"; + boolean result = StackUtil.isValidPairs(s); + System.out.println(result); + boolean result1 = StackUtil.isValidPairs(s1); + System.out.println(result1); + } +} diff --git a/group24/798277403/src/download/DownloadThread.java b/group24/798277403/src/download/DownloadThread.java new file mode 100644 index 0000000000..df55a6fee1 --- /dev/null +++ b/group24/798277403/src/download/DownloadThread.java @@ -0,0 +1,50 @@ +package download; + + +import download.api.Connection; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +class DownloadThread extends Thread{ + + private Connection conn; + private int startPos; + private int endPos; + private CyclicBarrier barrier; + private String localFile; + public DownloadThread(Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + + + try { + System.out.println("Begin to read [" + startPos +"-"+endPos+"]"); + + byte[] data = conn.read(startPos, endPos); + + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + + file.seek(startPos); + + file.write(data); + + file.close(); + + conn.close(); + + barrier.await(); //等待别的线程完成 + + } catch (Exception e) { + e.printStackTrace(); + + } + + } +} diff --git a/group24/798277403/src/download/FileDownloader.java b/group24/798277403/src/download/FileDownloader.java new file mode 100644 index 0000000000..928c1cf2e5 --- /dev/null +++ b/group24/798277403/src/download/FileDownloader.java @@ -0,0 +1,116 @@ +package download; + + +import download.api.Connection; +import download.api.ConnectionManager; +import download.api.DownloadListener; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class FileDownloader { + + private String url; + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + + private static final int DOWNLOAD_TRHEAD_NUM = 3; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + createPlaceHolderFile(this.localFile, length); + int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); + for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){ + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + thread.start(); + } + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + } + + //往文件里面写0,占住磁盘 + private void createPlaceHolderFile(String fileName, int contentLen) throws IOException { + RandomAccessFile file = new RandomAccessFile(fileName,"rw"); + for(int i=0; i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + } + +} diff --git a/group24/798277403/src/download/impl/ConnectionManagerImpl.java b/group24/798277403/src/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..e721d46194 --- /dev/null +++ b/group24/798277403/src/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package download.impl; + +import download.api.Connection; +import download.api.ConnectionException; +import download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group24/798277403/src/download/test/ConnectionTest.java b/group24/798277403/src/download/test/ConnectionTest.java new file mode 100644 index 0000000000..cd8c9ca04d --- /dev/null +++ b/group24/798277403/src/download/test/ConnectionTest.java @@ -0,0 +1,53 @@ +package download.test; + +import download.api.Connection; +import download.api.ConnectionManager; +import download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class ConnectionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testContentLength() throws Exception{ + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception{ + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + + // 测试不充分,没有断言内容是否正确 + } + + +} diff --git a/group24/798277403/src/download/test/FileDownloaderTest.java b/group24/798277403/src/download/test/FileDownloaderTest.java new file mode 100644 index 0000000000..f0337f6bc1 --- /dev/null +++ b/group24/798277403/src/download/test/FileDownloaderTest.java @@ -0,0 +1,63 @@ +package download.test; + +import download.FileDownloader; +import download.api.ConnectionManager; +import download.api.DownloadListener; +import download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + //String url = "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + + FileDownloader downloader = new FileDownloader(url,"C:\\Users\\zhouliang\\Desktop\\mycoding\\test.jpg"); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group24/798277403/src/litestruts/LoginAction.java b/group24/798277403/src/litestruts/LoginAction.java new file mode 100644 index 0000000000..8c448e3630 --- /dev/null +++ b/group24/798277403/src/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + + public String getMessage(){ + return this.message; + } +} diff --git a/group24/798277403/src/litestruts/Struts.java b/group24/798277403/src/litestruts/Struts.java new file mode 100644 index 0000000000..2139dd8551 --- /dev/null +++ b/group24/798277403/src/litestruts/Struts.java @@ -0,0 +1,155 @@ +package litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/* + +0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) +据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 +("name"="test" , "password"="1234") , +那就应该调用 setName和setPassword方法 + +2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + +3. 通过反射找到对象的所有getter方法(例如 getMessage), +通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +放到View对象的parameters + +4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, +放到View对象的jsp字段中。 + +*/ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + Class actionClass = null; + LoginAction loginAction = null; + View view = new View(); + try { + db = documentBuilderFactory.newDocumentBuilder(); + Document document = db.parse("src/litestruts/struts.xml"); + NodeList nodeList = document.getElementsByTagName("action"); + + //遍历每一个action节点 + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + //获取action节点的所有属性集合 + NamedNodeMap attrs = node.getAttributes(); + //获取name结点的值 + String nodeName = attrs.getNamedItem("name").getNodeValue(); + + if(nodeName.equals(actionName)){ + //获取LoginAction实例 + actionClass = Class.forName(attrs.getNamedItem("class").getNodeValue()); + loginAction = (LoginAction) actionClass.newInstance(); + + //设置用户名密码属性 + Set> entrySet = parameters.entrySet(); + for (Map.Entry entry : entrySet) { + if (entry.getKey().equals("name")) { + loginAction.setName(entry.getValue()); + } + if (entry.getKey().equals("password")) { + loginAction.setPassword(entry.getValue()); + } + } + + //执行execute()方法 + String result = loginAction.execute(); + + //将message封装到view + String message = loginAction.getMessage(); + Map map = new HashMap(); + map.put("message",message); + view.setParameters(map); + + //解析对应的result节点 + NodeList childNodes = node.getChildNodes(); + //遍历childNodes获取每个节点的节点名和节点值 + for (int k = 0; k < childNodes.getLength(); k++) { + Node childNode = childNodes.item(k); + //区分出text类型的node以及element类型的node + if (childNode.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap attributes = childNode.getAttributes(); + String nodeValue = attributes.getNamedItem("name").getNodeValue(); + if(nodeValue.equals(result)){ + view.setJsp(childNode.getTextContent()); + } + } + + } + + } + + } + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + + return view; + } + +/* DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + Class actionClass = null; + LoginAction loginAction = null; + try { + db = documentBuilderFactory.newDocumentBuilder(); + Document document = db.parse("src/week2/litestruts/struts.xml"); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("一共有" + nodeList.getLength() + "个结点"); + //遍历每一个action节点 + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + //获取action节点的所有属性集合 + NamedNodeMap attrs = node.getAttributes(); + //遍历action的属性 + for (int j = 0; j < attrs.getLength(); j++) { + //通过item(index)方法获取book节点的某一个属性 + Node attr = attrs.item(j); + String name = attrs.getNamedItem("name").getNodeValue(); + System.out.println("++++++++++"+name); + //获取属性名 + System.out.print("属性名:" + attr.getNodeName()); + //获取属性值 + System.out.println("--属性值" + attr.getNodeValue()); + if(attr.getNodeName().equals(actionName)){ + actionClass = Class.forName(attr.getNodeValue()); + loginAction = (LoginAction) actionClass.newInstance(); + } + } + //解析book节点的子节点 + NodeList childNodes = node.getChildNodes(); + //遍历childNodes获取每个节点的节点名和节点值 + for (int k = 0; k < childNodes.getLength(); k++) { + //区分出text类型的node以及element类型的node + if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { + //获取了element类型节点的节点名 + System.out.print(childNodes.item(k).getNodeName()); + //获取了element类型节点的节点值 + System.out.println("--节点值是:" + childNodes.item(k).getFirstChild().getNodeValue()); + System.out.println("--节点值是:" + childNodes.item(k).getTextContent()); + } + } + } + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + }*/ +} \ No newline at end of file diff --git a/group24/798277403/src/litestruts/StrutsTest.java b/group24/798277403/src/litestruts/StrutsTest.java new file mode 100644 index 0000000000..868a78cdba --- /dev/null +++ b/group24/798277403/src/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group24/798277403/src/litestruts/View.java b/group24/798277403/src/litestruts/View.java new file mode 100644 index 0000000000..1eed614744 --- /dev/null +++ b/group24/798277403/src/litestruts/View.java @@ -0,0 +1,23 @@ +package litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/798277403/src/mini_jvm/clz/AccessFlag.java b/group24/798277403/src/mini_jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..b6717402da --- /dev/null +++ b/group24/798277403/src/mini_jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package mini_jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + +} \ No newline at end of file diff --git a/group24/798277403/src/mini_jvm/clz/ClassFile.java b/group24/798277403/src/mini_jvm/clz/ClassFile.java new file mode 100644 index 0000000000..4cf756c090 --- /dev/null +++ b/group24/798277403/src/mini_jvm/clz/ClassFile.java @@ -0,0 +1,65 @@ +package mini_jvm.clz; + + +import mini_jvm.constant.ClassInfo; +import mini_jvm.constant.ConstantPool; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + + public void print(){ + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + System.out.println("Super Class Name:"+ getSuperClassName()); + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} diff --git a/group24/798277403/src/mini_jvm/clz/ClassIndex.java b/group24/798277403/src/mini_jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..dcc59908f0 --- /dev/null +++ b/group24/798277403/src/mini_jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package mini_jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/group24/798277403/src/mini_jvm/constant/ClassInfo.java b/group24/798277403/src/mini_jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..ed39387440 --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/ClassInfo.java @@ -0,0 +1,24 @@ +package mini_jvm.constant; + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group24/798277403/src/mini_jvm/constant/ConstantInfo.java b/group24/798277403/src/mini_jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..d488321043 --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package mini_jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/group24/798277403/src/mini_jvm/constant/ConstantPool.java b/group24/798277403/src/mini_jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..c8d30c78db --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/ConstantPool.java @@ -0,0 +1,27 @@ +package mini_jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + this.constantInfos.add(info); + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/group24/798277403/src/mini_jvm/constant/FieldRefInfo.java b/group24/798277403/src/mini_jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..3d74c4244f --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package mini_jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group24/798277403/src/mini_jvm/constant/MethodRefInfo.java b/group24/798277403/src/mini_jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..4037881ad8 --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package mini_jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/group24/798277403/src/mini_jvm/constant/NameAndTypeInfo.java b/group24/798277403/src/mini_jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..badba119df --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package mini_jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/group24/798277403/src/mini_jvm/constant/NullConstantInfo.java b/group24/798277403/src/mini_jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..28d102c997 --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package mini_jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group24/798277403/src/mini_jvm/constant/StringInfo.java b/group24/798277403/src/mini_jvm/constant/StringInfo.java new file mode 100644 index 0000000000..2a9387d247 --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package mini_jvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/group24/798277403/src/mini_jvm/constant/UTF8Info.java b/group24/798277403/src/mini_jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..6fc44f5355 --- /dev/null +++ b/group24/798277403/src/mini_jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package mini_jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/group24/798277403/src/mini_jvm/loader/ByteCodeIterator.java b/group24/798277403/src/mini_jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..35ca6420b6 --- /dev/null +++ b/group24/798277403/src/mini_jvm/loader/ByteCodeIterator.java @@ -0,0 +1,55 @@ +package mini_jvm.loader; + +import mini_jvm.util.Util; + +import java.util.Arrays; + +public class ByteCodeIterator { + private byte[] codes; + private int pos; + + public ByteCodeIterator(byte[] codes){ + this.codes = codes; + } + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1toInt() { + return Util.byteToInt(new byte[] { codes[pos++] }); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++] }); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] }); + } + + public String nextU4ToHexString() { + return Util.byteToHexString((new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] })); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + + } + + public void back(int n) { + this.pos -= n; + } + +} diff --git a/group24/798277403/src/mini_jvm/loader/ClassFileLoader.java b/group24/798277403/src/mini_jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..db3402aeb6 --- /dev/null +++ b/group24/798277403/src/mini_jvm/loader/ClassFileLoader.java @@ -0,0 +1,94 @@ +package mini_jvm.loader; + +import mini_jvm.clz.ClassFile; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + className = className.replace('.', File.separatorChar) +".class"; + for(String path : this.clzPaths){ + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } + } + return null; + } + + private byte[] loadClassFile(String clzFileName) { + File f = new File(clzFileName); + try { + return IOUtils.toByteArray(new FileInputStream(f)); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + this.clzPaths.add(path); + } + + public String getClassPath(){ + return StringUtils.join(this.clzPaths,";"); + } + + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + } + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + +} diff --git a/group24/798277403/src/mini_jvm/test/EmployeeV1.java b/group24/798277403/src/mini_jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..aed78de928 --- /dev/null +++ b/group24/798277403/src/mini_jvm/test/EmployeeV1.java @@ -0,0 +1,28 @@ +package mini_jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group24/798277403/src/mini_jvm/util/Util.java b/group24/798277403/src/mini_jvm/util/Util.java new file mode 100644 index 0000000000..bc2e724f03 --- /dev/null +++ b/group24/798277403/src/mini_jvm/util/Util.java @@ -0,0 +1,22 @@ +package mini_jvm.util; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i implements List { - - private Object[] elementData; - - private int size; - - public ArrayList(int size){ - this.elementData = new Object[size]; - } - - //默认大小为10 - public ArrayList(){ - this.elementData = new Object[10]; - } - - //判断是否需要扩展容量 ((旧容量 * 3) / 2) + 1 - private void checkcapacity(int index){ - if(index>elementData.length){ - int length = (elementData.length*3)/2+1; - /* - Object[] temp = new Object[length]; - for(int i=0; i= 0 && index < size; - } - - private void checkPositionIndex(int index) { - if (!isPositionIndex(index)) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " - + size); - } - } - - private boolean isPositionIndex(int index) { - return index >= 0 && index <= size; - } - - public Iterator iterator() { - return new ArrayListIterator(this); - } - - private class ArrayListIterator implements Iterator { - private ArrayList list = null; - private int position = 0; - - private ArrayListIterator(ArrayList list) { - this.list = list; - } - - @Override - public boolean hasNext() { - if ((position + 1) > size()) { - return false; - } - return true; - } - - @Override - public E next() { - return list.get(position++); - } - } -} diff --git a/group24/798277403/src/week1/ArrayListTest.java b/group24/798277403/src/week1/ArrayListTest.java deleted file mode 100644 index 98e30a222a..0000000000 --- a/group24/798277403/src/week1/ArrayListTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package week1; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * Created by zhouliang on 2017-03-10. - */ -public class ArrayListTest { - - private ArrayList arrayList = new ArrayList(); - - @Before - public void setUp(){ - for(int i=0; i<100; i++){ - arrayList.add(i); - } - } - - @Test - public void add() throws Exception { - for(int i=100; i<1000; i++){ - arrayList.add(i); - } - Assert.assertEquals(1000,arrayList.size()); - } - - @Test - public void add1() throws Exception { - java.util.LinkedList l = new java.util.LinkedList(); - } - - @Test - public void get() throws Exception { - System.out.println(arrayList.get(99)); - } - - @Test - public void remove() throws Exception { - System.out.println(arrayList.size()); - arrayList.remove(arrayList.size()-1); - System.out.println(arrayList.size()); - //Assert.assertEquals((Integer)99,(Integer)arrayList.size()); - } - - @Test - public void iterator() throws Exception { - Iterator iterator = arrayList.iterator(); - while(iterator.hasNext()){ - System.out.println(iterator.next()); - } - } - -} \ No newline at end of file diff --git a/group24/798277403/src/week1/BinaryTree.java b/group24/798277403/src/week1/BinaryTree.java deleted file mode 100644 index 3c480dc012..0000000000 --- a/group24/798277403/src/week1/BinaryTree.java +++ /dev/null @@ -1,124 +0,0 @@ -package week1; - -/** - * Created by zhouliang on 2017-03-11. - */ -class BinaryTree { - private TreeNode root; - - class TreeNode { - int val = 0; - TreeNode left = null; - TreeNode right = null; - public TreeNode(int val) { - this.val = val; - } - } - - /** - * 递归创建二叉树 - * @param node - * @param data - */ - public void buildTree(TreeNode node,int data){ - if(root == null){ - root = new TreeNode(data); - }else{ - if(data < node.val){ - if(node.left == null){ - node.left = new TreeNode(data); - }else{ - buildTree(node.left,data); - } - }else{ - if(node.right == null){ - node.right = new TreeNode(data); - }else{ - buildTree(node.right,data); - } - } - } - } - - /** - * 前序遍历 - * @param node - */ - public void preOrder(TreeNode node){ - if(node != null){ - System.out.println(node.val); - preOrder(node.left); - preOrder(node.right); - } - } - - /** - * 中序遍历 - * @param node - */ - public void inOrder(TreeNode node){ - if(node != null){ - inOrder(node.left); - System.out.println(node.val); - inOrder(node.right); - } - } - - /** - * 后序遍历 - * @param node 一般是传入根节点 - */ - public void postOrder(TreeNode node){ - if(node != null){ - postOrder(node.left); - postOrder(node.right); - System.out.println(node.val); - } - } - - - /** - * 分层打印 - * @param root 树的根节点 - * @return 分层后的数组 - */ - public int[][] printTree(TreeNode root) { - if(root == null){ - return null; - } - java.util.LinkedList queue = new java.util.LinkedList(); - TreeNode last = root; - TreeNode nlast = null ; - TreeNode currentNode = null; - java.util.ArrayList> lists = new java.util.ArrayList>(); - java.util.ArrayList list = new java.util.ArrayList(); - queue.add(last); - while(!queue.isEmpty()){ - currentNode = queue.poll(); - list.add(currentNode.val); - - if(currentNode.left!=null){ - queue.add(currentNode.left); - nlast = currentNode.left; - } - if(currentNode.right!=null){ - queue.add(currentNode.right); - nlast = currentNode.right; - } - if(currentNode == last){ - lists.add(list); - last = nlast; - list = new java.util.ArrayList(); - } - } - - int[][] result = new int[lists.size()][]; - for(int i = 0 ; i < lists.size() ; i++){ - result[i] = new int[lists.get(i).size()]; - for(int j = 0 ; j < lists.get(i).size() ; j++){ - result[i][j] = lists.get(i).get(j); - } - } - return result; - } -} diff --git a/group24/798277403/src/week1/BinaryTreeNode.java b/group24/798277403/src/week1/BinaryTreeNode.java deleted file mode 100644 index e680d5ed15..0000000000 --- a/group24/798277403/src/week1/BinaryTreeNode.java +++ /dev/null @@ -1,72 +0,0 @@ -package week1; - -/** - * 自己实现的BinaryTreeNode - * Created by zhouliang on 2017-03-10. - */ -class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - if(data==null && left==null && right==null){ - this.setData(o); - this.setLeft(null); - this.setRight(null); - return this; - }else{ - BinaryTreeNode temp = this; - BinaryTreeNode node = new BinaryTreeNode(); - while(true){ - if((Integer)o > (Integer)temp.getData()){ - if(temp.getRight() == null){ - node.setData(o); - node.setLeft(null); - node.setRight(null); - - temp.setRight(node); - return this; - }else{ - temp = temp.getRight(); - } - }else{ - if(temp.getLeft() == null){ - node.setData(o); - node.setLeft(null); - node.setRight(null); - - temp.setLeft(node); - return this; - }else{ - temp = temp.getLeft(); - } - } - } - } - } -} diff --git a/group24/798277403/src/week1/Iterator.java b/group24/798277403/src/week1/Iterator.java deleted file mode 100644 index 73ba87c125..0000000000 --- a/group24/798277403/src/week1/Iterator.java +++ /dev/null @@ -1,10 +0,0 @@ -package week1; - -/** - * 自己实现的Iterator - * Created by zhouliang on 2017-03-10. - */ -interface Iterator { - boolean hasNext(); - Object next(); -} diff --git a/group24/798277403/src/week1/LinkedList.java b/group24/798277403/src/week1/LinkedList.java deleted file mode 100644 index e0160d0e5f..0000000000 --- a/group24/798277403/src/week1/LinkedList.java +++ /dev/null @@ -1,234 +0,0 @@ -package week1; - -/** - * 自己实现的LinkedList - * Created by zhouliang on 2017-03-10. - */ -class LinkedList implements List { - - private int size; - private Node first; - private Node last; - - public LinkedList(){ - } - - @Override - public void add(E e) { - Node temp = new Node(e); - if(first != null){ - last.next = temp; - last = temp; - }else{ - first = temp; - last = temp; - } - size++; - } - - /** - * 指定下标添加元素 - * @param index 可以在链表末尾加,就是可以的等于size,不能大于size - * @param e 代表Element - */ - @Override - public void add(int index, E e) { - checkPositionIndex(index); - - Node temp = new Node(e); - if(index == size){ - last.next = temp; - last = temp; - }else{ - Node begin = first; - index--; - while(index>0){ - begin = begin.next; - index--; - } - Node next = begin.next; - begin.next = temp; - temp.next = next; - } - size++; - } - - @Override - public E get(int index) { - checkElementIndex(index); - - Node temp = first; - while(index>0){ - temp = temp.next; - index--; - } - return temp.value; - } - - @Override - public E remove(int index) { - checkElementIndex(index); - - Node temp; - if(index == 0){ - temp = first; - first = first.next; - size--; - return temp.value; - }else{ - temp = first; - index--; - //找到要删除节点的前一个节点 - while(index>0){ - temp = temp.next; - index--; - } - Node removeNode = temp.next; - temp.next = removeNode.next; - size--; - return removeNode.value; - - } - - } - - public E removeLast(){ - return remove(size-1); - } - - public void addFirst(E e){ - Node temp = new Node(e); - if(first == null){ - first = temp; - last = temp; - }else{ - temp.next = first; - first = temp; - } - size++; - } - - public void addLast(E e){ - Node temp = new Node(); - if(first == null){ - first = temp; - last = temp; - }else{ - last.next = temp; - last = temp; - } - size++; - } - - private void checkElementIndex(int index) { - if (!isElementIndex(index)) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " - + size); - } - } - - private boolean isElementIndex(int index) { - return index >= 0 && index < size; - } - - private void checkPositionIndex(int index) { - if (!isPositionIndex(index)) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " - + size); - } - } - - private boolean isPositionIndex(int index) { - return index >= 0 && index <= size; - } - @Override - public int size() { - return size; - } - - private static class Node{ - E value; - Node next; - - Node(){ - - } - - Node(E e){ - this.value = e; - } - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和listB均包含已升序排列的整数 - * 从当前链表中取出那些listB所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在listB中出现的元素 - * @param list - */ - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/group24/798277403/src/week1/LinkedListTest.java b/group24/798277403/src/week1/LinkedListTest.java deleted file mode 100644 index 9a8a9d936d..0000000000 --- a/group24/798277403/src/week1/LinkedListTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package week1; - -import org.junit.Before; -import org.junit.Test; - -/** - * Created by zhouliang on 2017-03-10. - */ -public class LinkedListTest { - - private LinkedList myLinkedList = new LinkedList<>(); - - private java.util.LinkedList systemLinkedList = new java.util.LinkedList<>(); - - @Before - public void setUp(){ - for(int i=0; i<10; i++){ - myLinkedList.add(i); - systemLinkedList.add(i); - } - } - @Test - public void add() throws Exception { - for(int i=0; i<10; i++){ - System.out.println(myLinkedList.get(i)); - } - } - - @Test - public void remove() throws Exception { - myLinkedList.remove(5); - for(int i=0; i { - void add(E e); - void add(int index, E e); - E get(int index); - E remove(int index); - int size(); -} diff --git a/group24/798277403/src/week1/Queue.java b/group24/798277403/src/week1/Queue.java deleted file mode 100644 index 184f3a5336..0000000000 --- a/group24/798277403/src/week1/Queue.java +++ /dev/null @@ -1,33 +0,0 @@ -package week1; - -/** - * 自己实现的Queue,用自己的LinkedList实现 - * Created by zhouliang on 2017-03-10. - */ -class Queue { - - private LinkedList linkedList; - - public Queue(){ - this.linkedList = new LinkedList(); - } - /** - * 从队列头部添加元素 - * @param e 代表Element - */ - public void enQueue(E e){ - linkedList.addFirst(e); - } - - public E deQueue(){ - return linkedList.removeLast(); - } - - public boolean isEmpty(){ - return linkedList.size() > 0; - } - - public int size(){ - return linkedList.size(); - } -} diff --git a/group24/798277403/src/week1/QueueTest.java b/group24/798277403/src/week1/QueueTest.java deleted file mode 100644 index b5a333f64d..0000000000 --- a/group24/798277403/src/week1/QueueTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package week1; - -import org.junit.Before; -import org.junit.Test; - -/** - * 自己实现的队列,先进先出 - * Created by zhouliang on 2017-03-10. - */ -public class QueueTest { - private Queue queue = new Queue<>(); - - - @Before - public void setUp(){ - for(int i=0; i<10; i++){ - queue.enQueue(i); - } - } - - @Test - public void enQueue() throws Exception { - System.out.println(queue.size()); - } - - @Test - public void deQueue() throws Exception { - System.out.println(queue.deQueue()+" "+queue.size()); - } - - @Test - public void isEmpty() throws Exception { - - } - -} \ No newline at end of file diff --git a/group24/798277403/src/week1/Stack.java b/group24/798277403/src/week1/Stack.java deleted file mode 100644 index f85aa2ada5..0000000000 --- a/group24/798277403/src/week1/Stack.java +++ /dev/null @@ -1,34 +0,0 @@ -package week1; - -/** - * 自己实现的Stack - * Created by zhouliang on 2017-03-10. - */ -class Stack { - - private ArrayList elementData; - - public Stack(){ - this.elementData = new ArrayList(); - } - - public void push(E e){ - elementData.add(e); - } - - public E pop(){ - return elementData.remove(elementData.size()-1); - } - - public E peek(){ - return elementData.get(elementData.size()-1); - } - - public boolean isEmpty(){ - return elementData.size() > 0; - } - - public int size(){ - return elementData.size(); - } -} diff --git a/group24/798277403/src/week1/StackTest.java b/group24/798277403/src/week1/StackTest.java deleted file mode 100644 index f4213d66d3..0000000000 --- a/group24/798277403/src/week1/StackTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package week1; - -import org.junit.Before; -import org.junit.Test; - -/** - * Created by zhouliang on 2017-03-10. - */ -public class StackTest { - private Stack stack = new Stack<>(); - - @Before - public void setUp(){ - for(int i=0; i<10; i++){ - stack.push(i); - } - } - - @Test - public void pop() throws Exception { - System.out.println("size "+stack.size()); - while(stack.size()>0){ - System.out.println(stack.pop()); - } - } - - @Test - public void peek() throws Exception { - System.out.println(stack.size()); - int i = stack.peek(); - System.out.println(i+" "+stack.size()); - } - - @Test - public void isEmpty() throws Exception { - - } - -} \ No newline at end of file diff --git a/group24/798277403/src/week2/array/ArrayUtil.java b/group24/798277403/src/week2/array/ArrayUtil.java deleted file mode 100644 index 04c03f95e0..0000000000 --- a/group24/798277403/src/week2/array/ArrayUtil.java +++ /dev/null @@ -1,235 +0,0 @@ -package week2.array; - -import java.util.Arrays; - - -class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin 原数组 - */ - void reverseArray(int[] origin){ - if(origin!=null && origin.length>0){ - int i, n; - for(i=0, n=origin.length-1; i0) { - int index = 0; - int length = 0; - int[] temp = new int[oldArray.length]; - for(int i=0; iarray2[j]){ - merges[index] = array2[j]; - j++; - }else{ - merges[index] = array2[j]; - i++; - j++; - split++; - } - } - while(i0) { - StringBuffer stringBuffer = new StringBuffer(); - for (int i = 0; i < array.length; i++) { - stringBuffer.append(array[i]); - if (i != array.length - 1) { - stringBuffer.append(seperator); - } - } - return stringBuffer.toString(); - }else{ - return null; - } - } - -} diff --git a/group24/798277403/src/week2/array/ArrayUtilTest.java b/group24/798277403/src/week2/array/ArrayUtilTest.java deleted file mode 100644 index 77c99242fa..0000000000 --- a/group24/798277403/src/week2/array/ArrayUtilTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package week2.array; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.Random; - -/** - * Created by zhouliang on 2017-03-13. - */ -public class ArrayUtilTest { - private int[] array; - private ArrayUtil arrayUtil ; - private int SIZE = 11; - - @Before - public void setUp() throws Exception { - arrayUtil = new ArrayUtil(); - array = new int[SIZE]; - Random random = new Random(); - - for(int i=0; i 配置,以及execute的返回值, 确定哪一个jsp, -放到View对象的jsp字段中。 - -*/ -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = null; - Class actionClass = null; - LoginAction loginAction = null; - View view = new View(); - try { - db = documentBuilderFactory.newDocumentBuilder(); - Document document = db.parse("src/week2/litestruts/struts.xml"); - NodeList nodeList = document.getElementsByTagName("action"); - - //遍历每一个action节点 - for (int i = 0; i < nodeList.getLength(); i++) { - Node node = nodeList.item(i); - //获取action节点的所有属性集合 - NamedNodeMap attrs = node.getAttributes(); - //获取name结点的值 - String nodeName = attrs.getNamedItem("name").getNodeValue(); - - if(nodeName.equals(actionName)){ - //获取LoginAction实例 - actionClass = Class.forName(attrs.getNamedItem("class").getNodeValue()); - loginAction = (LoginAction) actionClass.newInstance(); - - //设置用户名密码属性 - Set> entrySet = parameters.entrySet(); - for (Map.Entry entry : entrySet) { - if (entry.getKey().equals("name")) { - loginAction.setName(entry.getValue()); - } - if (entry.getKey().equals("password")) { - loginAction.setPassword(entry.getValue()); - } - } - - //执行execute()方法 - String result = loginAction.execute(); - - //将message封装到view - String message = loginAction.getMessage(); - Map map = new HashMap(); - map.put("message",message); - view.setParameters(map); - - //解析对应的result节点 - NodeList childNodes = node.getChildNodes(); - //遍历childNodes获取每个节点的节点名和节点值 - for (int k = 0; k < childNodes.getLength(); k++) { - Node childNode = childNodes.item(k); - //区分出text类型的node以及element类型的node - if (childNode.getNodeType() == Node.ELEMENT_NODE) { - NamedNodeMap attributes = childNode.getAttributes(); - String nodeValue = attributes.getNamedItem("name").getNodeValue(); - if(nodeValue.equals(result)){ - view.setJsp(childNode.getTextContent()); - } - } - - } - - } - - } - } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { - e.printStackTrace(); - } - - return view; - } - -/* DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = null; - Class actionClass = null; - LoginAction loginAction = null; - try { - db = documentBuilderFactory.newDocumentBuilder(); - Document document = db.parse("src/week2/litestruts/struts.xml"); - NodeList nodeList = document.getElementsByTagName("action"); - System.out.println("一共有" + nodeList.getLength() + "个结点"); - //遍历每一个action节点 - for (int i = 0; i < nodeList.getLength(); i++) { - Node node = nodeList.item(i); - //获取action节点的所有属性集合 - NamedNodeMap attrs = node.getAttributes(); - //遍历action的属性 - for (int j = 0; j < attrs.getLength(); j++) { - //通过item(index)方法获取book节点的某一个属性 - Node attr = attrs.item(j); - String name = attrs.getNamedItem("name").getNodeValue(); - System.out.println("++++++++++"+name); - //获取属性名 - System.out.print("属性名:" + attr.getNodeName()); - //获取属性值 - System.out.println("--属性值" + attr.getNodeValue()); - if(attr.getNodeName().equals(actionName)){ - actionClass = Class.forName(attr.getNodeValue()); - loginAction = (LoginAction) actionClass.newInstance(); - } - } - //解析book节点的子节点 - NodeList childNodes = node.getChildNodes(); - //遍历childNodes获取每个节点的节点名和节点值 - for (int k = 0; k < childNodes.getLength(); k++) { - //区分出text类型的node以及element类型的node - if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { - //获取了element类型节点的节点名 - System.out.print(childNodes.item(k).getNodeName()); - //获取了element类型节点的节点值 - System.out.println("--节点值是:" + childNodes.item(k).getFirstChild().getNodeValue()); - System.out.println("--节点值是:" + childNodes.item(k).getTextContent()); - } - } - } - } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { - e.printStackTrace(); - }*/ -} \ No newline at end of file diff --git a/group24/798277403/src/week2/litestruts/StrutsTest.java b/group24/798277403/src/week2/litestruts/StrutsTest.java deleted file mode 100644 index 5c4379d912..0000000000 --- a/group24/798277403/src/week2/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package week2.litestruts; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/group24/798277403/src/week2/litestruts/View.java b/group24/798277403/src/week2/litestruts/View.java deleted file mode 100644 index 01a422a808..0000000000 --- a/group24/798277403/src/week2/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package week2.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/group24/798277403/src/week2/litestruts/struts.xml b/group24/798277403/src/week2/litestruts/struts.xml deleted file mode 100644 index 54550a4174..0000000000 --- a/group24/798277403/src/week2/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - diff --git a/group24/798277403/src/week3/DownloadThread.java b/group24/798277403/src/week3/DownloadThread.java deleted file mode 100644 index 6361f6b394..0000000000 --- a/group24/798277403/src/week3/DownloadThread.java +++ /dev/null @@ -1,50 +0,0 @@ -package week3; - - -import week3.api.Connection; - -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; - -class DownloadThread extends Thread{ - - private Connection conn; - private int startPos; - private int endPos; - private CyclicBarrier barrier; - private String localFile; - public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - this.localFile = localFile; - this.barrier = barrier; - } - public void run(){ - - - try { - System.out.println("Begin to read [" + startPos +"-"+endPos+"]"); - - byte[] data = conn.read(startPos, endPos); - - RandomAccessFile file = new RandomAccessFile(localFile,"rw"); - - file.seek(startPos); - - file.write(data); - - file.close(); - - conn.close(); - - barrier.await(); //等待别的线程完成 - - } catch (Exception e) { - e.printStackTrace(); - - } - - } -} diff --git a/group24/798277403/src/week3/FileDownloader.java b/group24/798277403/src/week3/FileDownloader.java deleted file mode 100644 index 912fc47df3..0000000000 --- a/group24/798277403/src/week3/FileDownloader.java +++ /dev/null @@ -1,116 +0,0 @@ -package week3; - - -import week3.api.Connection; -import week3.api.ConnectionManager; -import week3.api.DownloadListener; - -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; - -public class FileDownloader { - - private String url; - private String localFile; - - DownloadListener listener; - - ConnectionManager cm; - - - private static final int DOWNLOAD_TRHEAD_NUM = 3; - - public FileDownloader(String _url, String localFile) { - this.url = _url; - this.localFile = localFile; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - - CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ - public void run(){ - listener.notifyFinished(); - } - }); - - Connection conn = null; - try { - conn = cm.open(this.url); - int length = conn.getContentLength(); - createPlaceHolderFile(this.localFile, length); - int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); - for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){ - DownloadThread thread = new DownloadThread( - cm.open(url), - ranges[i][0], - ranges[i][1], - localFile, - barrier); - thread.start(); - } - } catch (Exception e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - } - - //往文件里面写0,占住磁盘 - private void createPlaceHolderFile(String fileName, int contentLen) throws IOException { - RandomAccessFile file = new RandomAccessFile(fileName,"rw"); - for(int i=0; i totalLen){ - byte[] data = baos.toByteArray(); - return Arrays.copyOf(data, totalLen); - } - return baos.toByteArray(); - } - - @Override - public int getContentLength() { - URLConnection con; - try { - con = url.openConnection(); - return con.getContentLength(); - } catch (IOException e) { - e.printStackTrace(); - } - return -1; - } - - @Override - public void close() { - - } - -} diff --git a/group24/798277403/src/week3/impl/ConnectionManagerImpl.java b/group24/798277403/src/week3/impl/ConnectionManagerImpl.java deleted file mode 100644 index 130d7e5aaa..0000000000 --- a/group24/798277403/src/week3/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,14 +0,0 @@ -package week3.impl; - -import week3.api.Connection; -import week3.api.ConnectionException; -import week3.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - return new ConnectionImpl(url); - } - -} diff --git a/group24/798277403/src/week3/linkedlist/LinkedList.java b/group24/798277403/src/week3/linkedlist/LinkedList.java deleted file mode 100644 index ad4560ad7d..0000000000 --- a/group24/798277403/src/week3/linkedlist/LinkedList.java +++ /dev/null @@ -1,360 +0,0 @@ -package week3.linkedlist; - - -/** - * 自己实现的LinkedList - * Created by zhouliang on 2017-03-10. - */ -class LinkedList implements List{ - - private int size; - private Node first; - private Node last; - - public LinkedList(){ - } - - @Override - public void add(E e) { - Node temp = new Node(e); - if(first != null){ - last.next = temp; - last = temp; - }else{ - first = temp; - last = temp; - } - size++; - } - - /** - * 指定下标添加元素 - * @param index 可以在链表末尾加,就是可以的等于size,不能大于size - * @param e 代表Element - */ - @Override - public void add(int index, E e) { - checkPositionIndex(index); - - Node temp = new Node(e); - if(index == size){ - last.next = temp; - last = temp; - }else{ - Node begin = first; - index--; - while(index>0){ - begin = begin.next; - index--; - } - Node next = begin.next; - begin.next = temp; - temp.next = next; - } - size++; - } - - @Override - public E get(int index) { - checkElementIndex(index); - - Node temp = first; - while(index>0){ - temp = temp.next; - index--; - } - return temp.value; - } - - @Override - public E remove(int index) { - checkElementIndex(index); - - Node temp; - if(index == 0){ - temp = first; - first = first.next; - size--; - return temp.value; - }else{ - temp = first; - index--; - //找到要删除节点的前一个节点 - while(index>0){ - temp = temp.next; - index--; - } - Node removeNode = temp.next; - temp.next = removeNode.next; - size--; - return removeNode.value; - - } - - } - - public E removeLast(){ - return remove(size-1); - } - - public void addFirst(E e){ - Node temp = new Node(e); - if(first == null){ - first = temp; - last = temp; - }else{ - temp.next = first; - first = temp; - } - size++; - } - - public void addLast(E e){ - Node temp = new Node(); - if(first == null){ - first = temp; - last = temp; - }else{ - last.next = temp; - last = temp; - } - size++; - } - - //检查index是否是合法的get下标 - private void checkElementIndex(int index) { - if (!isElementIndex(index)) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " - + size); - } - } - - private boolean isElementIndex(int index) { - return index >= 0 && index < size; - } - - //检查index是否是合法的add下标 - private void checkPositionIndex(int index) { - if (!isPositionIndex(index)) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " - + size); - } - } - - private boolean isPositionIndex(int index) { - return index >= 0 && index <= size; - } - - @Override - public int size() { - return size; - } - - private static class Node{ - E value; - Node next; - Node(){ - } - Node(E e){ - this.value = e; - } - } - - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - Node preNode = first; - - //头尾结点互换位置 - Node node = last; - last = first; - first = node; - - node = preNode.next; - Node nextNode; - - while (node != null) { - nextNode = node.next; - node.next = preNode; - preNode = node; - node = nextNode; - } - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - public void removeFirstHalf(){ - int num = this.size/2; - this.size = this.size - num; - while(num>0){ - //Node temp = first.next; - first = first.next; - num--; - } - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - checkPositionIndex(i); - if(length+i>size-1){ - throw new IndexOutOfBoundsException("Index: " + (i+length) + ", Size: " - + size); - } - int temp = 0; - Node newFirst = first; - Node beginNode = newFirst; - while(temp < i){ - beginNode = beginNode.next; - temp++; - } - Node endNode = beginNode.next; - size = size - length; - while(length>0){ - endNode = endNode.next; - length--; - } - first = newFirst; - beginNode.next = endNode; - } - - /** - * 假定当前链表和listB均包含已升序排列的整数 - * 从当前链表中取出那些listB所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - if(list==null || list.size()==0){ - return null; - }else{ - int[] result = new int[list.size()]; - int index = 0; - int length = 0; - Node temp = first; - for (int i=0; imin && (Integer)temp.value myLinkedList = new week3.linkedlist.LinkedList<>(); - - private java.util.LinkedList systemLinkedList = new java.util.LinkedList<>(); - - @Before - public void setUp(){ - for(int i=0; i<10; i++){ - myLinkedList.add(i); - systemLinkedList.add(i); - } - } - @Test - public void add() throws Exception { - for(int i=0; i<10; i++){ - System.out.println(myLinkedList.get(i)); - } - } - - @Test - public void reverse(){ - myLinkedList.reverse(); - for(int i=0; i<10; i++){ - System.out.println(myLinkedList.get(i)); - } - } - - @Test - public void removeFirstHalf(){ - myLinkedList.removeFirstHalf(); - System.out.println(myLinkedList.size()); - for(int i=0; i list = new LinkedList(); - list.add(0); - list.add(7); - list.add(9); - int[] reuslt = myLinkedList.getElements(list); - System.out.println(reuslt.length); - for(int i=0; i list = new LinkedList(); - list.add(0); - list.add(7); - list.add(9); - myLinkedList.subtract(list); - for(int i=0; i list = new LinkedList(); - list.add(0); - list.add(2); - list.add(9); - list.add(9); - list.add(9); - list.add(9); - list.add(9); - list.add(9); - list.add(9); - - - - LinkedList result = myLinkedList.intersection(list); - for(int i=0; i { - void add(E e); - void add(int index, E e); - E get(int index); - E remove(int index); - int size(); -} diff --git a/group24/798277403/src/week3/test/ConnectionTest.java b/group24/798277403/src/week3/test/ConnectionTest.java deleted file mode 100644 index 918f4d0707..0000000000 --- a/group24/798277403/src/week3/test/ConnectionTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package week3.test; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import week3.api.Connection; -import week3.api.ConnectionManager; -import week3.impl.ConnectionManagerImpl; - - -public class ConnectionTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testContentLength() throws Exception{ - ConnectionManager connMan = new ConnectionManagerImpl(); - Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); - Assert.assertEquals(35470, conn.getContentLength()); - } - - @Test - public void testRead() throws Exception{ - - ConnectionManager connMan = new ConnectionManagerImpl(); - Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); - - byte[] data = conn.read(0, 35469); - - Assert.assertEquals(35470, data.length); - - data = conn.read(0, 1023); - - Assert.assertEquals(1024, data.length); - - data = conn.read(1024, 2023); - - Assert.assertEquals(1000, data.length); - - - // 测试不充分,没有断言内容是否正确 - } - - -} diff --git a/group24/798277403/src/week3/test/FileDownloaderTest.java b/group24/798277403/src/week3/test/FileDownloaderTest.java deleted file mode 100644 index 959796399b..0000000000 --- a/group24/798277403/src/week3/test/FileDownloaderTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package week3.test; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import week3.FileDownloader; -import week3.api.ConnectionManager; -import week3.api.DownloadListener; -import week3.impl.ConnectionManagerImpl; - - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - //String url = "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; - - String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; - - FileDownloader downloader = new FileDownloader(url,"C:\\Users\\zhouliang\\Desktop\\mycoding\\test.jpg"); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/group24/798277403/src/week4/LRU/LRUPageFrame.java b/group24/798277403/src/week4/LRU/LRUPageFrame.java deleted file mode 100644 index cbbb26fc7f..0000000000 --- a/group24/798277403/src/week4/LRU/LRUPageFrame.java +++ /dev/null @@ -1,130 +0,0 @@ -package week4.LRU; - -/** - * Created by zhouliang on 2017-04-04. - */ -public class LRUPageFrame { - private static class Node { - Node prev; - Node next; - int pageNum; - Node() { - } - } - - private int capacity; - - private int currentSize; - private Node first;// 链表头 - private Node last;// 链表尾 - - - public LRUPageFrame(int capacity) { - this.currentSize = 0; - this.capacity = capacity; - } - - /** - * 获取缓存中对象 - * @param pageNum - * @return - */ - public void access(int pageNum) { - Node node = find(pageNum); - //在该队列中存在, 则提到队列头 - if (node != null) { - moveExistingNodeToHead(node); - } else{ - node = new Node(); - node.pageNum = pageNum; - // 缓存容器是否已经超过大小. - if (currentSize >= capacity) { - removeLast(); - } - addNewNodetoHead(node); - } - } - - private void addNewNodetoHead(Node node) { - if(isEmpty()){ - node.prev = null; - node.next = null; - first = node; - last = node; - - } else{ - node.prev = null; - node.next = first; - first.prev = node; - first = node; - } - this.currentSize ++; - } - - private Node find(int data){ - Node node = first; - while(node != null){ - if(node.pageNum == data){ - return node; - } - node = node.next; - } - return null; - - } - - /** - * 删除链表尾部节点 表示 删除最少使用的缓存对象 - */ - private void removeLast() { - Node prev = last.prev; - prev.next = null; - last.prev = null; - last = prev; - this.currentSize --; - } - - /** - * 移动到链表头,表示这个节点是最新使用过的 - * - * @param node - */ - private void moveExistingNodeToHead(Node node) { - if (node == first) { - return; - } - else if(node == last){ - //当前节点是链表尾, 需要放到链表头 - Node prevNode = node.prev; - prevNode.next = null; - last.prev = null; - last = prevNode; - } else{ - //node 在链表的中间, 把node 的前后节点连接起来 - Node prevNode = node.prev; - prevNode.next = node.next; - Node nextNode = node.next; - nextNode.prev = prevNode; - } - node.prev = null; - node.next = first; - first.prev = node; - first = node; - } - private boolean isEmpty(){ - return (first == null) && (last == null); - } - - public String toString(){ - StringBuilder buffer = new StringBuilder(); - Node node = first; - while(node != null){ - buffer.append(node.pageNum); - node = node.next; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } -} diff --git a/group24/798277403/src/week4/LRU/LRUPageFrameTest.java b/group24/798277403/src/week4/LRU/LRUPageFrameTest.java deleted file mode 100644 index 0b6bdf2c25..0000000000 --- a/group24/798277403/src/week4/LRU/LRUPageFrameTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package week4.LRU; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Created by zhouliang on 2017-04-04. - */ -public class LRUPageFrameTest { - @Test - public void testAccess() { - MyLRUPageFrame frame = new MyLRUPageFrame(3); - frame.access(7); - frame.access(0); - frame.access(1); - Assert.assertEquals("1,0,7", frame.toString()); - frame.access(2); - Assert.assertEquals("2,1,0", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(3); - Assert.assertEquals("3,0,2", frame.toString()); - frame.access(0); - Assert.assertEquals("0,3,2", frame.toString()); - frame.access(4); - Assert.assertEquals("4,0,3", frame.toString()); - frame.access(5); - Assert.assertEquals("5,4,0", frame.toString()); - - } -} diff --git a/group24/798277403/src/week4/loader/ClassFileLoader.java b/group24/798277403/src/week4/loader/ClassFileLoader.java deleted file mode 100644 index 90bce86e77..0000000000 --- a/group24/798277403/src/week4/loader/ClassFileLoader.java +++ /dev/null @@ -1,67 +0,0 @@ -package week4.loader; - -import java.io.*; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by zhouliang on 2017-04-04. - */ -public class ClassFileLoader { - private List clzPaths = new ArrayList(); - - public byte[] readBinaryCode(String className) { - className = className.replace(".","\\"); - if(!className.endsWith(".class")){ - className=className+".class"; - } - File file = null; - for(String path : clzPaths){ - file = new File(path+"\\"+className); - if(file.exists()){ - break; - } - } - - if(file==null){ - try { - throw new FileNotFoundException(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - return null; - }else{ - byte[] buffer = new byte[1024]; - BufferedInputStream bufferedInputStream = null; - ByteArrayOutputStream byteArrayOutputStream = null; - try { - bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); - byteArrayOutputStream = new ByteArrayOutputStream(); - int length = 0; - while((length = bufferedInputStream.read(buffer))!= -1){ - byteArrayOutputStream.write(buffer,0,length); - } - } catch (IOException e) { - e.printStackTrace(); - } - return byteArrayOutputStream.toByteArray(); - } - } - - - public void addClassPath(String path) { - clzPaths.add(path); - } - - public String getClassPath(){ - StringBuilder stringBuilder = new StringBuilder(); - for(int i=0; i list = new ArrayList(); + for(int i=0;i list = new ArrayList(); + for(int i =0;i list = new ArrayList(); + for(int i=2;i<=max;i++){ + if(isPrime(i)){ + list.add(i); + } + } + + return listToArray(list); + + + } + + public static int[] listToArray(List list){ + if(list == null){ + return null; + } + + int[] arr = new int[list.size()]; + + for(int i=0;i list = new ArrayList(); + for(int i=1;i<=max;i++){ + if(isPerfectNum(i)){ + list.add(i); + } + } + + return listToArray(list); + } + + + public static boolean isPerfectNum(int num){ + if(num <=1){ + return false; + } + + List factors = getFactor(num); + int sum = 0; + for (Integer integer : factors) { + sum = integer+sum; + } + if(sum == num){ + return true; + } + return false; + } + + public static List getFactor(int num){ + List list = new ArrayList(); + list.add(1); + + + for(int i=2;i getFactor(int num){ + List list = new ArrayList(); + list.add(1); + int temp = num; + + while(!isPrime(temp)){ + if(temp ==1){ + break; + } + for(int i=2;i<=temp;i++){ + if(temp % i ==0){ + list.add(i); + temp = temp/i; + break; + } + } + + } + list.add(temp); + + return list; + }*/ + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + + } + + return sb.subSequence(0, sb.length()-1).toString(); + } + + +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..550d47ec2b --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,137 @@ +package com.coderising.litestruts; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import org.jdom2.output.XMLOutputter; + +import com.sun.istack.internal.Builder; + + + +public class Struts { + + @SuppressWarnings("deprecation") + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + String xmlpath = Struts.class.getResource("struts.xml").getFile(); + SAXBuilder builder = null; + try { + xmlpath = URLDecoder.decode(xmlpath, "utf-8"); + builder = new SAXBuilder(false); + Document doc = builder.build(xmlpath); + Element root = doc.getRootElement(); + Element action = root.getChild("action"); + String className = action.getAttributeValue("class"); + Class clazz = Class.forName(className); + Object logAction = clazz.newInstance(); + for(String key :parameters.keySet()){ + String methodName = "set"+ Struts.captureName(key); + Method method = null; + try { + method = clazz.getMethod(methodName, String.class); + } catch (SecurityException e) { + e.printStackTrace(); + break; + } catch (NoSuchMethodException e) { + break; + } + method.invoke(logAction, parameters.get(key)); + + } + Method executeMethod = clazz.getMethod("execute", (Class[])null); + String result = (String)executeMethod.invoke(logAction, (Object[])null); + Method[] declareMtds = clazz.getDeclaredMethods(); + Map paramForView = new HashMap(); + for (Method method : declareMtds) { + String methodName = method.getName(); + if(methodName.startsWith("get")){ + paramForView.put(methodName.substring(3).toLowerCase(), (String)method.invoke(logAction, (Object[])null)); + } + + } + view.setParameters(paramForView); + List results = action.getChildren("result"); + for (Element element : results) { + String resultName = element.getAttributeValue("name"); + if(result.equals(resultName)){ + view.setJsp(element.getText()); + } + } + + } catch (JDOMException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return view; + } + + + public static String captureName(String name) { + // name = name.substring(0, 1).toUpperCase() + name.substring(1); +// return name; + char[] cs=name.toCharArray(); + cs[0]-=32; + return String.valueOf(cs); + + + } + +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java b/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..e5fec6fceb --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * @author hugaoqing + * created on 2017-3-8 + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[3]; + + /* + * 添加元素 + * + */ + public void add(Object o){ + /*if(elementData.length == size){ + Object[] buffer = new Object[size+15]; + System.arraycopy(elementData, 0, buffer, 0, size); + elementData = buffer; + elementData[size] = o; + size++; + }else { + + elementData[size] = o; + size++; + }*/ + add(size, o); + + } + + + /* + * + * 指定位置添加元素 + */ + public void add(int index, Object o){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException(); + } + if(size+1=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException(); + } + Object out = elementData[index]; + Object[] temp = new Object[size-index-1]; + System.arraycopy(elementData, index+1, temp, 0, size-index-1); + System.arraycopy(temp, 0, elementData,index, size-index-1); + //将移除的元素的指针去掉,避免内存泄漏 + elementData[size-1] = null; + size--; + return out; + } + + public int size(){ + return this.size; + } + + @Override + public String toString() { + Object[] objs = new Object[size]; + System.arraycopy(elementData, 0,objs , 0, size); + return Arrays.toString(objs); + + } + public Iterator iterator(){ + return new Iterator() { + int cursor = 0; + public Object next() throws Exception { + cursor++; + return get(cursor-1); + } + + public boolean hasNext() { + + return this.cursor0){ + while(curNode.getRight()!=null){ + curNode = curNode.getRight(); + } + curNode = node; + + }else{ + while(curNode.getLeft()!=null){ + curNode = curNode.getLeft(); + } + curNode = node; + } + + } + return null; + } + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java b/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4bd92572c9 --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,109 @@ +package com.coding.basic; + + + +/** + * @author hugaoqing + * created on 2017-3-11 + */ +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Comparable data2) { + // TODO Auto-generated constructor stub + } + public BinaryTreeNode() { + // TODO Auto-generated constructor stub + } + /*public BinaryTreeNode(Comparable data) { + super(); + this.data = data; + }*/ + public Comparable getData() { + return data; + } + public void setData(Comparable data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Comparable data){ + /*if(this.data==null){ + return new BinaryTreeNode(o); + } + + BinaryTreeNode curNode = this; + while(curNode != null){ + if(curNode.data.compareTo(o) == 0){ + return curNode; + } + else if(o.compareTo(curNode.data) < 0){ + BinaryTreeNode node = curNode; + curNode = curNode.left; + if(curNode == null){ + curNode = new BinaryTreeNode(o); + node.left = curNode; + return curNode; + } + + + }else if(o.compareTo(curNode.data) > 0){ + BinaryTreeNode node = curNode; + curNode = curNode.right; + if(curNode == null){ + curNode = new BinaryTreeNode(o); + node.right = curNode; + return curNode; + } + } + } + return curNode;*/ + BinaryTreeNode curNode = this; + BinaryTreeNode insertNode = new BinaryTreeNode(); + insertNode.setData(data); + + while(curNode != null){ + if(null == curNode.data){ + curNode.setData(data); + break; + }else{ + Comparable dataOfNode = curNode.getData(); + if(dataOfNode.compareTo(data) == 0){ + break; + }else if(dataOfNode.compareTo(data) < 0){ + BinaryTreeNode leftNode = curNode.left; + if(null == leftNode){ + curNode.setLeft(insertNode); + break; + } + curNode = leftNode; + }else{ + BinaryTreeNode rightNode = curNode.right; + if(null == rightNode){ + curNode.setRight(insertNode); + break; + } + curNode = rightNode; + } + } + } + return insertNode; + + + } + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java b/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d4656a7daf --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator { + //ӿijԱĬ϶final static +// int cursor = 0; + public boolean hasNext(); + public Object next() throws Exception; + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3d7e06e19f --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,223 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + + public void add(Object o){ + this.addLast(o); + + } + public void add(int index , Object o){ + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } + if(index==0){ + this.addFirst(o); + size++; + return; + }else if(index==size){ + this.addLast(o); + size++; + return; + } + Node preNode = this.getNode(index-1); + Node curNode = this.getNode(index); + Node newNode = new Node(o, curNode); + preNode.next = newNode; + + + size++; + + + } + + private Node getNode(int index){ + if(index <0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + if(index ==0){ + return head; + } + Node curNode = head; + for(int i=1;i<=index;i++){ + curNode = head.next; + } + return curNode; + } + + public Object get(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; + } + return temp.data; + } + public Object remove(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + Object o = null; + if(size == 1){ + o = head.data; + size--; + return o; + } + if(index==0){ + o = head.data; + Node afterHead = head.next; + head = afterHead; + + }else if(index==size-1){ + Node preTail = getNode(index-1); + Node tail = preTail.next; + o = tail.data; + preTail.next = null; + }else{ + Node preCur = getNode(index-1); + Node cur = preCur.next; + Node nextCur = cur.next; + o = cur.data; + preCur.next = nextCur; + + } + size--; + return o; + + + + + + + + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(o,null); + + if(head == null){ + head = node; + size++; + return; + } + head = new Node(o, head); + size++; + + } + public void addLast(Object o){ + //½ڵnextָָtail + Node add = new Node(o, null); + if(head==null){ + head = add; + size++; + return; + } + Node curNode = head; + while(curNode.next != null){ + curNode = curNode.next; + } + + curNode.next = add; + size++; + } + + + public Object removeFirst() throws Exception{ + return this.remove(0); + } + public Object removeLast() throws Exception{ + return this.remove(size-1); + } + + private class Itr implements Iterator{ + int cursor = 0; + public boolean hasNext() { + return cursor list=new ArrayList(); int i=0; int j=0; diff --git a/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java b/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java index 7b0d3c94e0..f560b2d658 100644 --- a/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java +++ b/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java @@ -1,5 +1,5 @@ -package week2.test; +package week2.arrayutil; import static org.junit.Assert.*; diff --git a/group26/1515345281/src/week2/blog b/group26/1515345281/src/week2/blog new file mode 100644 index 0000000000..19c1272ec0 --- /dev/null +++ b/group26/1515345281/src/week2/blog @@ -0,0 +1 @@ +数筛法的具体应用--素数求解问题:http://blog.csdn.net/sjshenjian/article/details/68943399 \ No newline at end of file diff --git a/group26/1515345281/src/week2/struts2/Configuration.java b/group26/1515345281/src/week2/struts2/Configuration.java new file mode 100644 index 0000000000..ddb27cc229 --- /dev/null +++ b/group26/1515345281/src/week2/struts2/Configuration.java @@ -0,0 +1,122 @@ +package week2.struts2; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + + +/** + * 配置解析struts.xml文件 + * @author Administrator + * + */ +public class Configuration { + + Map actions=new HashMap<>(); + + public Configuration(String fileName) { + + //获取当前类包名 + String packageName=this.getClass().getPackage().getName(); + + packageName=packageName.replace(".", "/"); + //获取文件流 + InputStream input=this.getClass().getResourceAsStream("/"+packageName+"/"+fileName); + + parseXml(input); + + try{ + input.close(); + }catch(IOException e){ + throw new ConfigurationException(e); + } + } + + private void parseXml(InputStream input) { + + SAXBuilder builder=new SAXBuilder(); + + try { + Document document=builder.build(input); + + Element root=document.getRootElement(); + + for(Element actionElement:root.getChildren("action")){ + + String actionName=actionElement.getAttributeValue("name"); + String clazzName=actionElement.getAttributeValue("class"); + + ActionConfig ac=new ActionConfig(actionName,clazzName); + + for(Element resultElement:actionElement.getChildren("result")){ + + String resultName=resultElement.getAttributeValue("name"); + String viewName=resultElement.getText().trim(); + + ac.setViewResult(resultName, viewName); + } + + actions.put(actionName, ac); + } + + } catch (JDOMException e) { + throw new ConfigurationException(e); + } catch (IOException e) { + throw new ConfigurationException(e); + } + + } + + public String getClassName(String actionName) { + + ActionConfig ac=this.actions.get(actionName); + + if(null == ac){ + return null; + } + + return ac.getClazzName(); + } + + public String getResultView(String actionName, String resultName) { + + ActionConfig ac=this.actions.get(actionName); + + if(null == ac){ + return null; + } + + return ac.getViewResult(resultName); + } + + private static class ActionConfig{ + + String actionName; + String clazzName; + Map viewResults=new HashMap<>(); + + public ActionConfig(String actionName,String clazzName){ + this.actionName=actionName; + this.clazzName=clazzName; + } + + public String getClazzName() { + return clazzName; + } + + public void setViewResult(String name,String viewName){ + viewResults.put(name, viewName); + } + + public String getViewResult(String name){ + String viewName=viewResults.get(name); + return viewName; + } + } +} diff --git a/group26/1515345281/src/week2/struts2/ConfigurationException.java b/group26/1515345281/src/week2/struts2/ConfigurationException.java new file mode 100644 index 0000000000..842ca1866b --- /dev/null +++ b/group26/1515345281/src/week2/struts2/ConfigurationException.java @@ -0,0 +1,20 @@ +package week2.struts2; + +import java.io.IOException; + +import org.jdom2.JDOMException; + +public class ConfigurationException extends RuntimeException{ + + public ConfigurationException(String msg){ + super(msg); + } + + public ConfigurationException(JDOMException e){ + super(e); + } + + public ConfigurationException(IOException e){ + super(e); + } +} diff --git a/group26/1515345281/src/week2/struts2/LoginAction.java b/group26/1515345281/src/week2/struts2/LoginAction.java index 0a34e92f18..7bfaee9134 100644 --- a/group26/1515345281/src/week2/struts2/LoginAction.java +++ b/group26/1515345281/src/week2/struts2/LoginAction.java @@ -1,39 +1,43 @@ package week2.struts2; -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } +public class LoginAction { + private String userName; + private String password; + private String message; + + + public String excute(){ + if("沈健".equals(userName) && "123456".equals(password)){ + this.message="login successful"; + return "success"; + } + + this.message="login failed,please check your user/pwd"; + return "fail"; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + } diff --git a/group26/1515345281/src/week2/struts2/ReflectionUtil.java b/group26/1515345281/src/week2/struts2/ReflectionUtil.java new file mode 100644 index 0000000000..0c9890019b --- /dev/null +++ b/group26/1515345281/src/week2/struts2/ReflectionUtil.java @@ -0,0 +1,88 @@ +package week2.struts2; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + /** + * 反射赋值 + * @param o:类对象 + * @param params:用户信息 + */ + public static void setParameters(Object o, Map params){ + + List Methods=getSetterMethods(o.getClass()); + + for(String name:params.keySet()){ + + String methodName="set"+name; + + for(Method method:Methods){ + + if(method.getName().equalsIgnoreCase(methodName)){ + try { + method.invoke(o, params.get(name)); + } catch (IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } + + public static Map getParamterMap(Object o){ + + Map params=new HashMap<>(); + + List methods=getGetterMethods(o.getClass()); + + for(Method method:methods){ + + String name=method.getName().replaceFirst("get", "").toLowerCase(); + + try { + Object object=method.invoke(o); + params.put(name, object); + } catch (IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + e.printStackTrace(); + } + } + + return params; + } + + public static List getSetterMethods(Class clazz){ + return getMethod(clazz,"set"); + } + + public static List getGetterMethods(Class clazz){ + return getMethod(clazz,"get"); + } + + /** + *反射获取给定名开始的方法 + * @param clazz + * @param startWithName + * @return + */ + private static List getMethod(Class clazz, String startWithName) { + + List methods=new ArrayList(); + + for(Method method:clazz.getDeclaredMethods()){ + + if(method.getName().startsWith(startWithName)){ + methods.add(method); + } + } + + return methods; + } +} diff --git a/group26/1515345281/src/week2/struts2/Struts.java b/group26/1515345281/src/week2/struts2/Struts.java index 06edfe7e20..caca81418e 100644 --- a/group26/1515345281/src/week2/struts2/Struts.java +++ b/group26/1515345281/src/week2/struts2/Struts.java @@ -1,34 +1,63 @@ package week2.struts2; +import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - - return null; - } + private static final Configuration config = new Configuration("struts.xml"); + + public static View runAction(String actionName, + Map parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - 3. + * 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + + String clazzName= config.getClassName(actionName); + if (clazzName == null) { + return null; + } + + try { + + Class clazz=Class.forName(clazzName); + Object action=clazz.newInstance(); + + ReflectionUtil.setParameters(action,parameters); + + Method method=clazz.getDeclaredMethod("excute"); + String result=(String) method.invoke(action); + + Map params=new HashMap<>(); + params=ReflectionUtil.getParamterMap(action); + + String resultView=config.getResultView(actionName, result); + + View view=new View(); + view.setJsp(resultView); + view.setParameters(params); + + return view; + + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } } diff --git a/group26/1515345281/src/week2/struts2/StrutsTest.java b/group26/1515345281/src/week2/struts2/StrutsTest.java index 68ec3ee4b4..376f7fd431 100644 --- a/group26/1515345281/src/week2/struts2/StrutsTest.java +++ b/group26/1515345281/src/week2/struts2/StrutsTest.java @@ -1,5 +1,7 @@ package week2.struts2; +import static org.junit.Assert.*; + import java.util.HashMap; import java.util.Map; @@ -11,30 +13,27 @@ public class StrutsTest { @Test public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); + String actionName="login"; + Map params=new HashMap<>(); + + params.put("userName", "沈健"); + params.put("password", "123456"); + + View view=Struts.runAction(actionName, params); + assertEquals("/jsp/homepage.jsp", view.getJsp()); + assertEquals("login successful", view.getParameters().get("message")); } @Test public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + String actionName="login"; + Map params=new HashMap<>(); + + params.put("name", "沈健"); + params.put("password", "1234565"); + + View view=Struts.runAction(actionName, params); + assertEquals("/jsp/showLogin.jsp", view.getJsp()); + assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); } } diff --git a/group26/1515345281/src/week2/struts2/View.java b/group26/1515345281/src/week2/struts2/View.java index 5a907a455f..7017ab58ab 100644 --- a/group26/1515345281/src/week2/struts2/View.java +++ b/group26/1515345281/src/week2/struts2/View.java @@ -13,6 +13,7 @@ public View setJsp(String jsp) { this.jsp = jsp; return this; } + public Map getParameters() { return parameters; } diff --git a/group26/1515345281/src/week2/struts2/struts.xml b/group26/1515345281/src/week2/struts2/struts.xml index 9e69fa4e47..f2fb0166e0 100644 --- a/group26/1515345281/src/week2/struts2/struts.xml +++ b/group26/1515345281/src/week2/struts2/struts.xml @@ -5,8 +5,9 @@ /jsp/showLogin.jsp - - /jsp/welcome.jsp + + /jsp/welcome.jsp /jsp/error.jsp + \ No newline at end of file diff --git a/group26/1515345281/src/week2/struts2/test/ConfigurationTest.java b/group26/1515345281/src/week2/struts2/test/ConfigurationTest.java new file mode 100644 index 0000000000..dbbaf30eee --- /dev/null +++ b/group26/1515345281/src/week2/struts2/test/ConfigurationTest.java @@ -0,0 +1,40 @@ +package week2.struts2.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week2.struts2.Configuration; + +public class ConfigurationTest { + + Configuration config=new Configuration("struts.xml"); + + @Test + public void testGetClassName(){ + + String clazzName=config.getClassName("login"); + assertEquals("week2.struts2.LoginAction",clazzName); + + clazzName=config.getClassName("logout"); + assertEquals("week2.struts2.LoginOutAction",clazzName); + + clazzName=config.getClassName("logoutf"); + } + + @Test + public void testGetResultView(){ + + String jsp=config.getResultView("login","success"); + assertEquals("/jsp/homepage.jsp",jsp); + + jsp=config.getResultView("login", "fail"); + assertEquals("/jsp/showLogin.jsp",jsp); + + jsp=config.getResultView("logout", "success"); + assertEquals("/jsp/welcome.jsp",jsp); + + jsp=config.getResultView("logout", "error"); + assertEquals("/jsp/error.jsp",jsp); + } +} diff --git a/group26/1515345281/src/week2/struts2/test/ReflectionTest.java b/group26/1515345281/src/week2/struts2/test/ReflectionTest.java new file mode 100644 index 0000000000..44bebb2343 --- /dev/null +++ b/group26/1515345281/src/week2/struts2/test/ReflectionTest.java @@ -0,0 +1,110 @@ +package week2.struts2.test; + +import static org.junit.Assert.*; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week2.struts2.LoginAction; +import week2.struts2.ReflectionUtil; + +public class ReflectionTest { + + @Test + public void testGetSetterMethods() throws ClassNotFoundException { + + String clazzName = "week2.struts2.LoginAction"; + + Class clazz = Class.forName(clazzName); + + List methods = ReflectionUtil.getSetterMethods(clazz); + + assertEquals(3, methods.size()); + + List expectedNames = new ArrayList(); + expectedNames.add("setUserName"); + expectedNames.add("setPassword"); + expectedNames.add("setMessage"); + + Set actualsNames = new HashSet<>(); + for (Method m : methods) { + actualsNames.add(m.getName()); + } + + assertTrue(actualsNames.containsAll(expectedNames)); + } + + @Test + public void testGetGetterMethods() throws ClassNotFoundException { + + String clazzName = "week2.struts2.LoginAction"; + Class clazz = Class.forName(clazzName); + List methods = ReflectionUtil.getGetterMethods(clazz); + + assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getUserName"); + expectedNames.add("getPassword"); + expectedNames.add("getMessage"); + + Set actualNames = new HashSet<>(); + for (Method m : methods) { + actualNames.add(m.getName()); + } + + assertTrue(actualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception { + + String clazzName = "week2.struts2.LoginAction"; + Class clazz = Class.forName(clazzName); + + Object o = clazz.newInstance(); + Map params = new HashMap<>(); + params.put("userName", "沈健"); + params.put("password", "123456"); + + ReflectionUtil.setParameters(o, params); + + Field userName = clazz.getDeclaredField("userName"); + userName.setAccessible(true); + assertEquals(userName.get(o), "沈健"); + + Field password = clazz.getDeclaredField("password"); + password.setAccessible(true); + assertEquals(password.get(o), "123456"); + + } + + @Test + public void testGetParameterMap() throws Exception { + + String clazzName = "week2.struts2.LoginAction"; + Class clazz = Class.forName(clazzName); + LoginAction action = (LoginAction) clazz.newInstance(); + action.setUserName("沈健"); + action.setPassword("123456"); + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage")); + Assert.assertEquals("沈健", params.get("username")); + Assert.assertEquals("123456", params.get("password")); + } + +} diff --git a/group26/1515345281/src/week3/blog b/group26/1515345281/src/week3/blog new file mode 100644 index 0000000000..2c4d2cdc00 --- /dev/null +++ b/group26/1515345281/src/week3/blog @@ -0,0 +1 @@ +图片验证码生成 : http://blog.csdn.net/sjshenjian/article/details/68943294 \ No newline at end of file diff --git a/group26/1515345281/src/week3/download/DownloadThread.java b/group26/1515345281/src/week3/download/DownloadThread.java new file mode 100644 index 0000000000..21e29bac91 --- /dev/null +++ b/group26/1515345281/src/week3/download/DownloadThread.java @@ -0,0 +1,57 @@ +package week3.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +import week3.download.api.Connection; + +public class DownloadThread extends Thread { + + Connection conn; + int startPos; + int endPos; + String localFile; + // 它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。 + // 在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待 + CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos, + String localFile, CyclicBarrier barrier) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + @Override + public void run() { + + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + + try { + byte[] data = conn.read(startPos, endPos); + + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + + file.seek(startPos); + + file.write(data); + + file.close(); + + conn.close(); + + barrier.await();//等待别的线程完成 + + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } + } +} diff --git a/group26/1515345281/src/week3/download/FileDownloader.java b/group26/1515345281/src/week3/download/FileDownloader.java new file mode 100644 index 0000000000..467c95843a --- /dev/null +++ b/group26/1515345281/src/week3/download/FileDownloader.java @@ -0,0 +1,106 @@ +package week3.download; + +import java.net.URL; +import java.util.concurrent.CyclicBarrier; + +import week3.download.api.Connection; +import week3.download.api.ConnectionException; +import week3.download.api.ConnectionManager; +import week3.download.api.DownloadListener; +import week3.download.api.impl.ConnectionManagerImpl; + +public class FileDownloader { + + private String url; + private String localFile; + private static final int DOWNLOAD_THREAD_NUM = 6; + + private DownloadListener listener; + + public FileDownloader(String url, String localFile) { + this.url = url; + this.localFile = localFile; + } + + public void execute() { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + // 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM, + new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + + ConnectionManager connManager = new ConnectionManagerImpl(); + + Connection conn = null; + + try { + conn = connManager.open(url); + int totalLen = conn.getContentLength(); + + int[][] range=allocateDownloadRange(DOWNLOAD_THREAD_NUM,totalLen); + + for (int i = 0; i < DOWNLOAD_THREAD_NUM; i++) { + + DownloadThread thread = new DownloadThread(connManager.open(url), range[i][0], + range[i][1], localFile, barrier); + + thread.start(); + } + + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + private int[][] allocateDownloadRange(int threadNum,int totalLen){ + + int[][] range=new int[threadNum][2]; + + int eachThreadSize=totalLen/threadNum; + + int left=totalLen%threadNum;//剩余的由最后一个线程处理 + + for(int i=0;i totalLen){ + byte[] data=baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + +} diff --git a/group26/1515345281/src/week3/download/api/impl/ConnectionManagerImpl.java b/group26/1515345281/src/week3/download/api/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..47bfcc62d0 --- /dev/null +++ b/group26/1515345281/src/week3/download/api/impl/ConnectionManagerImpl.java @@ -0,0 +1,17 @@ +package week3.download.api.impl; + +import java.io.IOException; + +import week3.download.api.Connection; +import week3.download.api.ConnectionException; +import week3.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group26/1515345281/src/week3/download/test/ConnectionTest.java b/group26/1515345281/src/week3/download/test/ConnectionTest.java new file mode 100644 index 0000000000..36bb9d1761 --- /dev/null +++ b/group26/1515345281/src/week3/download/test/ConnectionTest.java @@ -0,0 +1,52 @@ +package week3.download.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week3.download.api.Connection; +import week3.download.api.ConnectionManager; +import week3.download.api.impl.ConnectionManagerImpl; + + +public class ConnectionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testContentLength() throws Exception{ + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://pic.sc.chinaz.com/files/pic/pic9/201508/apic14052.jpg"); + Assert.assertEquals(112504, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception{ + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://pic.sc.chinaz.com/files/pic/pic9/201508/apic14052.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + + // 测试不充分,没有断言内容是否正确 + } + +} diff --git a/group26/1515345281/src/week3/download/test/FileDownloaderTest.java b/group26/1515345281/src/week3/download/test/FileDownloaderTest.java new file mode 100644 index 0000000000..7da91ce6c6 --- /dev/null +++ b/group26/1515345281/src/week3/download/test/FileDownloaderTest.java @@ -0,0 +1,45 @@ +package week3.download.test; + +import org.junit.Test; + +import week3.download.FileDownloader; +import week3.download.api.DownloadListener; + +public class FileDownloaderTest { + + boolean downloadFinished = false; + + @Test + public void testFileDownloader() { + + /*String url = "http://210.43.133.109:9999/dldir1.qq.com/qqfile/qq/QQ8.9.1/20437/QQ8.9.1.exe"; + String localFile = "e://qq8.exe";*/ + String url="http://www.iqiyi.com/common/flashplayer/20170331/036801ea7a2e24.swf"; + String localFile="e:\\036801ea7a2e24.swf"; + long begin=System.currentTimeMillis(); + FileDownloader downloader = new FileDownloader(url, localFile); + downloader.setListener(new DownloadListener() { + + @Override + public void notifyFinished() {// + downloadFinished = true; + } + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + long end=System.currentTimeMillis(); + + long cost=(end-begin)/1000; + + System.out.println("下载完成!时间为"+cost+"秒"); + } +} \ No newline at end of file diff --git a/group26/1515345281/src/week3/list/Iterator.java b/group26/1515345281/src/week3/list/Iterator.java new file mode 100644 index 0000000000..4939cb8073 --- /dev/null +++ b/group26/1515345281/src/week3/list/Iterator.java @@ -0,0 +1,5 @@ +package week3.list; +public interface Iterator{ + public boolean hasNext(); + public Object next(); +} \ No newline at end of file diff --git a/group26/1515345281/src/week3/list/LinkedList.java b/group26/1515345281/src/week3/list/LinkedList.java new file mode 100644 index 0000000000..2832694aa9 --- /dev/null +++ b/group26/1515345281/src/week3/list/LinkedList.java @@ -0,0 +1,363 @@ +package week3.list; + +import java.util.Stack; + + +public class LinkedList implements List{ + + private int size=0;//表示该链表的长度 + private Node head;//链表的头元素 + + public void add(Object o){ + if(null == head){ + head = new Node(o); + size++; + return ; + } + + Node node=head; + while(null != node.next){ + node=node.next; + } + Node addNode=new Node(o); + node.next=addNode; + size++; + } + + public void add(int index , Object o){ + if(size == 0 || index ==size){ + add(o); + return ; + } + + ListUtils.checkIndexRange(index, size); + + if(index==0){ + Node node=new Node(head.next.data); + node.next=head.next; + head.next=node; + head.data=o; + size++; + return ; + } + + Node node=head; + for(int i=0;i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + if(null == head || null ==head.next) + return ; + + Stack stack=new Stack(); + + Node currentNode=head; + + while(currentNode!=null){ + + stack.push(currentNode); + + Node tempNode=currentNode.next; + currentNode.next=null;//断开连接 + + currentNode=tempNode; + + } + + head=stack.pop(); + currentNode=head; + + while(!stack.isEmpty()){ + + currentNode.next=stack.pop(); + currentNode=currentNode.next; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + int num=size()/2; + for(int i=0;i= size){ + throw new IndexOutOfBoundsException(); + } + + int len=size()-i>=length ? length:size-i; + + int k=0; + + while(k101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + + int[] arr=new int[list.size()]; + + for(int i=0;i min && (int)head.data < max){ + head=head.next; + } + + Node cur=head; + + while(cur.next!=null){ + Node next=cur.next; + if( (int)next.data> min && (int)next.data < max){ + cur.next=next.next; + }else{ + cur=cur.next; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + + LinkedList result=new LinkedList(); + Node cur=head; + int i=0; + while(cur!=null && i(int)list.get(i)){ + i++; + }else{ + cur=cur.next; + } + } + return result; + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + Node node = head; + while(node != null){ + buffer.append(node.data); + if(node.next != null){ + buffer.append(","); + } + node = node.next; + } + buffer.append("]"); + return buffer.toString(); + } +} \ No newline at end of file diff --git a/group26/1515345281/src/week3/list/LinkedListTest.java b/group26/1515345281/src/week3/list/LinkedListTest.java new file mode 100644 index 0000000000..7329960d49 --- /dev/null +++ b/group26/1515345281/src/week3/list/LinkedListTest.java @@ -0,0 +1,201 @@ +package week3.list; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverse() { + LinkedList l = new LinkedList(); + + Assert.assertEquals("[]", l.toString()); + + l.add(1); + l.reverse(); + Assert.assertEquals("[1]", l.toString()); + + l.add(2); + l.add(3); + l.add(4); + + l.reverse(); + Assert.assertEquals("[4,3,2,1]", l.toString()); + } + + + @Test + public void testRemoveFirstHalf() { + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.removeFirstHalf(); + Assert.assertEquals("[3,4]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.removeFirstHalf(); + Assert.assertEquals("[3,4,5]", linkedList.toString()); + } + } + + @Test + public void testRemoveIntInt() { + + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(0, 2); + Assert.assertEquals("[3,4]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(3, 2); + Assert.assertEquals("[1,2,3]", linkedList.toString()); + } + { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.remove(2, 2); + Assert.assertEquals("[1,2]", linkedList.toString()); + } + + } + @Test + public void testGetElements() { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + LinkedList list = new LinkedList(); + list.add(1); + list.add(3); + list.add(4); + list.add(6); + Assert.assertArrayEquals(new int[]{101,301,401,601}, linkedList.getElements(list)); + } + + @Test + public void testSubtract() { + LinkedList list1 = new LinkedList(); + list1.add(101); + list1.add(201); + list1.add(301); + list1.add(401); + list1.add(501); + list1.add(601); + list1.add(701); + + LinkedList list2 = new LinkedList(); + + list2.add(201); + list2.add(301); + list2.add(501); + + list1.subtract(list2); + + Assert.assertEquals("[101,401,601,701]", list1.toString()); + } + + + @Test + public void testRemoveDuplicateValues() { + LinkedList list = new LinkedList(); + list.add(1); + list.add(1); + list.add(2); + list.add(2); + list.add(3); + list.add(5); + list.add(5); + list.add(6); + list.add(6); + list.removeDuplicateValues(); + + Assert.assertEquals("[1,2,3,5,6]", list.toString()); + } + + + @Test + public void testRemoveRange() { + + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(16); + linkedList.add(16); + linkedList.add(19); + + linkedList.removeRange(10, 19); + Assert.assertEquals("[19]", linkedList.toString()); + } + + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(16); + linkedList.add(16); + linkedList.add(19); + + linkedList.removeRange(10, 14); + Assert.assertEquals("[14,16,16,19]", linkedList.toString()); + } + } + @Test + public void testIntersection() { + LinkedList list1 = new LinkedList(); + list1.add(1); + list1.add(6); + list1.add(7); + + LinkedList list2 = new LinkedList(); + list2.add(2); + list2.add(5); + list2.add(6); + + LinkedList newList = list1.intersection(list2); + Assert.assertEquals("[6]", newList.toString()); + } + +} diff --git a/group26/1515345281/src/week3/list/List.java b/group26/1515345281/src/week3/list/List.java new file mode 100644 index 0000000000..cc4a2cb261 --- /dev/null +++ b/group26/1515345281/src/week3/list/List.java @@ -0,0 +1,14 @@ +package week3.list; + +public interface List { + + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group26/1515345281/src/week3/list/ListUtils.java b/group26/1515345281/src/week3/list/ListUtils.java new file mode 100644 index 0000000000..b37e401490 --- /dev/null +++ b/group26/1515345281/src/week3/list/ListUtils.java @@ -0,0 +1,9 @@ +package week3.list; + +public class ListUtils { + + public static void checkIndexRange(int index, int size) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException(); + } +} diff --git a/group26/1515345281/src/week4/origin/jvm/loader/ClassFileLoader.java b/group26/1515345281/src/week4/origin/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..601aab6aad --- /dev/null +++ b/group26/1515345281/src/week4/origin/jvm/loader/ClassFileLoader.java @@ -0,0 +1,72 @@ +package week4.origin.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + static final int BUFFER_SIZE = 1024; + private List clzPaths = new ArrayList(); + + private String path; + + public byte[] readBinaryCode(String className) { + // "week4.origin.jvm.loader.test.EmployeeV1" + String fileName = path+File.separator+className.replace(".", File.separator) + ".class"; + + FileInputStream in=null; + BufferedInputStream bis=null; + ByteArrayOutputStream baos=null; + + try { + in = new FileInputStream(fileName); + + bis = new BufferedInputStream(in, BUFFER_SIZE); + + // 缓冲区会因为数据的不断写入而自动增长 + baos = new ByteArrayOutputStream(); + + byte[] buffer = new byte[BUFFER_SIZE]; + + int len = 0; + + while ((len = bis.read(buffer)) != -1) { + baos.write(buffer, 0, len); + } + + } catch (IOException e) { + e.printStackTrace(); + }finally{ + if(bis!=null){ + try { + bis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(in!=null){ + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + return baos.toByteArray(); + } + + public void addClassPath(String path) { + this.path=path; + } + + public String getClassPath() { + return path; + } +} \ No newline at end of file diff --git a/group26/1515345281/src/week4/origin/jvm/loader/LRUPageFrame.java b/group26/1515345281/src/week4/origin/jvm/loader/LRUPageFrame.java new file mode 100644 index 0000000000..104f3b8690 --- /dev/null +++ b/group26/1515345281/src/week4/origin/jvm/loader/LRUPageFrame.java @@ -0,0 +1,105 @@ +package week4.origin.jvm.loader; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private int capacity; + private int curSize;//记录当前缓存大小 + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + curSize=0; + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + if(capacity <=0 ){ + throw new IndexOutOfBoundsException(); + } + + if(curSize == 0){ + Node node=new Node(null,null,pageNum); + curSize++; + first=node; + last=node; + return ; + } + + if(first.pageNum == pageNum){ + return ; + } + + //不管是否需要置换头指针均需要改变 + Node node=new Node(null,first,pageNum); + first.prev=node; + first=node; + + if(curSize == 1){ + last.prev=node; + } + + if(curSize < capacity){ + curSize++; + return ; + } + + if(curSize == capacity){//容量不足,开始置换 + + //首先判断里面是否存在值相同元素 + Node curNode=first.next; + while(curNode.next!=null){ + if(curNode.pageNum == pageNum){//如果找到 + curNode.prev.next=curNode.next; + curNode.next.prev=curNode.prev; + return ; + } + curNode=curNode.next; + } + Node tempNode=last.prev; + last=tempNode; + last.next=null; + } + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + private static class Node { + Node prev; + Node next; + int pageNum; + public Node(Node prev,Node next,int pageNum){ + this.prev=prev; + this.next=next; + this.pageNum=pageNum; + } + } +} \ No newline at end of file diff --git a/group26/1515345281/src/week4/origin/jvm/loader/blog b/group26/1515345281/src/week4/origin/jvm/loader/blog new file mode 100644 index 0000000000..010f9cb473 --- /dev/null +++ b/group26/1515345281/src/week4/origin/jvm/loader/blog @@ -0,0 +1 @@ +SAX解析Xml实战 http://blog.csdn.net/sjshenjian/article/details/68950978 \ No newline at end of file diff --git a/group26/1515345281/src/week4/origin/jvm/loader/test/ClassFileLoaderTest.java b/group26/1515345281/src/week4/origin/jvm/loader/test/ClassFileLoaderTest.java new file mode 100644 index 0000000000..69836b7c04 --- /dev/null +++ b/group26/1515345281/src/week4/origin/jvm/loader/test/ClassFileLoaderTest.java @@ -0,0 +1,55 @@ +package week4.origin.jvm.loader.test; + +import org.junit.Assert; +import org.junit.Test; + +import week4.origin.jvm.loader.ClassFileLoader; + + +public class ClassFileLoaderTest { + + + static String path1="E:\\JAVA\\liuxin\\coding2017\\group26\\1515345281\\bin"; + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "week4.origin.jvm.loader.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1066, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "week4.origin.jvm.loader.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer=new StringBuffer(); + for(int i=0;iindex) {return null;} + return p.data; + } + public Object remove(int index){ + int i=1; + Node p=head; + Object o=null; + if(index==1) + { + o=head.data; + if(head.next!=null) + { + p=head.next; + head.data=p.data; + p=head; + } + else{ + head=null; + } + } + else{ + while(i7->10 , 逆置后变为 10->7->3 + */ + public void reverse() + { + if(head==null||null==head.next) + { + return; + } + Stack s=new Stack(); + Node currentNode=head; + while(currentNode!=null) + { + s.push(currentNode); + Node nextNode=currentNode.next; + currentNode.next=null; //把链表断开 + currentNode=nextNode; + } + head=s.pop(); + currentNode=head; + while(!s.isEmpty()) + { + Node nextNode=s.pop(); + currentNode.next=nextNode; + currentNode=nextNode; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + int length=size/2; + Node p=head; + for(int i=0;isize-1-i) return; + if(i==0) + { + removeFirst(); + for(i=1;i<=length-1;i++) + { + removeFirst(); + } + } + else{ + int j=0; + Node p=head; + while(j101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(this.size<1) return null; + if((int)(list.get(list.size))>this.size) return null; + //将链表转为数组 + int[] listToArray=new int[this.size]; + Node n=head; + for(int i=0;i actions=new HashMap<>(); + + public Configureation(String fileName) + { + String packageName=this.getClass().getPackage().getName(); + packageName=packageName.replace('.', '/'); + InputStream is=this.getClass().getResourceAsStream("/"+packageName+"/"+fileName); + parseXML(is); + + try{ + is.close(); + } + catch(IOException e){ + e.printStackTrace(); + } + } + + private void parseXML(InputStream is) { + + SAXBuilder builder=new SAXBuilder(); + + try{ + Document doc=builder.build(is); + Element root =doc.getRootElement(); + for(Element actionElement :root.getChildren("action")) + { + String actionName=actionElement.getAttributeValue("name"); + String clzName=actionElement.getAttributeValue("class"); + ActionConfig ac=new ActionConfig(actionName,clzName); + for(Element resultElement:actionElement.getChildren()) + { + String resultName=resultElement.getAttributeValue("name"); + String viewName=resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); + + } + this.actions.put(actionName, ac);//把actionName以及其下面的resultName和viewName构成一个键值对 + } + } + catch(JDOMException e){ + e.printStackTrace(); + } catch(IOException e) + { + e.printStackTrace(); + } + } + + public String getClassName(String action) { + ActionConfig ac=this.actions.get(action); + if(ac==null) + { + return null; + } + return ac.getClassName(); + } + + public String getResultView(String action,String resultName) + { + ActionConfig ac=this.actions.get(action); + if(ac==null){ + return null; + } + return ac.getViewName(resultName); + } + private static class ActionConfig{ + String name; + String clzName; + Map viewResult=new HashMap<>(); + + public ActionConfig(String actionName,String clzName){ + this.name=actionName; + this.clzName=clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name,String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName) + { + return viewResult.get(resultName); + } + } +} diff --git a/group26/1778842360/third homework/TTD/LoginAction.java b/group26/1778842360/third homework/TTD/LoginAction.java new file mode 100644 index 0000000000..e398f0d39a --- /dev/null +++ b/group26/1778842360/third homework/TTD/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts; + +public class LoginAction { + + private String name; + private String password; + private String message; + + public String getName() + { + return name; + } + + public String getPassword() + { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)) + { + this.message="login successful"; + return "success"; + } + this.message="login failed,please check you user/pwd"; + return "fail"; + } + public void setName(String name) { + + this.name=name; + } + + public void setPassword(String password) { + + this.password=password; + } + public String getMessage() + { + return message; + } + +} diff --git a/group26/1778842360/third homework/TTD/ReflectionUtil.java b/group26/1778842360/third homework/TTD/ReflectionUtil.java new file mode 100644 index 0000000000..186edb6c5a --- /dev/null +++ b/group26/1778842360/third homework/TTD/ReflectionUtil.java @@ -0,0 +1,76 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + + return getMethods(clz,"set"); + } + + public static void setParameters(Object o, Map params) { + + List methods=getSetterMethods(o.getClass()); + + for(String name:params.keySet()) + { + String methodName="set"+name; + for(Method m:methods) + { + if(m.getName().equalsIgnoreCase(methodName)) + { + try{ + m.invoke(o, params.get(name)); + }catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) + { + e.printStackTrace(); + } + } + } + } + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz,"get"); + } + + public static List getMethods(Class clz,String startWithName) + { + List methods=new ArrayList<>(); + for(Method m:clz.getDeclaredMethods()) + { + if(m.getName().startsWith(startWithName)) + { + methods.add(m); + } + } + return methods; + } + + public static Map getParamterMap(Object o) { + Map params=new HashMap<>(); + + List methods=getGetterMethods(o.getClass()); + + for(Method m:methods) + { + String methodName=m.getName(); + String name=methodName.replaceFirst("get","").toLowerCase(); + try{ + Object value=m.invoke(o); + params.put(name, value); + }catch(Exception e) + { + e.printStackTrace(); + } + } + return params; + } + +} diff --git a/group26/1778842360/third homework/TTD/ReflectionUtilTest.java b/group26/1778842360/third homework/TTD/ReflectionUtilTest.java new file mode 100644 index 0000000000..27a56433ca --- /dev/null +++ b/group26/1778842360/third homework/TTD/ReflectionUtilTest.java @@ -0,0 +1,108 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ReflectionUtilTest { + + @Before + public void setUp() throws Exception{ + + } + @After + public void TearDown() throws Exception{ + + } + @Test + public void testGetSetterMethod() throws ClassNotFoundException { + String name="com.coderising.litestruts.LoginAction"; + Class clz=Class.forName(name); + List methods=ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames=new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set actualNames=new HashSet<>(); + for(Method m:methods){ + actualNames.add(m.getName()); + } + Assert.assertTrue(actualNames.containsAll(expectedNames)); + } + @Test + public void testSetParameter() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException + { + String name="com.coderising.litestruts.LoginAction"; + Class clz=Class.forName(name); + + Object o=clz.newInstance(); + + Map params=new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + ReflectionUtil.setParameters(o,params); + + Field f=clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f=clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + @Test + public void testGetGetterMethod() throws Exception + { + String name="com.coderising.litestruts.LoginAction"; + Class clz=Class.forName(name); + List methods=ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3,methods.size()); + + List expectedNames=new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set actualNames=new HashSet<>(); + for(Method m:methods) + { + actualNames.add(m.getName()); + } + Assert.assertTrue(actualNames.containsAll(expectedNames)); + } + @Test + public void testGetParmters() throws Exception + { + String name="com.coderising.litestruts.LoginAction"; + Class clz=Class.forName(name); + + LoginAction action=(LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + Map params=ReflectionUtil.getParamterMap(action); + Assert.assertEquals(3,params.size()); + + Assert.assertEquals(null, params.get("message")); + Assert.assertEquals("test",params.get("name")); + Assert.assertEquals("123456",params.get("password")); + } + + +} diff --git a/group26/1778842360/third homework/TTD/Struts.java b/group26/1778842360/third homework/TTD/Struts.java new file mode 100644 index 0000000000..ab704cf7e9 --- /dev/null +++ b/group26/1778842360/third homework/TTD/Struts.java @@ -0,0 +1,56 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.Map; + +public class Struts { + private final static Configureation cfg=new Configureation("struts.xml"); + public static View runAction(String actionName,Map parameters) + { + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + String clzName=cfg.getClassName(actionName); + if(clzName==null) + { + return null; + } + try{ + Class clz=Class.forName(clzName); + Object action=clz.newInstance(); + + ReflectionUtil.setParameters(action, parameters); + + Method m=clz.getDeclaredMethod("execute"); + String resultName=(String) m.invoke(action); + + Map params=ReflectionUtil.getParamterMap(action); + String resultView=cfg.getResultView(actionName, resultName); + + View view=new View(); + view.setParameters(params); + view.setJsp(resultView); + return view; + }catch(Exception e) + { + e.printStackTrace(); + } + return null; + } +} diff --git a/group26/1778842360/third homework/TTD/View.java b/group26/1778842360/third homework/TTD/View.java new file mode 100644 index 0000000000..46138088a9 --- /dev/null +++ b/group26/1778842360/third homework/TTD/View.java @@ -0,0 +1,27 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + public View setParameters(Map params) { + this.parameters=params; + return this; + } + + public View setJsp(String jsp) { + this.jsp=jsp; + return this; + } + public Map getParameters() + { + return parameters; + } + public String getJsp() + { + return jsp; + } + +} diff --git a/group26/191191717/src/week3/com/coding/download/DownloadThread.java b/group26/191191717/src/week3/com/coding/download/DownloadThread.java new file mode 100644 index 0000000000..53f77ff508 --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/DownloadThread.java @@ -0,0 +1,65 @@ +package week3.com.coding.download; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +import week3.com.coding.download.api.Connection; + +public class DownloadThread extends Thread +{ + + Connection conn; + + int startPos; + + int endPos; + + public DownloadThread(Connection conn, int startPos, int endPos) + { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + /** + * ִ߳з̶߳ȡһȵֽڣдļ + */ + public void run() + { + File f = new File("d:\\test.txt"); + RandomAccessFile raf = null; + try + { + raf = new RandomAccessFile(f, "rwd"); + raf.seek(startPos);// λǰָ + // raf.close(); + byte[] bs = conn.read(startPos, endPos); + raf.write(bs); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + /** + * Դͷ + * + * @param raf + * @param conn + */ + public void release(RandomAccessFile raf, Connection conn) + { + try + { + raf.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + conn.close(); + } +} diff --git a/group26/191191717/src/week3/com/coding/download/FileDownloader.java b/group26/191191717/src/week3/com/coding/download/FileDownloader.java new file mode 100644 index 0000000000..916dd3b13f --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/FileDownloader.java @@ -0,0 +1,145 @@ +package week3.com.coding.download; + +import java.io.IOException; + +import week3.com.coding.download.api.Connection; +import week3.com.coding.download.api.ConnectionException; +import week3.com.coding.download.api.ConnectionManager; +import week3.com.coding.download.api.DownloadListener; +import week3.com.coding.download.impl.ConnectionImpl; +import week3.com.coding.download.impl.ConnectionManagerImpl; + +public class FileDownloader +{ + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + int ThreadNum; + + public FileDownloader(String url, int threadNum) + { + super(); + this.url = url; + ThreadNum = threadNum; + } + + public void execute() + { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try + { + conn = (ConnectionImpl)cm.open(this.url); + int length = conn.getContentLength();// 获取文件的长度 + // 三个线程,每个线程下载长度要平均 + int blockSize = length / this.ThreadNum; + for (int i = 1; i <= this.ThreadNum; i++) + { + int sPos = (i - 1) * blockSize; + int ePos = i * blockSize - 1; + // 如果是最后一个,则结束位置等于最后的地方 + if (i == this.ThreadNum) + { + ePos = length; + } + new DownloadThread(conn, sPos, ePos).start(); + } + } + catch (ConnectionException e) + { + e.printStackTrace(); + } + finally + { + if (conn != null) + { + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) + { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) + { + this.cm = ucm; + } + + public DownloadListener getListener() + { + return this.listener; + } + + public String getUrl() + { + return url; + } + + public void setUrl(String url) + { + this.url = url; + } + + public ConnectionManager getCm() + { + return cm; + } + + public void setCm(ConnectionManager cm) + { + this.cm = cm; + } + + public int getThreadNum() + { + return ThreadNum; + } + + public void setThreadNum(int threadNum) + { + ThreadNum = threadNum; + } + + public static void main(String[] args) + throws ConnectionException, IOException + { + + String url = "http://localhost:8088/JSPDemo/test.txt"; + // ConnectionImpl ci=(ConnectionImpl)cm.open(url); + // System.out.println(new String(ci.read(2, 31))); + // File f = new File("d:\\test.txt"); + // RandomAccessFile raf = new RandomAccessFile(f, "rwd"); + // raf.seek(raf.length());// 定位当前的指针 + + FileDownloader downloader = new FileDownloader(url,3); + downloader.setConnectionManager(new ConnectionManagerImpl()); + downloader.execute(); + // int length = conn.getContentLength();// 获取文件的长度 + // System.out.println("urlConn: " + length); + // int blockSize = length / 3; + + // new DownloadThread(conn, 0, blockSize - 1).start();// 第一个线程 + // new DownloadThread(conn, blockSize, blockSize * 2 - 1).start();// 第二个线程 + // new DownloadThread(conn, blockSize * 2 , length - 1).start();// 第三个线程 + } +} diff --git a/group26/191191717/src/week3/com/coding/download/FileDownloaderTest.java b/group26/191191717/src/week3/com/coding/download/FileDownloaderTest.java new file mode 100644 index 0000000000..44dadbdd4a --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/FileDownloaderTest.java @@ -0,0 +1,66 @@ +package week3.com.coding.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import week3.com.coding.download.api.ConnectionManager; +import week3.com.coding.download.api.DownloadListener; +import week3.com.coding.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest +{ + boolean downloadFinished = false; + + @Before + public void setUp() + throws Exception + { + } + + @After + public void tearDown() + throws Exception + { + } + + @Test + public void testDownload() + { + String url = "http://localhost:8088/JSPDemo/test.txt"; + + FileDownloader downloader = new FileDownloader(url,3); + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() + { + @Override + public void notifyFinished() + { + downloadFinished = true; + } + + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) + { + try + { + System.out.println("还没有下载完成,休眠五秒"); + // 休眠5秒 + Thread.sleep(5000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + } + +} diff --git a/group26/191191717/src/week3/com/coding/download/api/Connection.java b/group26/191191717/src/week3/com/coding/download/api/Connection.java new file mode 100644 index 0000000000..8d664048df --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/api/Connection.java @@ -0,0 +1,28 @@ +package week3.com.coding.download.api; + +import java.io.IOException; + +public interface Connection +{ + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) + throws IOException; + + /** + * 得到数据内容的长度 + * + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} \ No newline at end of file diff --git a/group26/191191717/src/week3/com/coding/download/api/ConnectionException.java b/group26/191191717/src/week3/com/coding/download/api/ConnectionException.java new file mode 100644 index 0000000000..5a5dfb8986 --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package week3.com.coding.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group26/191191717/src/week3/com/coding/download/api/ConnectionManager.java b/group26/191191717/src/week3/com/coding/download/api/ConnectionManager.java new file mode 100644 index 0000000000..508c9cfd6b --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package week3.com.coding.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group26/191191717/src/week3/com/coding/download/api/DownloadListener.java b/group26/191191717/src/week3/com/coding/download/api/DownloadListener.java new file mode 100644 index 0000000000..88fb30cf93 --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/api/DownloadListener.java @@ -0,0 +1,6 @@ +package week3.com.coding.download.api; + +public interface DownloadListener +{ + public void notifyFinished(); +} diff --git a/group26/191191717/src/week3/com/coding/download/impl/ConnectionImpl.java b/group26/191191717/src/week3/com/coding/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..cf03537556 --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/impl/ConnectionImpl.java @@ -0,0 +1,70 @@ +package week3.com.coding.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import week3.com.coding.download.api.Connection; + +public class ConnectionImpl implements Connection +{ + HttpURLConnection conn; + + public ConnectionImpl() + { + } + + public ConnectionImpl(HttpURLConnection urlConn) + { + this.conn = urlConn; + } + + public HttpURLConnection getConn() + { + return conn; + } + + public void setConn(HttpURLConnection conn) + { + this.conn = conn; + } + + @Override + public byte[] read(int startPos, int endPos) + throws IOException + { + System.out.println("startPos: " + startPos + " endPos " + endPos); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + (endPos + 1)); + InputStream is = conn.getInputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buff = new byte[1024]; + int len = 0; + while ((len = is.read(buff)) != -1) + { + out.write(buff, 0, len); + } + byte[] bs = out.toByteArray(); + return bs; + } + + /** + * ȡļij + */ + @Override + public int getContentLength() + { + + return conn == null ? 0 : conn.getContentLength(); + } + + @Override + public void close() + { + if (conn != null) + { + conn.disconnect(); + } + } + +} diff --git a/group26/191191717/src/week3/com/coding/download/impl/ConnectionManagerImpl.java b/group26/191191717/src/week3/com/coding/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..8a09013c3e --- /dev/null +++ b/group26/191191717/src/week3/com/coding/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,42 @@ +package week3.com.coding.download.impl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import week3.com.coding.download.api.Connection; +import week3.com.coding.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager +{ + /** + * һurl , һ + * + * @param url + * @return + */ + @Override + public Connection open(String url) + { + Connection conn=null; + URL httpUrl = null; + HttpURLConnection urlConn = null; + try + { + httpUrl = new URL(url); + urlConn = (HttpURLConnection)httpUrl.openConnection(); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } + catch (IOException e) + { + e.printStackTrace(); + } + conn= new ConnectionImpl(urlConn); + return conn; + } + +} diff --git a/group26/2441547139/week1/collection/MyArrayList.java b/group26/2441547139/week1/collection/MyArrayList.java new file mode 100644 index 0000000000..979ee23d8c --- /dev/null +++ b/group26/2441547139/week1/collection/MyArrayList.java @@ -0,0 +1,65 @@ +package week1.collection; + +import java.util.Arrays; +/** + * Created by zndbl on 2017/3/11. + */ +public class MyArrayList { + + private int size; + private Object[] arr; + + public MyArrayList() { + this(10); + } + + public MyArrayList(int oldLength) { + if(oldLength < 0) { + throw new RuntimeException("创建集合失败"); + } + arr = new Object[oldLength]; + } + + public int size() { + return size; + } + + /** + * 数组的长度扩充策略 + */ + public void ensureCapacity(int minCapatity ) { + int oldCapacity = arr.length; + if(minCapatity > oldCapacity) { + int newCapatity = 3 * oldCapacity / 2 + 1; + if(minCapatity > newCapatity) { + newCapatity = minCapatity; + } + arr = Arrays.copyOf(arr,newCapatity); + } + } + + public void add(Object element) { + ensureCapacity(size+1); + arr[size++] = element; + } + + public void add(int index, Object element) { + if(index < 0 || index > size) { + throw new RuntimeException("数组越界"); + } + ensureCapacity(size+1); + System.arraycopy(arr,index,arr,index+1,size-index); + arr[index] = element; + size++; + } + + public boolean remove(Object o) { + for(int i=0; i index ; i--) { + cussor = cussor.prev; + } + return cussor; + } + } + + public Object get(int index) { + checkRange(index); + return node(index).item; + } + + public void checkRange(int index) { + if(index >= size || index < 0) { + throw new RuntimeException("index超过界限"); + } + } + + public int indexOf(Object element) { + Node cussor = first; + int count = 0; + while (cussor != null) { + if(element.equals(cussor.item)) { + return count; + } + count++; + cussor = cussor.next; + } + return -1; + } + + public boolean remove(Object o) { + int index = indexOf(o); + if(index < 0) { + return false; + } + deleteLink(index); + return true; + } + + public Object deleteLink(int index) { + Node l = node(index); + Object item = l.item; + Node prevNode = l.prev; + Node nextNode = l.next; + + if(prevNode == null) { + first = nextNode; + } else { + prevNode.next = nextNode; + l.next = null; + } + if(nextNode == null) { + last = prevNode; + } else { + nextNode.prev = prevNode; + l.prev = null; + } + size--; + l.item = null; + return item; + + } + + +} diff --git a/group26/2441547139/week1/collection/MyQueue.java b/group26/2441547139/week1/collection/MyQueue.java new file mode 100644 index 0000000000..2721b305a7 --- /dev/null +++ b/group26/2441547139/week1/collection/MyQueue.java @@ -0,0 +1,29 @@ +package week1.collection; + +/** + * Created by zndbl on 2017/3/12. + */ +public class MyQueue { + + private Object[] data; + private int head; + private int tail; + + public MyQueue() { + data = new Object[10]; + head = 1; + tail = 1; + } + + public void put(Object obj) { + data[tail] = obj; + tail++; + } + + public Object get() { + Object obj = data[head]; + head++; + return obj; + } + +} diff --git a/group26/2441547139/week1/collection/MyStack.java b/group26/2441547139/week1/collection/MyStack.java new file mode 100644 index 0000000000..1b2b8c5d2c --- /dev/null +++ b/group26/2441547139/week1/collection/MyStack.java @@ -0,0 +1,29 @@ +package week1.collection; + +/** + * Created by zndbl on 2017/3/12. + */ +public class MyStack { + + private Object[] data; + private int top; + + public MyStack() { + data = new Object[100]; + top = -1; + } + + public void put(Object t) { + data[data.length] = t; + top++; + } + + public Object pop() { + if(top < 0) { + return null; + } + Object object = data[top]; + top--; + return object; + } +} diff --git a/group26/2441547139/week2/arrayutil/ArrayUtils.java b/group26/2441547139/week2/arrayutil/ArrayUtils.java new file mode 100644 index 0000000000..5bb9f6582f --- /dev/null +++ b/group26/2441547139/week2/arrayutil/ArrayUtils.java @@ -0,0 +1,172 @@ +package week2.arrayutil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by zndbl on 2017/3/23. + */ +public class ArrayUtils { + + public static void main(String[] args) { +// int[] oldArray = new int[]{4,6,2,1,0,5,0,8}; +// int[] newArray = reverseArray(oldArray); +// int[] newArray = removeZero(oldArray); +// String array = "["; +// for(int i = 0 ; i < newArray.length ; i++) { +// array += newArray[i]; +// } +// array += "]"; +// System.out.println(array); +// String s = seperatorArray(oldArray); +// System.out.println(s); +// List math = getAllMath(6); +// int[] array = getPrimeArray(23); +// printArray(array); + getFibonacci(15); + } + + public static void printArray(int[] newArray) { + String array = "["; + for (int i = 0; i < newArray.length; i++) { + array += (newArray[i]+","); + } + array += "]"; + System.out.println(array); + } + + /** + * 数组的反转 + * + * @param oldArray + * @return + */ + public static int[] reverseArray(int[] oldArray) { + int[] newArray = new int[oldArray.length]; + for (int i = 0; i < (oldArray.length); i++) { + newArray[i] = oldArray[oldArray.length - 1 - i]; + } + return newArray; + } + + /** + * 清除数组中的元素0 + * + * @param array + * @return + */ + public static int[] removeZero(int[] array) { + int[] newArray = new int[array.length]; + int j = 0; + for (int i = 0; i < array.length; i++) { + if (array[i] != 0) { + newArray[j++] = array[i]; + } + } + System.out.println(j); + Arrays.copyOf(newArray, j); + return newArray; + } + + /** + * 连接字符 + * + * @param oldArray + * @return + */ + public static String seperatorArray(int[] oldArray) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < oldArray.length; i++) { + if (i == oldArray.length - 1) { + sb.append(oldArray[i]); + break; + } + sb.append(oldArray[i]).append("_"); + } + return sb.toString(); + } + + /** + * 传100,求小于100的所有完数 + * + * @param old + * @return + */ + public static List getAllMath(int old) { + List list = new ArrayList<>(); + int count = 0; + for (int i = 1; i <= old - 1; i++) { + if (old % i == 0) { + System.out.println(i); + count = count + i; + list.add(i); + } + } + if (count == old) { + return list; + } + return new ArrayList(); + } + + /** + * 返回所有小于给定数的素数数组 + * + * @param old + * @return + */ + public static int[] getPrimeArray(int old) { + int[] primeArray = new int[old]; + int k = 0; + for (int i = 1; i < old; i++) { + if (isPrime(i)) { + primeArray[k] = i; + k++; + } + } + return Arrays.copyOf(primeArray, k ); + } + + /** + * 判断一个数是不是素数 + * + * @param i + * @return + */ + public static boolean isPrime(int i) { + int count = 0; + for (int j = 1; j <= i; j++) { + if (i % j == 0) { + count++; + } + } + if (count > 2 || count == 1) { + return false; + } + return true; + } + + /** + * 小于给定数的斐波那契数列 + * 传进去15,1 1 2 3 5 8 13 + * @param old + * @return + */ + public static void getFibonacci(int old) { + int first = 1; + int second = 1; + int num = add(first, second, old); + System.out.println(num); + } + + public static int add(int first, int second, int old) { + int last = first + second; + int before = second; + if(last > old) { + return before; + } + return add(before, last, old); + } + + +} diff --git a/group26/2441547139/week2/struts/LoginAction.java b/group26/2441547139/week2/struts/LoginAction.java new file mode 100644 index 0000000000..b7bf1adc62 --- /dev/null +++ b/group26/2441547139/week2/struts/LoginAction.java @@ -0,0 +1,39 @@ +package week2.struts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group26/2441547139/week2/struts/LoginOutAction.java b/group26/2441547139/week2/struts/LoginOutAction.java new file mode 100644 index 0000000000..450d158339 --- /dev/null +++ b/group26/2441547139/week2/struts/LoginOutAction.java @@ -0,0 +1,5 @@ +package week2.struts; + +public class LoginOutAction { + +} diff --git a/group26/2441547139/week2/struts/Struts.java b/group26/2441547139/week2/struts/Struts.java new file mode 100644 index 0000000000..ddc5509539 --- /dev/null +++ b/group26/2441547139/week2/struts/Struts.java @@ -0,0 +1,74 @@ +package week2.struts; + + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.List; + +public class Struts { + + public static void main(String[] args) { + runAction(); + } + + public static void runAction() { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + - + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + File file = new File("src/week2/struts/struts.xml"); + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(file); + Element rootElement = document.getRootElement(); +// List elements = rootElement.elements("action"); +// for (Element element : elements) { +// String aClass = element.attributeValue("class"); +// Class firstClass = Class.forName(aClass); +// Object obj = firstClass.newInstance(); +// Method setName = firstClass.getMethod("setName",String.class); +// Method setPassWord = firstClass.getMethod("setPassword",String.class); +// Method execute = firstClass.getMethod("execute"); +// setName.invoke(obj , "test"); +// setPassWord.invoke(obj , "1234"); +// String result = (String) execute.invoke(obj); +// System.out.println(result); +// } + //根节点的子节点 + List elements = rootElement.elements(); + for(Element element : elements) { + //节点的属性 + List attributes = element.attributes(); + List list = element.elements(); + for(Element e : list) { + if(e.attributeValue("name").equals("success")) { + String jsp = e.getTextTrim(); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/group26/2441547139/week2/struts/View.java b/group26/2441547139/week2/struts/View.java new file mode 100644 index 0000000000..2b8c86c49f --- /dev/null +++ b/group26/2441547139/week2/struts/View.java @@ -0,0 +1,23 @@ +package week2.struts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group26/2441547139/week3/linkedlist/MyLinkedLists.java b/group26/2441547139/week3/linkedlist/MyLinkedLists.java new file mode 100644 index 0000000000..dfd299dae6 --- /dev/null +++ b/group26/2441547139/week3/linkedlist/MyLinkedLists.java @@ -0,0 +1,272 @@ +package week3.linkedlist; + +import java.util.Iterator; + +/** + * Created by zndbl on 2017/3/29. + */ +public class MyLinkedLists { + + private Node head; + private int size; + + /** + * 删除第一个 + * + * @return + */ + public Node removeFirst() { + Node node = head; + head = node.getNext(); + size--; + return node; + } + + /** + * 删除最后一个 + * + * @return + */ + public Node removeLast() { + Node node = head; + Node pre = null; + while (node != null) { + pre = node; + node = node.getNext(); + } + pre.setNext(null); + size--; + return pre; + } + + /** + * 新增节点在第一个 + * + * @param data + */ + public void addFirst(Object data) { + Node node = new Node(data); + node.setNext(head); + head = node; + size++; + } + + /** + * 得到指定索引的节点 + * + * @param index + * @return + */ + public Node getNode(int index) { + index++; + Node node = null; + for (int i = 0; i < index; i++) { + node = head; + node = node.getNext(); + } + return head; + } + + /** + * 删除指定索引的节点 + * + * @param index + * @return + */ + public Node removeNode(int index) { + Node prevNode = getNode(index--); + Node currNode = getNode(index); + Node succNode = currNode.getNext(); + prevNode.setNext(succNode); + currNode = null; + size--; + return succNode; + } + + /** + * 在最后添加一个节点 + * + * @return + */ + public Node addLast(Object data) { + Node node = new Node(data); + Node curr = head; + Node succ = null; + while (curr != null) { + succ = curr; + curr = curr.getNext(); + } + succ.setNext(node); + size++; + return node; + } + + /** + * 在指定索引增加 + * + * @param index + * @param obj + */ + public void add(int index, Object obj) { + Node curr = head; + Node prev = null; + while (curr != null) { + if (index == 0) { + break; + } + prev = curr; + curr = curr.getNext(); + index--; + } + if (prev != null) { + Node node = new Node(obj); + node.setNext(curr); + prev.setNext(node); + } + } + + /** + * 得到大小 + * + * @return + */ + public int size() { + return size; + } + + /** + * 得到迭代器 + * + * @return + */ + public Iterator iterator() { + return new MyLinkedListsIterator(this); + } + + /** + * 反转节点 + */ + public void reverse() { + Node prev = null; + Node next = null; + Node curr = head; + while (curr != null) { + next = curr.getNext(); + curr.setNext(prev); + prev = curr; + curr = next; + } + head = prev; + } + + /** + * 链表头一半删除 + */ + public void removeFirstHalf() { + Node curr = head; + int half = size / 2; + while (half != 0) { + curr = curr.getNext(); + half--; + } + head = curr; + } + + /** + * 指定索引,指定长度的删除 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (i + length >= size - 1) { + length = size - 1 - i; + } + int count = i; + Node pre = null; + Node curr = head; + while (curr != null) { + if (count == 0) { + break; + } + pre = curr; + curr = curr.getNext(); + count--; + } + while (curr != null) { + if (length == 0) { + break; + } + curr = curr.getNext(); + length--; + } + pre.setNext(curr.getNext()); + + } + + /** + * 打印方法 + * @return + */ + public String toString() { + StringBuilder builder = new StringBuilder(); + Node current = head; + while (current == null) { + builder.append(current.toString()); + current = current.getNext(); + } + return builder.toString(); + } + + private class MyLinkedListsIterator implements Iterator { + + private MyLinkedLists linkedList; + private int length = 0; + + public MyLinkedListsIterator(MyLinkedLists linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return length < size; + } + + @Override + public Object next() { + return linkedList.getNode(length++); + } + + @Override + public void remove() { + linkedList.removeNode(length--); + } + } + + + public static class Node { + + private Object data; + private Node next; + + public Node(Object data) { + this.data = data; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node getNext() { + return next; + } + + public Object getData() { + return this.data; + } + + public String toString() { + return "data = "+data; + } + } +} diff --git a/group26/2441547139/week3/thread/DownloadThread.java b/group26/2441547139/week3/thread/DownloadThread.java new file mode 100644 index 0000000000..f6ea4ae09f --- /dev/null +++ b/group26/2441547139/week3/thread/DownloadThread.java @@ -0,0 +1,67 @@ +package week3.thread; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.CountDownLatch; + +public class DownloadThread extends Thread { + + private File file; + private CountDownLatch countDownLatch; + private String address; + private int startPos; + private int endPos; + + public DownloadThread(File file, CountDownLatch countDownLatch, + String address, int startPos, int endPos) { + super(); + this.file = file; + this.countDownLatch = countDownLatch; + this.address = address; + this.startPos = startPos; + this.endPos = endPos; + } + + public void run() { + Thread current = Thread.currentThread(); + System.out.println(current.getName() + "开始下载:" + startPos + "-" + + endPos); + RandomAccessFile randomAccessFile = null; + InputStream inputStream = null; + try { + URL url = new URL(address); + HttpURLConnection httpURLConnection = (HttpURLConnection) url + .openConnection(); + httpURLConnection.setRequestProperty("Range", "bytes=" + startPos + + "-" + endPos); + inputStream = httpURLConnection.getInputStream(); + randomAccessFile = new RandomAccessFile(file, "rw"); + randomAccessFile.seek(startPos); + byte[] bytes = new byte[1024]; + int read = 0; + while ((read = inputStream.read(bytes)) != -1) { + randomAccessFile.write(bytes, 0, read); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (randomAccessFile != null) { + randomAccessFile.close(); + } + if (inputStream != null) { + inputStream.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println(current.getName() + "下载完成"); + countDownLatch.countDown(); + } + + } +} \ No newline at end of file diff --git a/group26/2441547139/week3/thread/FileDownload.java b/group26/2441547139/week3/thread/FileDownload.java new file mode 100644 index 0000000000..7dd7df1a28 --- /dev/null +++ b/group26/2441547139/week3/thread/FileDownload.java @@ -0,0 +1,53 @@ +package week3.thread; + +import java.io.File; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.CountDownLatch; + +public class FileDownload { + + private String address; + + public FileDownload(String address) { + this.address = address; + } + + public void download(int threadCount) { + try { + URL url = new URL(address); + HttpURLConnection httpURLConnection = (HttpURLConnection) url + .openConnection(); + int length = httpURLConnection.getContentLength(); + System.out.println("文件大小:"+length); + File file = new File("D:\\download.jpg"); + CountDownLatch countDownLatch = new CountDownLatch(threadCount); + // 计算每个线程下载的数据大小 + int blockSize = length / threadCount; + for (int i = 0; i < threadCount; i++) { + int startPos = blockSize * i; + int endPos = blockSize * (i + 1); + if (i == threadCount - 1) { + //最后一个下载剩下的 + endPos = length; + } + new DownloadThread(file, countDownLatch, address, startPos, + endPos - 1).start(); + } + while (countDownLatch.getCount() != 0) { + System.out.println("下载中...."); + try { + // 休眠 + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成"); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/group26/2441547139/week3/thread/ThreadDownload.java b/group26/2441547139/week3/thread/ThreadDownload.java new file mode 100644 index 0000000000..7e09e4f843 --- /dev/null +++ b/group26/2441547139/week3/thread/ThreadDownload.java @@ -0,0 +1,35 @@ +package week3.thread; + +/** + * Created by zndbl on 2017/3/26. + */ +public class ThreadDownload { + + public static void main(String[] args) { +// //单线程下载 +// try { +// String url = "http://img.alicdn.com/bao/uploaded/i2/412712826/TB2eNIZXXYC11BjSspfXXXcPFXa_!!412712826.jpg_240x240.jpg"; +// HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); +// conn.setRequestMethod("GET"); +// conn.setReadTimeout(5000); +// conn.setConnectTimeout(5000); +// InputStream in = conn.getInputStream(); +// BufferedInputStream bufferedInputStream = new BufferedInputStream(in); +// File file = new File("D:/a.jpg"); +// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); +// byte[] buffer = new byte[1024]; +// int len = -1; +// while ((len = bufferedInputStream.read(buffer)) != -1) { +// bufferedOutputStream.write(buffer, 0, len); +// bufferedOutputStream.flush(); +// } +// } catch (Exception e) {ScheduledThreadPoolExecutor +// e.printStackTrace(); +// } + + //多线程部分参考网上的 + String url = "http://wx.qlogo.cn/mmopen/fqCl7qHPjf2JaKGXwqRe3WoMwnBouoSNG2Xd3kYAcfLEmibXEpZH9HVDyDiassfPgiav8kx9wNDypGxaibxdQFIXzIhib2N2ibuo07/0"; + FileDownload fileDownload = new FileDownload(url); + fileDownload.download(3); + } +} diff --git "a/group26/2441547139/\346\234\211\351\201\223\347\254\224\350\256\260.txt" "b/group26/2441547139/\346\234\211\351\201\223\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000..31173ac162 --- /dev/null +++ "b/group26/2441547139/\346\234\211\351\201\223\347\254\224\350\256\260.txt" @@ -0,0 +1,3 @@ +http://note.youdao.com/noteshare?id=63cfdc5e194deb6458f9733681088516 +http://note.youdao.com/noteshare?id=3078d5c9a9f86e590973ee40ba3fdb25 +http://note.youdao.com/noteshare?id=8fb7ea6223b152c647f09dc98882751b lucene򵥽 diff --git "a/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" "b/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" index 165922cc87..463477e992 100644 --- "a/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" +++ "b/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" @@ -11,7 +11,7 @@ | | | | | | | | 2070509107 | 已完成 | | | | | | | | | | | | -| lizhy2017 | 部分完成 | 已完成 | | | | +| lizhy2017           |                   已完成                   |   已完成 |     |  已完成   |     | | | | | | | | | JEE-逆水百川 | 部分完成 | | | | | | | http://blog.csdn.net/u012759397/article/details/61618612 | | | | | diff --git a/group26/723161901/jvm/loader/ClassFileLoader.java b/group26/723161901/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..a87ca911dd --- /dev/null +++ b/group26/723161901/jvm/loader/ClassFileLoader.java @@ -0,0 +1,63 @@ +package com.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws IOException { + File f = new File(clzPaths.get(0)+File.separatorChar+className+".class"); + if(!f.exists()){ + throw new FileNotFoundException("File not found"); + } + ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); + BufferedInputStream in = null; + try { + in = new BufferedInputStream(new FileInputStream(f)); + int buf_size = 1024; + byte[] buffer = new byte[buf_size]; + int len = 0; + while(-1 != (len = in.read(buffer, 0, buf_size))){ + bos.write(buffer, 0, len); + } + return bos.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + }finally { + try { + in.close(); + } catch (Exception e2) { + e2.printStackTrace(); + } + bos.close(); + } + return null; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + String results = ""; + for (int i = 0; i < clzPaths.size(); i++) { + results += clzPaths.get(i); + if(i!=clzPaths.size()-1){ + results += ";"; + } + } + return results; + } + +} diff --git a/group26/723161901/jvm/test/ClassFileloaderTest.java b/group26/723161901/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..bd835c3857 --- /dev/null +++ b/group26/723161901/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,94 @@ +package com.jvm.test; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.jvm.loader.ClassFileLoader; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "/Users/Macx/Workspaces/coding2017/mini-jvm/bin/com/jvm/test"; + static String path2 = "/Users/Macx/Workspaces/coding2017/mini-jvm/bin/com/jvm/temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() throws IOException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1034, byteCodes.length); + + } + + + @Test + public void testMagicNumber() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + try { + URLConnection con = url.openConnection(); + return con.getContentLength(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group26/723161901/src/com/download/download/impl/ConnectionManagerImpl.java b/group26/723161901/src/com/download/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..acb0c3ea38 --- /dev/null +++ b/group26/723161901/src/com/download/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.impl; + +import com.api.Connection; +import com.api.ConnectionException; +import com.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String urlStr) throws ConnectionException { + + return new ConnectionImpl(urlStr); + } + +} diff --git a/group26/723161901/src/com/litestruts/LoginAction.java b/group26/723161901/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..b27743e9a8 --- /dev/null +++ b/group26/723161901/src/com/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group26/723161901/src/com/litestruts/Struts.java b/group26/723161901/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..51cb708e26 --- /dev/null +++ b/group26/723161901/src/com/litestruts/Struts.java @@ -0,0 +1,91 @@ +package com.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import com.litestruts.strutsBean.Action; + + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + StrutsXmlReader strutsXml = new StrutsXmlReader(new File("src/com/litestruts/struts.xml")); + View view = new View(); + try { + HashMap actMap = (HashMap) strutsXml.loadXml(); + Action act = (Action) actMap.get(actionName); + Class clazz = Class.forName(act.getClazz()); + Object obj = clazz.newInstance(); + HashMap paraMap = act.getParameters(); + Iterator> iteraPara = parameters.entrySet().iterator(); + + while(iteraPara.hasNext()){ + Entry itera = iteraPara.next(); + Field field = clazz.getDeclaredField(itera.getKey()); + field.setAccessible(true); + field.set(obj, itera.getValue()); + } + + Method method = clazz.getMethod("execute", null); + String results = (String)method.invoke(obj, null); + Field[] getFields = clazz.getDeclaredFields(); + HashMap getMapPara = new HashMap(); + for (Field field : getFields) { + field.setAccessible(true); + String getFiledName = field.getName(); + Object objValue = field.get(obj); + getMapPara.put(getFiledName, objValue); + } + + view.setParameters(getMapPara); + view.setJsp((String)paraMap.get(results)); + + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + + +} diff --git a/group26/723161901/src/com/litestruts/StrutsTest.java b/group26/723161901/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..48614832d0 --- /dev/null +++ b/group26/723161901/src/com/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package com.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group26/723161901/src/com/litestruts/StrutsXmlReader.java b/group26/723161901/src/com/litestruts/StrutsXmlReader.java new file mode 100644 index 0000000000..6470bc1d96 --- /dev/null +++ b/group26/723161901/src/com/litestruts/StrutsXmlReader.java @@ -0,0 +1,59 @@ +package com.litestruts; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import com.litestruts.strutsBean.Action; + +public class StrutsXmlReader { + private File file; + private HashMap actMap = new HashMap(); + + public StrutsXmlReader(File file) { + super(); + this.file = file; + } + + + public Map loadXml() throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(file); + NodeList nl = doc.getElementsByTagName("action"); + for (int i = 0; i < nl.getLength(); i++) { + Element book = (Element) nl.item(i); + String name = book.getAttribute("name"); + String claz = book.getAttribute("class"); + Action act = new Action(name, claz); + System.out.println(name); + System.out.println(claz); + NodeList bookNode = book.getChildNodes(); + HashMap paraMap = new HashMap(); + for (int j = 0; j < bookNode.getLength(); j++) { + Node bookCh = bookNode.item(j); + if (bookCh.getNodeType() == Node.ELEMENT_NODE) { + String valTag = bookCh.getTextContent(); + NamedNodeMap attrs = bookCh.getAttributes(); + String resultsValue = attrs.getNamedItem("name").getNodeValue(); + paraMap.put(resultsValue, valTag); + } + act.setParameters(paraMap); + } + actMap.put(act.getName(), act); + } + return actMap; + } +} diff --git a/group26/723161901/src/com/litestruts/View.java b/group26/723161901/src/com/litestruts/View.java new file mode 100644 index 0000000000..af63dce301 --- /dev/null +++ b/group26/723161901/src/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group26/723161901/src/com/litestruts/strutsBean/Action.java b/group26/723161901/src/com/litestruts/strutsBean/Action.java new file mode 100644 index 0000000000..37435653f2 --- /dev/null +++ b/group26/723161901/src/com/litestruts/strutsBean/Action.java @@ -0,0 +1,46 @@ +package com.litestruts.strutsBean; + +import java.util.HashMap; + +public class Action { + private String name; + private String clazz; + private HashMap parameters; + + public Action() { + super(); + } + + public Action(String name, String clazz) { + super(); + this.name = name; + this.clazz = clazz; + } + + public Action(String name, String clazz, HashMap parameters) { + super(); + this.name = name; + this.clazz = clazz; + this.parameters = parameters; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClazz() { + return clazz; + } + public void setClazz(String clazz) { + this.clazz = clazz; + } + public HashMap getParameters() { + return parameters; + } + public void setParameters(HashMap parameters) { + this.parameters = parameters; + } + + +} diff --git a/group26/89460886/src/week03/source/download/DownloadThread.java b/group26/89460886/src/week03/source/download/DownloadThread.java new file mode 100644 index 0000000000..75ba05c62b --- /dev/null +++ b/group26/89460886/src/week03/source/download/DownloadThread.java @@ -0,0 +1,40 @@ +package coding.coderising.download; + +import coding.coderising.download.api.Connection; + +import java.io.IOException; + +/** + * @author jiaxun + */ +public class DownloadThread extends Thread{ + + private Connection connection; + private int startPos; + private int endPos; + private byte[] downloadByte; + + public DownloadThread(Connection connection, int startPos, int endPos) { + this.connection = connection; + this.startPos = startPos; + this.endPos = endPos; + } + + @Override + public void run() { + try { + downloadByte = connection.read(startPos, endPos); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public byte[] getDownloadByte() { + return downloadByte; + } + + public int getStartPos() { + return startPos; + } + +} diff --git a/group26/89460886/src/week03/source/download/FileDownloader.java b/group26/89460886/src/week03/source/download/FileDownloader.java new file mode 100644 index 0000000000..9cf09cbd40 --- /dev/null +++ b/group26/89460886/src/week03/source/download/FileDownloader.java @@ -0,0 +1,101 @@ +package coding.coderising.download; + +import coding.coderising.download.api.Connection; +import coding.coderising.download.api.ConnectionManager; +import coding.coderising.download.api.DownloadListener; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +/** + * @author jiaxun + */ +public class FileDownloader { + + private static final int THREAD_COUNT = 3; + + private int threadCount; + private String downloadUrl; + private DownloadListener downloadListener; + private ConnectionManager connectionManager; + private String savePath; + + public FileDownloader(String downloadUrl, String savePath) { + this.downloadUrl = downloadUrl; + this.savePath = savePath; + this.threadCount = THREAD_COUNT; + } + + public void execute() { + Connection connection = null; + RandomAccessFile out = null; + try { + connection = connectionManager.open(downloadUrl); + int length = connection.getContentLength(); + connection.close(); + + int downloadOffset = 0; + List threadList = new ArrayList<>(); + for (int i = 0; i < threadCount; i++) { + DownloadThread thread = new DownloadThread(connectionManager.open(downloadUrl), downloadOffset, downloadOffset + (i + 1) * (length / threadCount)); + threadList.add(thread); + thread.start(); + downloadOffset = (i + 1) * (length / threadCount) + 1; + } + if (downloadOffset < length) { + DownloadThread thread = new DownloadThread(connectionManager.open(downloadUrl), downloadOffset, length - 1); + threadList.add(thread); + thread.start(); + } + + for (DownloadThread thread : threadList) { + thread.join(); + } + + File file = new File(savePath); + out = new RandomAccessFile(file, "rwd"); + for (DownloadThread thread : threadList) { + out.seek(thread.getStartPos()); + out.write(thread.getDownloadByte()); + } + + if (downloadListener != null) { + downloadListener.notifyFinished(); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (connection != null) { + connection.close(); + } + try { + if (out != null) { + out.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public void setConnectionManager(ConnectionManager connectionManager) { + this.connectionManager = connectionManager; + } + + public void setDownloadListener(DownloadListener downloadListener) { + this.downloadListener = downloadListener; + } + + public DownloadListener getDownloadListener() { + return this.downloadListener; + } + + public void setThreadCount(int threadCount) { + this.threadCount = threadCount; + } + +} diff --git a/group26/89460886/src/week03/source/download/api/Connection.java b/group26/89460886/src/week03/source/download/api/Connection.java new file mode 100644 index 0000000000..59db422b34 --- /dev/null +++ b/group26/89460886/src/week03/source/download/api/Connection.java @@ -0,0 +1,16 @@ +package coding.coderising.download.api; + +import java.io.IOException; + +/** + * @author jiaxun + */ +public interface Connection { + + byte[] read(int startPos, int endPos) throws IOException; + + int getContentLength(); + + void close(); + +} diff --git a/group26/89460886/src/week03/source/download/api/ConnectionException.java b/group26/89460886/src/week03/source/download/api/ConnectionException.java new file mode 100644 index 0000000000..1e1b2a3299 --- /dev/null +++ b/group26/89460886/src/week03/source/download/api/ConnectionException.java @@ -0,0 +1,10 @@ +package coding.coderising.download.api; + +/** + * @author jiaxun + */ +public class ConnectionException extends Exception { + + + +} diff --git a/group26/89460886/src/week03/source/download/api/ConnectionManager.java b/group26/89460886/src/week03/source/download/api/ConnectionManager.java new file mode 100644 index 0000000000..47bb30e9fb --- /dev/null +++ b/group26/89460886/src/week03/source/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package coding.coderising.download.api; + +/** + * @author jiaxun + */ +public interface ConnectionManager { + + Connection open(String url) throws ConnectionException; + +} diff --git a/group26/89460886/src/week03/source/download/api/DownloadListener.java b/group26/89460886/src/week03/source/download/api/DownloadListener.java new file mode 100644 index 0000000000..8202d6cbad --- /dev/null +++ b/group26/89460886/src/week03/source/download/api/DownloadListener.java @@ -0,0 +1,10 @@ +package coding.coderising.download.api; + +/** + * @author jiaxun + */ +public interface DownloadListener { + + void notifyFinished(); + +} diff --git a/group26/89460886/src/week03/source/download/impl/ConnectionImpl.java b/group26/89460886/src/week03/source/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2cb3c342b3 --- /dev/null +++ b/group26/89460886/src/week03/source/download/impl/ConnectionImpl.java @@ -0,0 +1,43 @@ +package coding.coderising.download.impl; + +import coding.coderising.download.api.Connection; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +/** + * @author jiaxun + */ +public class ConnectionImpl implements Connection { + + private HttpURLConnection urlConnection; + + public ConnectionImpl(HttpURLConnection urlConnection) { + this.urlConnection = urlConnection; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + urlConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream input = urlConnection.getInputStream(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + int length; + byte[] byteArray = new byte[1024]; + while ((length = input.read(byteArray)) != -1) { + output.write(byteArray, 0, length); + } + return output.toByteArray(); + } + + @Override + public int getContentLength() { + return urlConnection.getContentLength(); + } + + @Override + public void close() { + urlConnection.disconnect(); + } +} diff --git a/group26/89460886/src/week03/source/download/impl/ConnectionManagerImpl.java b/group26/89460886/src/week03/source/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..359a3c4d16 --- /dev/null +++ b/group26/89460886/src/week03/source/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,27 @@ +package coding.coderising.download.impl; + +import coding.coderising.download.api.Connection; +import coding.coderising.download.api.ConnectionException; +import coding.coderising.download.api.ConnectionManager; + +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * @author jiaxun + */ +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String urlString) throws ConnectionException { + + try { + URL url = new URL(urlString); + return new ConnectionImpl((HttpURLConnection) url.openConnection()); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group26/89460886/src/week03/source/linkedlist/SinglyLinkedList.java b/group26/89460886/src/week03/source/linkedlist/SinglyLinkedList.java new file mode 100644 index 0000000000..a35cbfa944 --- /dev/null +++ b/group26/89460886/src/week03/source/linkedlist/SinglyLinkedList.java @@ -0,0 +1,357 @@ +package list; + +/** + * @author jiaxun + */ +public class SinglyLinkedList implements List { + + private Node head; + private int size; + + public SinglyLinkedList() { + size = 0; + } + + public void addFirst(Object data) { + Node node = new Node(data); + node.setNext(head); + head = node; + size++; + } + + public Node removeFirst() { + Node object = head; + head = object.getNext(); + size--; + return object; + } + + public Node removeLast() { + Node curr = head; + Node prev = null; + while (curr != null) { + prev = curr; + curr = curr.getNext(); + } + if (prev != null) { + prev.setNext(null); + } + size--; + return curr; + } + + public Node get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException(); + } + Node curr = head; + while (curr != null) { + if (index == 0) + break; + curr = curr.getNext(); + index--; + + } + return curr; + } + + public Node remove(int index) { + Node curr = head; + Node prev = null; + while (curr != null) { + if (index == 0) + break; + prev = curr; + curr = curr.getNext(); + index--; + } + if (prev != null) { + prev.setNext(curr.getNext()); + curr.setNext(null); + } + size--; + return curr; + } + + public void addLast(Object object) { + if (head == null) { + head = new Node(object); + } else { + Node curr = head; + Node prev = null; + while (curr != null) { + prev = curr; + curr = curr.getNext(); + } + prev.setNext(new Node(object)); + } + size++; + } + + @Override + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object object) { + Node curr = head; + Node prev = null; + while (curr != null) { + if (index == 0) + break; + prev = curr; + curr = curr.getNext(); + index--; + } + if (prev != null) { + Node newNode = new Node(object); + newNode.setNext(curr); + prev.setNext(newNode); + size++; + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new SinglyLinkedListIterator(this); + } + + public void reverse() { + if (head == null || head.getNext() == null) return; + Node prev = null; + Node next = null; + Node curr = head; + while (curr != null) { + next = curr.getNext(); + curr.setNext(prev); + prev = curr; + curr = next; + } + head = prev; + } + + public void removeFirstHalf() { + if (head == null) return; + int half = size / 2; + Node curr = head; + while (half != 0) { + curr = curr.getNext(); + half--; + } + head = curr; + } + + public void remove(int i, int length) { + if (head == null || length == 0 || i >= size) return; + if (i + length >= size) length = size - i - 1; + Node prev = head; + Node curr = head; + int firstPos = i; + while (curr != null) { + if (firstPos == 0) + break; + prev = curr; + curr = curr.getNext(); + firstPos--; + } + int lastPos = length - i; + while (curr != null) { + if (lastPos == 0) + break; + curr = curr.getNext(); + lastPos--; + } + prev.setNext(curr == null ? null : curr.getNext()); + } + + public int[] getElements(SinglyLinkedList list) { + if (list == null || list.size() == 0) return null; + int[] resultList = new int[list.size()]; + int offset = 0; + int count = 0; + Node curr = head; + for (int i = 0, len = list.size(); i < len; i++) { + int index = (int) list.get(i).getData(); + index = index - offset; + offset = (int) list.get(i).getData(); + while (curr != null) { + if (index == 0) { + resultList[count++] = (int) curr.getData(); + break; + } + curr = curr.getNext(); + index--; + } + } + return resultList; + } + + public void subtract(SinglyLinkedList list) { + if (head == null || list == null) return; + Node curr = head; + Node prev = null; + int bCount = 0; + while (curr != null) { + if (bCount == list.size()) break; + int currData = (int) curr.getData(); + int bData = (int) list.get(bCount).getData(); + if (currData == bData) { + if (prev != null) { + prev.setNext(curr.getNext()); + } else { + head = curr.getNext(); + } + bCount++; + } else { + prev = curr; + } + curr = curr.getNext(); + } + } + + public void removeDuplicateValues() { + if (size <= 1) return; + Node prev = head; + Node curr = head.getNext(); + while (curr != null) { + if (prev.getData().equals(curr.getData())) { + if (curr.getNext() != null) { + curr = curr.getNext(); + } else { + curr = curr.getNext(); + prev.setNext(null); + } + } else { + prev.setNext(curr); + prev = curr; + curr = curr.getNext(); + } + } + } + + public void removeRange(int min, int max) { + if (head == null || (int) head.getData() > max) return; + Node prev = null; + Node next = null; + Node curr = head; + boolean lessHead = false; + if ((int) head.getData() > min) { + prev = head; + lessHead = true; + } + while (curr != null) { + int data = (int) curr.getData(); + if (!lessHead && data < min) { + prev = curr; + } + if (data > max) { + next = curr; + } + curr = curr.getNext(); + } + if (prev != null) { + if (prev == head && lessHead) { + head = next; + } else { + prev.setNext(next); + } + } + } + + public SinglyLinkedList intersection(SinglyLinkedList list) { + SinglyLinkedList resultList = new SinglyLinkedList(); + Node aCurr = head; + Node bCurr = list.head; + while (aCurr != null && bCurr != null) { + int a = (int) aCurr.getData(); + int b = (int) bCurr.getData(); + if (a < b) { + resultList.add(aCurr.getData()); + aCurr = aCurr.getNext(); + } else if (a > b) { + resultList.add(bCurr.getData()); + bCurr = bCurr.getNext(); + } else { + resultList.add(aCurr.getData()); + aCurr = aCurr.getNext(); + bCurr = bCurr.getNext(); + } + } + while (aCurr != null) { + resultList.add(aCurr.getData()); + aCurr = aCurr.getNext(); + } + while (bCurr != null) { + resultList.add(bCurr.getData()); + bCurr = bCurr.getNext(); + } + return resultList; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + Node current = head; + while (current != null) { + builder.append(current.toString()); + current = current.getNext(); + } + return builder.toString(); + } + + private class SinglyLinkedListIterator implements Iterator { + + private SinglyLinkedList linkedList; + private int currentPosition = 0; + + public SinglyLinkedListIterator(SinglyLinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return currentPosition < size; + } + + @Override + public Object next() { + return linkedList.get(currentPosition++); + } + + @Override + public Object remove() { + return linkedList.remove(--currentPosition); + } + } + + public static class Node { + + private Object data; + private Node next; + + public Node(Object data) { + this.data = data; + } + + public Object getData() { + return data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + @Override + public String toString() { + return "[data is " + getData() + "]"; + } + } + +} diff --git a/group26/89460886/src/week03/test/TestDownload.java b/group26/89460886/src/week03/test/TestDownload.java new file mode 100644 index 0000000000..7bfcb8e589 --- /dev/null +++ b/group26/89460886/src/week03/test/TestDownload.java @@ -0,0 +1,57 @@ +package coding.coderising.download; + +import coding.coderising.download.api.ConnectionManager; +import coding.coderising.download.api.DownloadListener; +import coding.coderising.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author jiaxun + */ +public class TestDownload { + + private static boolean downloaderFinished = false; + + @Before + public void setUp() { + + } + + @After + public void tearDown() { + + } + + @Test + public void testDownload() { + String downloadUrl = "http://img.ithome.com/newsuploadfiles/2017/3/20170324_152202_144.jpg"; + String savePath = "/Users/jiaxun/Downloads/download_thread.jpg"; + + FileDownloader downloader = new FileDownloader(downloadUrl, savePath); + + ConnectionManager connectionManager = new ConnectionManagerImpl(); + downloader.setConnectionManager(connectionManager); + + downloader.setDownloadListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloaderFinished = true; + } + }); + + downloader.execute(); + + while (!downloaderFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } + +} diff --git a/group26/89460886/src/week03/test/TestSinglyLinkedList.java b/group26/89460886/src/week03/test/TestSinglyLinkedList.java new file mode 100644 index 0000000000..8ae36aed9a --- /dev/null +++ b/group26/89460886/src/week03/test/TestSinglyLinkedList.java @@ -0,0 +1,156 @@ +package list; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author jiaxun + */ +public class TestSinglyLinkedList { + + SinglyLinkedList singlyLinkedList; + + @Before + public void setUp() { + singlyLinkedList = new SinglyLinkedList(); + } + + @After + public void tearDown() { + + } + + @Test + public void testReverse() { + singlyLinkedList.add(3); + singlyLinkedList.add(7); + singlyLinkedList.add(10); + singlyLinkedList.reverse(); + int[] resultList = {10, 7, 3}; + for (int i = 0, len = resultList.length; i < len; i++) { + Assert.assertEquals(resultList[i], singlyLinkedList.get(i).getData()); + } + } + + @Test + public void testRemoveFirstHalf() { + singlyLinkedList.add(2); + singlyLinkedList.add(5); + singlyLinkedList.add(7); + singlyLinkedList.add(8); + singlyLinkedList.add(10); + singlyLinkedList.removeFirstHalf(); + int[] resultList = {7, 8, 10}; + for (int i = 0, len = resultList.length; i < len; i++) { + Assert.assertEquals(resultList[i], singlyLinkedList.get(i).getData()); + } + } + + @Test + public void testRemove() { + singlyLinkedList.add(1); + singlyLinkedList.add(2); + singlyLinkedList.add(3); + singlyLinkedList.add(4); + int[] resultList = {1, 3, 4}; + singlyLinkedList.remove(1, 1); + for (int i = 0, len = resultList.length; i < len; i++) { + Assert.assertEquals(singlyLinkedList.get(i).getData(), resultList[i]); + } + } + + @Test + public void testGetElements() { + singlyLinkedList.add(11); + singlyLinkedList.add(101); + singlyLinkedList.add(201); + singlyLinkedList.add(301); + singlyLinkedList.add(401); + singlyLinkedList.add(501); + singlyLinkedList.add(601); + singlyLinkedList.add(701); + SinglyLinkedList listB = new SinglyLinkedList(); + listB.add(1); + listB.add(3); + listB.add(4); + listB.add(6); + int[] expectedArray = {101, 301, 401, 601}; + int[] resultArray = singlyLinkedList.getElements(listB); + for (int i = 0, len = expectedArray.length; i < len; i++) { + Assert.assertEquals(expectedArray[i], resultArray[i]); + } + } + + @Test + public void testSubtract() { + singlyLinkedList.add(11); + singlyLinkedList.add(101); + singlyLinkedList.add(201); + singlyLinkedList.add(301); + singlyLinkedList.add(401); + singlyLinkedList.add(501); + singlyLinkedList.add(601); + singlyLinkedList.add(701); + SinglyLinkedList listB = new SinglyLinkedList(); + listB.add(101); + listB.add(301); + listB.add(401); + listB.add(601); + singlyLinkedList.subtract(listB); + int[] expectedArray = {11, 201, 501, 701}; + for (int i = 0, len = expectedArray.length; i < len; i++) { + Assert.assertEquals(expectedArray[i], singlyLinkedList.get(i).getData()); + } + } + + @Test + public void testRemoveDuplicateValues() { + singlyLinkedList.add(11); + singlyLinkedList.add(101); + singlyLinkedList.add(201); + singlyLinkedList.add(201); + singlyLinkedList.add(301); + singlyLinkedList.add(301); + singlyLinkedList.add(301); + singlyLinkedList.removeDuplicateValues(); + int[] expectedArray = {11, 101, 201, 301}; + for (int i = 0, len = expectedArray.length; i < len; i++) { + Assert.assertEquals(expectedArray[i], singlyLinkedList.get(i).getData()); + } + } + + @Test + public void testRemoveRange() { + singlyLinkedList.add(11); + singlyLinkedList.add(101); + singlyLinkedList.add(201); + singlyLinkedList.add(301); + singlyLinkedList.add(401); + singlyLinkedList.removeRange(10, 400); + int[] expectedArray = {401}; + for (int i = 0, len = expectedArray.length; i < len; i++) { + Assert.assertEquals(expectedArray[i], singlyLinkedList.get(i).getData()); + } + } + + @Test + public void testIntersection() { + singlyLinkedList.add(12); + singlyLinkedList.add(18); + singlyLinkedList.add(32); + singlyLinkedList.add(98); + SinglyLinkedList bList = new SinglyLinkedList(); + bList.add(34); + bList.add(51); + bList.add(53); + bList.add(78); + SinglyLinkedList resultList = singlyLinkedList.intersection(bList); + int[] expectedArray = {12, 18, 32, 34, 51, 53, 78, 98}; + for (int i = 0, len = expectedArray.length; i < len; i++) { + Assert.assertEquals(expectedArray[i], resultList.get(i).getData()); + } + } + +} diff --git a/group26/89460886/src/week04/source/LRUPageFrame.java b/group26/89460886/src/week04/source/LRUPageFrame.java new file mode 100644 index 0000000000..44b7ad7a45 --- /dev/null +++ b/group26/89460886/src/week04/source/LRUPageFrame.java @@ -0,0 +1,159 @@ +package list; + +/** + * @author jiaxun + */ +public class LRUPageFrame { + + private int capacity; + private Node first; + private Node last; + private int size = 0; + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + public void access(int pageNum) { + if (size < capacity) { + addFirst(pageNum); + } else { + Node node = searchNode(pageNum); + if (node == null) { + removeLast(); + addFirst(pageNum); + } else { + if (node.getData() == first.getData()) return; + if (node.getData() == last.getData()) { + last = node.getPrev(); + node.getPrev().setNext(null); + node.setPrev(null); + node.setNext(first); + first.setPrev(node); + first = node; + } else { + node.getPrev().setNext(node.getNext()); + node.getNext().setPrev(node.getPrev()); + node.setNext(first); + node.setPrev(null); + first = node; + } + } + } + } + + public Node searchNode(int pageNum) { + Node curr = first; + while (curr != null) { + if (curr.getData() == pageNum) { + return curr; + } + curr = curr.getNext(); + } + return null; + } + + public void addFirst(int data) { + Node node = new Node(data); + if (first == null) { + first = node; + } else if (last == null) { + last = first; + first = node; + first.setNext(last); + last.setPrev(first); + } else { + node.setNext(first); + first.setPrev(node); + first = node; + } + size++; + } + + public void addLast(int data) { + Node node = new Node(data); + if (first == null) { + first = node; + } else if (last == null) { + last = node; + first.setNext(last); + last.setPrev(first); + } else { + node.setPrev(last); + last.setNext(node); + last = node; + } + size++; + } + + public void removeLast() { + if (last != null && last.getPrev() != null) { + Node prev = last.getPrev(); + last.getPrev().setNext(null); + last = prev.getData() == first.getData() ? null : last.getPrev(); + } else if (first != null) { + first = null; + } + if (size > 0) { + size--; + } + } + + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + Node curr = first; + while (curr != null) { + builder.append(curr.getData()); + if (curr.getNext() != null) { + builder.append(","); + } + curr = curr.getNext(); + } + return builder.toString(); + } + + private static class Node { + + private int data; + private Node prev; + private Node next; + + public Node(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public Node getPrev() { + return prev; + } + + public void setPrev(Node prev) { + this.prev = prev; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + @Override + public String toString() { + return "[data is " + data + "]"; + } + } +} diff --git a/group26/89460886/src/week04/test/TestLRUPageFrame.java b/group26/89460886/src/week04/test/TestLRUPageFrame.java new file mode 100644 index 0000000000..a82a2512ac --- /dev/null +++ b/group26/89460886/src/week04/test/TestLRUPageFrame.java @@ -0,0 +1,32 @@ +package list; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author jiaxun + */ +public class TestLRUPageFrame { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group26/lizhy2017/homework/second/array/ArrayUtil.java b/group26/lizhy2017/homework/second/array/ArrayUtil.java index 253356ef7d..1e7855bd1b 100644 --- a/group26/lizhy2017/homework/second/array/ArrayUtil.java +++ b/group26/lizhy2017/homework/second/array/ArrayUtil.java @@ -9,14 +9,14 @@ public class ArrayUtil { * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] * - * @param origin + * @param origin 整形数组 */ - public void reverseArray(int[] origin) { - if (null != origin) return; - for (int i = 0; i < origin.length; i++) { + public static void reverseArray(int[] origin) { + if (null == origin) return; + for (int i = 0; i < origin.length / 2; i++) { int temp = origin[i]; origin[i] = origin[origin.length - i - 1]; - origin[i] = temp; + origin[origin.length - i - 1] = temp; } } @@ -29,16 +29,18 @@ public void reverseArray(int[] origin) { * @return */ - public int[] removeZero(int[] oldArray) { - if (null != oldArray) { - return null; - } + public static int[] removeZero(int[] oldArray) { + if (null == oldArray) return null; + int size = oldArray.length; for (int i = 0; i < oldArray.length; i++) { if (oldArray[i] == 0) { System.arraycopy(oldArray, i + 1, oldArray, i, oldArray.length - i - 1); + size--; } } - return oldArray; + int[] newArray = new int[size]; + System.arraycopy(oldArray, 0, newArray, 0, size); + return newArray; } /** @@ -50,30 +52,34 @@ public int[] removeZero(int[] oldArray) { * @return */ - public int[] merge(int[] array1, int[] array2) { - if (null != array1 && null != array2) return null; + public static int[] merge(int[] array1, int[] array2) { + if (null == array1 || null == array2) return null; int[] temp = new int[array1.length + array2.length]; int i = 0, j = 0; if (array1.length >= array2.length) { while (i < array2.length) { - i++; if (array1[i] <= array2[i]) temp[i] = array1[i]; else temp[i] = array2[i]; + i++; } - System.arraycopy(array1, i + 1, temp, i + 1, temp.length - i - 1); + System.arraycopy(array1, i - 1, temp, 2 * i - 1, temp.length - 2 * i - 1); } else { - while (j < array1.length) { - j++; - if (array1[j] <= array2[j]) + while (j < array1.length - 1) { + if (array1[j] <= array2[j]) { temp[j] = array1[j]; - else + if (array1[j + 1] > array2[j]) { + temp[j] = array2[j]; + } + } else temp[j] = array2[j]; + j++; + } - System.arraycopy(array1, j + 1, temp, j + 1, temp.length - j - 1); + System.arraycopy(array2, j - 1, temp, 2 * j - 1, temp.length - 2 * j - 1); } - return null; + return temp; } /** @@ -86,7 +92,7 @@ public int[] merge(int[] array1, int[] array2) { * @param size * @return */ - public int[] grow(int[] oldArray, int size) { + public static int[] grow(int[] oldArray, int size) { int oldCapacity = oldArray.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity < size) { @@ -106,7 +112,7 @@ public int[] grow(int[] oldArray, int size) { * @param max * @return */ - public int[] fibonacci(int max) { + public static int[] fibonacci(int max) { if (max <= 1) return new int[0]; int[] temp = new int[max]; @@ -131,7 +137,7 @@ public int[] fibonacci(int max) { * @param max * @return */ -// public int[] getPrimes(int max) { +// public static int[] getPrimes(int max) { // int[] temp = new int[max]; // if (max < 2) // return new int[0]; @@ -165,7 +171,7 @@ public int[] fibonacci(int max) { * @param max * @return */ - public int[] getPerfectNumbers(int max) { + public static int[] getPerfectNumbers(int max) { int[] temp = new int[max]; int index = 0; for (int i = 1; i <= max; i++) { @@ -192,7 +198,7 @@ public int[] getPerfectNumbers(int max) { * @param seperator * @return */ - public String join(int[] array, String seperator) { + public static String join(int[] array, String seperator) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < array.length; i++) { if (i == array.length - 1) { diff --git a/group26/lizhy2017/homework/second/array/ArrayUtilTest.java b/group26/lizhy2017/homework/second/array/ArrayUtilTest.java new file mode 100644 index 0000000000..58827657da --- /dev/null +++ b/group26/lizhy2017/homework/second/array/ArrayUtilTest.java @@ -0,0 +1,90 @@ +package second.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * ${} + * Created by spark_lizhy on 2017/3/20. + */ + +public class ArrayUtilTest { + + private int[] temp; + private int size; + + @Before + public void setUp() throws Exception { + size = 10; + temp = new int[size]; + for (int i = 0; i < size; i++) { + temp[i] = i; + } + + } + + @Test + public void reverseArray() throws Exception { + ArrayUtil.reverseArray(temp); + for (int i = 0; i < size; i++) { + Assert.assertEquals(size - 1 - i, temp[i]); + } + } + + @Test + public void removeZero() throws Exception { + for (int i = 0; i < size; i++) { + if (i % 5 == 0) { + temp[i] = 0; + } else { + temp[i] = i; + } + } + + temp = ArrayUtil.removeZero(temp); + Assert.assertNotNull(temp); + for (int i = 0; i < temp.length; i++) { + if (temp[i] != 0) { + continue; + } + Assert.assertEquals(temp[i], i); + } + + // 测试空数组 + { + int[] testArray = new int[5]; + for (int i = 0; i < 5; i++) { + testArray[i] = 0; + } + + int[] newArray = ArrayUtil.removeZero(testArray); + Assert.assertNotNull(newArray); + Assert.assertEquals(newArray.length, 0); + } + } + + @Test + public void merge() throws Exception { + // 构建数组 + int[] array1 = new int[10]; + int[] array2 = new int[11]; + array2[10] = 100; + for (int i = 0; i < 10; i++) { + if (i % 2 == 0) { + array1[i / 2] = i; // 0, 2, 4, 6, 8 + } else { + array2[i / 2] = i; // 1, 3, 5, 7, 9 + } + } + + // 测试merge + { + int[] merge = ArrayUtil.merge(array1, array2); + Assert.assertNotNull(merge); + Assert.assertEquals(merge.length, 21); + } + + } + +} diff --git a/group26/lizhy2017/homework/third/basic/LRUPageFameTest.java b/group26/lizhy2017/homework/third/basic/LRUPageFameTest.java new file mode 100644 index 0000000000..32bf061852 --- /dev/null +++ b/group26/lizhy2017/homework/third/basic/LRUPageFameTest.java @@ -0,0 +1,32 @@ +package third.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * ${} + * Created by spark_lizhy on 2017/3/31. + */ + +public class LRUPageFameTest { + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } +} diff --git a/group26/lizhy2017/homework/third/basic/LRUPageFrame.java b/group26/lizhy2017/homework/third/basic/LRUPageFrame.java new file mode 100644 index 0000000000..29fa687bba --- /dev/null +++ b/group26/lizhy2017/homework/third/basic/LRUPageFrame.java @@ -0,0 +1,63 @@ +package third.basic; + +/** + * ${} + * Created by spark_lizhy on 2017/3/31. + */ +/** + * 用双向链表实现 LRU 算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @return + */ + public void access(int pageNum) { + + + } + + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group26/lizhy2017/homework/third/basic/LinkedList.java b/group26/lizhy2017/homework/third/basic/LinkedList.java new file mode 100644 index 0000000000..49265f7b31 --- /dev/null +++ b/group26/lizhy2017/homework/third/basic/LinkedList.java @@ -0,0 +1,204 @@ +package third.basic; + +import java.util.Objects; + +/** + * ${} + * Created by spark_lizhy on 2017/3/31. + */ + +public class LinkedList { + private Node mHead; + private Node mCurrent; + private int mSize = 0; + + public void add(T o) { + addLast(o); + mSize++; + + } + + public void add(int index, T o) { + checkIndex(index); + + Node next = find(index); + Node pre = next.previous; + Node current = new Node<>(o, next, pre); + next.previous = current; + pre.next = current; + mSize++; + + } + + private Node find(int index) { + Node tra = mHead; + if (index < (mSize >> 1)) { + for (int i = 0; i <= index; i++) { + tra = tra.next; + } + } else { + for (int i = mSize; i > index; i--) { + tra = tra.previous; + } + } + return tra; + } + + private void checkIndex(int index) { + if (index >= mSize || index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + " Size:" + mSize); + } + } + + public Object get(int index) { + checkIndex(index); + + return find(index).data; + } + + public T remove(int index) { + checkIndex(index); + //重链接 + Node temp = this.find(index); + Node next = temp.next; + Node pre = temp.previous; + pre.next = next; + next.previous = pre; + //清除数据 + T removedObject = temp.data; + temp.data = null; + temp.next = null; + temp.previous = null; + mSize--; + return removedObject; + } + + public int size() { + return mSize; + } + + public void addFirst(T o) { + Node next = mHead.next; + Node first = new Node<>(o, next, mHead); + next.next = first; + next.previous = first; + mSize++; + + } + + public void addLast(T o) { + Node last = mHead.previous; + Node temp = new Node<>(o, mHead, last); + mHead.previous = temp; + last.next = temp; + mSize++; + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(mSize - 1); + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果 list = 2->5->7->8->10 , 删除以后的值为 7,8,10 + */ + public void removeFirstHalf() { + + } + + /** + * 从第 i 个元素开始, 删除 length 个元素 , 注意 i 从 0 开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和 listB 均包含已升序排列的整数 + * 从当前链表中取出那些 listB 所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是 [101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在 listB 中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于 min 且小于 max 的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数 list 指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表 C,其元素为当前链表和 list 中元素的交集,且表 C 中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } + + + private static class Node { + T data; + Node next; + Node previous; + + public Node(T data) { + this.data = data; + this.next = this; + this.previous = this; + } + + public Node(T data, Node next, Node previous) { + this.data = data; + this.next = next; + this.previous = previous; + } + } +} diff --git a/group26/lizhy2017/homework/third/download/DownloadThread.java b/group26/lizhy2017/homework/third/download/DownloadThread.java new file mode 100644 index 0000000000..9fa8cb2659 --- /dev/null +++ b/group26/lizhy2017/homework/third/download/DownloadThread.java @@ -0,0 +1,51 @@ +package third.download; + + +import java.io.IOException; +import java.io.RandomAccessFile; + +import third.download.api.Connection; +import third.download.api.ConnectionException; +import third.download.api.DownloadListener; + +public class DownloadThread extends Thread { + + private RandomAccessFile accessFile; + private DownloadListener listener; + private Connection conn; + private int startPos; + private int endPos; + + public DownloadThread(Connection conn, int startPos, int endPos, DownloadListener listener) { + this.listener = listener; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + + } + + public void run() { + try { + byte[] bytes = conn.read(startPos, endPos); + accessFile = new RandomAccessFile("./" + conn.getFileName(), "rw"); + accessFile.seek(startPos); + accessFile.write(bytes); + } catch (IOException e) { + e.printStackTrace(); + } catch (ConnectionException e) { + e.printStackTrace(); + } finally { + if (null != accessFile) + try { + accessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + if (null != conn) + conn.close(); + if (null != listener) + listener.notifyFinished(); + } + } +} diff --git a/group26/lizhy2017/homework/third/download/FileDownloader.java b/group26/lizhy2017/homework/third/download/FileDownloader.java new file mode 100644 index 0000000000..498b09ee97 --- /dev/null +++ b/group26/lizhy2017/homework/third/download/FileDownloader.java @@ -0,0 +1,86 @@ +package third.download; + +import java.util.concurrent.atomic.AtomicInteger; + +import third.download.api.Connection; +import third.download.api.ConnectionException; +import third.download.api.ConnectionManager; +import third.download.api.DownloadListener; + + +public class FileDownloader { + private final static int THREAD_NUM=15; + private String url; + private DownloadListener listener; + private ConnectionManager cm; + private AtomicInteger atomicInteger=new AtomicInteger(); + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + int length = cm.getContentLength(url); + int perTread_lenth=length/THREAD_NUM; + int redundant=length%THREAD_NUM; + for (int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + } + + return baos.toByteArray(); + + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..8b7e9cc665 --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java b/group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java new file mode 100644 index 0000000000..6c127cb054 --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java @@ -0,0 +1,57 @@ +package com.coderising.download.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class ConnectionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + //测试连接url的功能 + public void testContentLength() throws Exception{ + //new一个接口,然后实现这个接口 + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + //测试读入,确保设计的接口ok + public void testRead() throws Exception{ + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + + // 测试不充分,没有断言内容是否正确 + } + + + + +} diff --git a/group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java b/group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java new file mode 100644 index 0000000000..40f01fd9ac --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java @@ -0,0 +1,66 @@ +package com.coderising.download.test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.FileDownloader; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + /*题 一般的时候是网络连接的问题。还有你可以设置一下 服务器的超时时间。有的可能是三十秒。你试试更长点的。 + * 还有一种可能性是。你程序里创建了很多connection 但是没有关闭调。现在数据库处于半死状态,然后连接超时。 + * 你ping的是服务器的ip吗? 你可以用plsql检验一下你的网络是否通。还有配置服务器数据源的时候 如果能配置成功, + * 那网络也没问题。 + */ + //String url = "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + + FileDownloader downloader = new FileDownloader(url,"e:\\项目练手\\test.jpg"); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group27/1016908591/week03/src/com/coding/basic/LinkedList.java b/group27/1016908591/week03/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..8efd566a00 --- /dev/null +++ b/group27/1016908591/week03/src/com/coding/basic/LinkedList.java @@ -0,0 +1,283 @@ +package com.coding.basic; +import javax.xml.crypto.Data; + + + +public class LinkedList implements List { + + private Node head; + private int length; + //构造函数 + public LinkedList(){ + clear(); + } + public final void clear(){ + head = null; + length = 0; + } + + public void add(Object o){ + Node newNode = new Node(o); + if(length == 0) + { + head = newNode; + } + else{ + Node lastNode = getNodeAt(length); + lastNode.next = newNode; + + } + length++; + + + } + public void add(int index , Object o){ + Node newNode = new Node(o); + Node nodeBefor = getNodeAt(index-1); + Node nodeAfter = nodeBefor.next; + newNode.next = nodeAfter; + nodeBefor.next = newNode; + length++; + + + } + public Object get(int index){ + if((1<=index)&&(index<=length)) + { + Node currentNode = head; + for(int i= 0;i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + Node lastNode = getNodeAt(length); + head = lastNode; + while(length>0){ + Node currentNode = getNodeAt(--length); + add(currentNode); + + } + + + + + + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int num = length/2; + while(num>0){ + remove(num); + num--; + } + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + while (length>0){ + remove(i+length); + length--; + } + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] arr = new int[list.size()]; + + for(int i =0;idata) + remove(i); + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + if(list==null){ + return null; + } + int i1 = 0; + int i2 = 0; + LinkedList result = new LinkedList(); + Node currentListNode = list.head; + Node currentThisNode = this.head; + for(i1 =0;i1 clzPaths = new ArrayList(); + /* + public byte[] readBinaryCode(String className) { + className = className.replace('.', File.separatorChar); + + for(String path:this.clzPaths) + { + String clzFileName = path + File.separatorChar+className; + byte[] codes = loadClassFile_V2(clzFileName); + if(codes != null){ + return codes; + } + + } + + return null; + + + } + + private byte[] loadClassFile_V2(String clzFileName) { + File f= new File(clzFileName); + try { + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (Exception e) { + + return null; + } + + } + + //第一种加载类的方法 + private byte[] loadClassFile_V1(String clzFileName) { + /*BufferedInputStream bis = null; + try{ + File f = new File(clzFileName); + bis = new BufferedInputStream(new FileInputStream(f)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = -1; + while((length = bis.read(buffer))!=-1) + { + bos.write(buffer,0,length); + + } + byte[] codes = bos.toByteArray(); + return codes; + }catch(IOException e){ + e.printStackTrace(); + return null; + }*/ + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)) + { + return; + } + this.clzPaths.add(path); + + } + + public String getClassPath() { + int count = 0; + String clzP = null; + for(String clzPathName:clzPaths){ + if(count=capacity){ + removeLast(); + } + addNewNodeAtHead(node); + + } + + } + + + + private void moveExistingNodeToHead(Node node) { + if(first.pageNum == node.pageNum ){ + return; + }else if(last.pageNum == node.pageNum) { + Node lastToHead = last; + last = last.prev; + last.next = null; + lastToHead.prev = null; + lastToHead.next = first; + first.prev = lastToHead; + first = lastToHead; + + + + }else { + Node MiddleNode = first.next; + first.next = last; + last.prev = first; + MiddleNode.next = first; + MiddleNode.prev = null; + first.prev = MiddleNode; + first = MiddleNode; + + + } + + } + + private void addNewNodeAtHead(Node node) { + if(isEmpty()){ + node.prev = null; + node.next = null; + first = node; + last = node; + }else{ + node.prev = null; + node.next =first; + first.prev = node; + first = node; + + + } + curreantSize++; + + + } + + private boolean isEmpty() { + // TODO Auto-generated method stub + return (curreantSize==0); + } + + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev ; + this.curreantSize--; + + } + + private Node find(int pageNum) { + Node node = first; + while(node!=null){ + if(node.pageNum==pageNum){ + return node; + } + node = node.next; + + + } + return null; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group27/1016908591/week04/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group27/1016908591/week04/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..67cf36067b --- /dev/null +++ b/group27/1016908591/week04/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java b/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java index a96d7287fb..4370a34a3d 100644 --- a/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java +++ b/group27/1252327158/task1_20170312/src/com/coding/ArrayListTest.java @@ -9,7 +9,7 @@ import org.junit.Test; public class ArrayListTest { - + ArrayList list; @Before diff --git a/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java b/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java index 168bb07592..7226968638 100644 --- a/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java +++ b/group27/1252327158/task1_20170312/src/com/coding/LinkedList.java @@ -183,7 +183,16 @@ public Node(T data, Node node) { * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ - + if (size <= 1) { + return; + } + Node node = head; + while (node.next != null) { + Node temp = node.next; + node.next = temp.next; + temp.next = head; + head = temp; + } } /** @@ -193,7 +202,11 @@ public void reverse(){ */ public void removeFirstHalf(){ - + if (size < 2) { + return; + } + int delSize = (int)Math.floor(size/2); + remove(0, delSize); } /** @@ -202,8 +215,31 @@ public void removeFirstHalf(){ * @param length */ public void remove(int i, int length){ + if (i < 0 || i >= size || length < 0 || i + length > size) { + throw new IndexOutOfBoundsException(); + } + if (i == 0) { + head = removeStartWith(head, length); + return; + } + Node beforeStart = head; //被删除元素的前一个 + for (int index = 1; index < i; index++) { + beforeStart = beforeStart.next; + } + beforeStart.next = removeStartWith(beforeStart.next, length); + } + private Node removeStartWith(Node startNode, int length) { + Node node = null; + for (int index = 1; index <= length; index++) { + node = startNode; + startNode = startNode.next; + node.next = null; + size--; + } + return startNode; } + /** * 假定当前链表和list均包含已升序排列的整数 * 从当前链表中取出那些list所指定的元素 @@ -212,8 +248,29 @@ public void remove(int i, int length){ * 返回的结果应该是[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ - return null; + public int[] getElements(LinkedList list){ + if (size == 0 || list == null || list.size == 0) { + return new int[0]; + } + int[] result = new int[list.size]; + Node node = head; + int index = 0; + int resultIndex = 0; + for (int i = 0; i < size; i++ ) { + int listData = ((Integer)list.get(index)).intValue(); + if ( listData >= size) { + throw new IndexOutOfBoundsException(); + } + if (i == listData) { + result[resultIndex++] = ((Integer)node.data).intValue(); + index++; + } + if (index == list.size || listData == size) { + break; + } + node = node.next; + } + return result; } /** @@ -224,7 +281,35 @@ public static int[] getElements(LinkedList list){ */ public void subtract(LinkedList list){ - + if (list == null || list.size() == 0) { + return; + } + Node node = head; + Node beforeNode = null; + Node temp = null; + int j = 0; //参数list索引 + for (;node != null && j < list.size() ;) { + int paradata = ((Integer)list.get(j)).intValue(); + int data = ((Integer)node.data).intValue(); + if (data == paradata) { + j++; + size--; + temp = node; + if (beforeNode == null) { + head = node.next; + node = node.next; + } else {; + beforeNode.next = node.next; + node = node.next; + } + temp.next = null; + } else if (data < paradata) { + beforeNode = node; + node = node.next; + } else { + j++; + } + } } /** @@ -232,7 +317,21 @@ public void subtract(LinkedList list){ * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + if (size < 2) { + return; + } + Node node = head; + Node delNode = null; + while (node.next != null) { + if (((Integer)node.next.data).equals(node.data)) { + delNode = node.next; + node.next = node.next.next; + delNode.next = null; + size--; + } else { + node = node.next; + } + } } /** @@ -242,7 +341,27 @@ public void removeDuplicateValues(){ * @param max */ public void removeRange(int min, int max){ - + if (min >= max) { + return; + } + Node node = head; + int delLen = 0; + int startIndex = -1; + for (int i = 0; i < size; i++) { + int currentData = ((Integer)node.data).intValue(); + if (currentData > min && currentData < max) { + if (delLen == 0) { + startIndex = i; + } + delLen++; + } else if (currentData >= max) { + break; + } + node = node.next; + } + if (delLen > 0) { + remove(startIndex, delLen); + } } /** @@ -251,6 +370,24 @@ public void removeRange(int min, int max){ * @param list */ public LinkedList intersection( LinkedList list){ - return null; + if (list.size() == 0 || size == 0) { + return null; + } + LinkedList result = new LinkedList(); + Node node = head; + Iterator listIter = list.iterator(); + while (listIter.hasNext()) { + int listData = ((Integer)listIter.next()).intValue(); + for (;node != null;) { + int currentData = ((Integer)node.data).intValue(); + if (currentData == listData) { + result.addLast(currentData); + } else if (currentData > listData) { + break; + } + node = node.next; + } + } + return result; } } diff --git a/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java b/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java index 03f7198998..c9a2504964 100644 --- a/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java +++ b/group27/1252327158/task1_20170312/src/com/coding/LinkedListTest.java @@ -8,7 +8,8 @@ import org.junit.Test; public class LinkedListTest { - LinkedList list; + + LinkedList list; @Before public void setUp() throws Exception { @@ -92,4 +93,118 @@ public void testIterator() { } } + @Test + public void reverse() throws Exception { + list.add("third"); + list.add("forth"); + Assert.assertEquals("forth", list.get(0)); + list.reverse(); + Assert.assertEquals("first", list.get(0)); + Assert.assertEquals("second", list.get(1)); + Assert.assertEquals("third", list.get(2)); + Assert.assertEquals("forth", list.get(3)); + } + + @Test + public void removeFirstHalf() throws Exception { + list.add("third"); + list.add("forth"); + list.removeFirstHalf(); + Assert.assertEquals("second", list.get(0)); + Assert.assertEquals(2, list.size()); + } + + @Test + public void remove() throws Exception { + list.add("third"); + list.add("forth"); + list.remove(1, 2); + Assert.assertEquals("forth", list.get(0)); + Assert.assertEquals("first", list.get(1)); + } + + @Test + public void getElements() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + intList.addLast(501); + intList.addLast(601); + intList.addLast(701); + LinkedList searchList = new LinkedList<>(); + searchList.addLast(1); + searchList.addLast(3); + searchList.addLast(4); + searchList.addLast(7); + + Assert.assertArrayEquals(new int[]{101,301,401,701}, intList.getElements(searchList)); + } + + @Test + public void subtract() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + LinkedList delList= new LinkedList<>(); + delList.addLast(11); + delList.addLast(101); + delList.addLast(301); + delList.addLast(401); + intList.subtract(delList); + Assert.assertEquals(201, ((Integer)intList.get(0)).intValue()); + Assert.assertEquals(1, intList.size()); + } + + @Test + public void removeDuplicateValues() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(101); + intList.addLast(101); + intList.addLast(401); + intList.removeDuplicateValues(); + Assert.assertEquals(11, ((Integer)intList.get(0)).intValue()); + Assert.assertEquals(101, ((Integer)intList.get(1)).intValue()); + Assert.assertEquals(401, ((Integer)intList.get(2)).intValue()); + Assert.assertEquals(3, intList.size()); + } + + @Test + public void removeRange() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + intList.removeRange(11, 301); + Assert.assertEquals(3, intList.size()); + Assert.assertEquals(11, ((Integer)intList.get(0)).intValue()); + Assert.assertEquals(301, ((Integer)intList.get(1)).intValue()); + } + + @Test + public void intersection() throws Exception { + LinkedList intList = new LinkedList<>(); + intList.addLast(11); + intList.addLast(101); + intList.addLast(201); + intList.addLast(301); + intList.addLast(401); + LinkedList paraList= new LinkedList<>(); + paraList.addLast(11); + paraList.addLast(301); + paraList.addLast(501); + LinkedList newList = intList.intersection(paraList); + Assert.assertEquals(2, newList.size()); + Assert.assertEquals(11, ((Integer)newList.get(0)).intValue()); + Assert.assertEquals(301, ((Integer)newList.get(1)).intValue()); + } } diff --git a/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java b/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java index ce0ec1a04c..3d5ed76bb3 100644 --- a/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java +++ b/group27/1252327158/task2_20170319/litestruts/src/com/coderising/litestruts/Struts.java @@ -39,17 +39,28 @@ public static View runAction(String actionName, Map parameters) { */ - XMLHandle xmlHandle; + Document document = null; Class action = null; Object object = null; - + try { + SAXReader saxReader = new SAXReader(); + document = saxReader.read(new File("struts.xml")); // 读取XML文件,获得document对象 + Element root = document.getRootElement(); + for (Iterator it = root.elementIterator(); it.hasNext();) { + Element element = it.next(); + if (element.attribute("name").getValue().equals(actionName)) { + String className = element.attribute("class").getValue(); + action = Class.forName(className); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + if (action == null) { + return null; + } + try { - xmlHandle = new XMLHandle(); - String className = xmlHandle.getClassName(actionName); - if (className == null) { - return null; - } - action = Class.forName(className); object = action.newInstance(); Iterator> mapIt = parameters.entrySet().iterator(); while (mapIt.hasNext()) { @@ -58,28 +69,35 @@ public static View runAction(String actionName, Map parameters) { for (Method method : methods) { if (method.getName().equalsIgnoreCase("set" + entry.getKey())) { method.invoke(object, entry.getValue()); + break; } - } + } +// methods = action.getDeclaredMethods(); +// for (Method method : methods) { +// if (method.getName().equalsIgnoreCase("get" + entry.getKey())) { +// String name = (String)method.invoke(object); +// System.out.println(name); +// break; +// } +// } } - + } + } + Method execute = action.getDeclaredMethod("execute"); String result = (String)execute.invoke(object); - Field[] fields = action.getDeclaredFields(); + Method[] methods = action.getMethods(); Map map = new HashMap(); - for (Field field : fields) { - Method[] methods = action.getMethods(); - for (Method method : methods) { - if (method.getName().equalsIgnoreCase("get" + field.getName())) { - map.put(field.getName(), (String)method.invoke(object)); - } + for (Method method : methods) { + if (method.getName().startsWith("get")) { + map.put(method.getName().substring(3).toLowerCase(), (String)method.invoke(object)); } } + View view = new View(); - view.setParameters(map); - String jspResult = xmlHandle.getResult(actionName, result); - view.setJsp(jspResult); - return view; + view.setParameters(map); + return view; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { @@ -92,10 +110,6 @@ public static View runAction(String actionName, Map parameters) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); - } catch (DocumentException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); } diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/DownloadThread.java b/group27/1252327158/task3_20170326/download/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..58b8a4e3a6 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/DownloadThread.java @@ -0,0 +1,79 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread{ + + private int endPos; + private int startPos; + private String url; + private String destFilePath; + private ConnectionManager connManager; + private DownloadListener downloadListener; + + public DownloadThread(ConnectionManager connManager, String url, int startPos, int endPos, String destFilePath, + DownloadListener downloadListener) { + + this.url = url; + this.endPos = endPos; + this.startPos = startPos; + this.connManager = connManager; + this.destFilePath = destFilePath; + this.downloadListener = downloadListener; + } + + @Override + public void run() { + Connection conn = null; + RandomAccessFile randomAccessFile = null; + try { + doLog("BIN"); + conn = connManager.open(url, startPos, endPos); + byte[] read = conn.read(startPos, endPos); + String _filePath = destFilePath; + if (_filePath == null || _filePath.length() == 0) { + _filePath = conn.getFileName(); + } + randomAccessFile = new RandomAccessFile(_filePath, "rw"); + randomAccessFile.seek(startPos); + randomAccessFile.write(read); + doLog("END"); + } catch (IOException e) { + doLog("EXP1"); + e.printStackTrace(); + } catch (com.coderising.download.api.ConnectionException e) { + doLog("EXP2"); + e.printStackTrace(); + } finally { + if (randomAccessFile != null) { + try { + randomAccessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (conn != null) { + conn.close(); + } + if (downloadListener != null) { + downloadListener.notifyFinished(); + } + } + } + + private void doLog(String action) { + System.out.println( + "*********** " + action + + " [" + + startPos + + "-" + + endPos + + "]" + + " ***********"); + } +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloader.java b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..ee3a321b81 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloader.java @@ -0,0 +1,83 @@ +package com.coderising.download; + +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.util.concurrent.atomic.AtomicInteger; + + +public class FileDownloader { + + private String url; + + private DownloadListener listener; + + private ConnectionManager cm; + + private AtomicInteger atomicInteger; + + public FileDownloader(String _url) { + this.url = _url; + atomicInteger = new AtomicInteger(); + } + + /** + * 在这里实现你的代码, 注意: 需要用多线程实现下载 + * 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + * (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + * (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + * 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + * 具体的实现思路: + * 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + * 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + * 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + * 3. 把byte数组写入到文件中 + * 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + * + * 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + */ + public void execute() { + try { + + int threadCount = 5; + int length = this.cm.getContentLength(this.url); + for (int i = 0; i < threadCount; i++) { + + int threadLoadLength = length / threadCount; + int startPos = threadLoadLength * i; + int endPos; + if (i != threadCount - 1) { + endPos = threadLoadLength * (i + 1) - 1; + } else { + endPos = length - 1; + } + atomicInteger.getAndIncrement(); + new DownloadThread(cm, this.url, startPos, endPos, "download_file.jpeg", new DownloadListener() { + @Override + public void notifyFinished() { + if (atomicInteger.decrementAndGet() == 0) { + if (FileDownloader.this.listener != null) { + FileDownloader.this.listener.notifyFinished(); + } + } + } + }).start(); + } + } catch (ConnectionException e) { + e.printStackTrace(); + } + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return this.listener; + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } +} \ No newline at end of file diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloaderTest.java b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..865d65510d --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489721424&di=1fda6467501ab1d5e5bff43e801d14ee&imgtype=jpg&er=1&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201507%2F30%2F20150730163204_A24MX.thumb.700_0.jpeg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/Connection.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0795bca536 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/Connection.java @@ -0,0 +1,30 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); + + /** + * 获取下载文件的文件名 + * + * @return 文件名 + */ + String getFileName(); +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionException.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..bd8934095f --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,11 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + public ConnectionException(Exception e) { + super(e); + } + + public ConnectionException(String msg) { + super(msg); + } +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionManager.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..69321679fa --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,21 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * + * @param url 连接地址 + * @param startPos 读取文件的起始位置 + * @param endPos 读取文件的结束位置 + * @return 连接 + */ + Connection open(String url, int startPos, int endPos) throws ConnectionException; + + /** + * 获取文件长度 + * + * @param url 连接地址 + * @return 文件长度 + */ + int getContentLength(String url) throws ConnectionException; +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/api/DownloadListener.java b/group27/1252327158/task3_20170326/download/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionImpl.java b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..0a367ea1d1 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,94 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private static final int BUFFER_SIZE = 4096; + private HttpURLConnection httpConn; + private String fileUrl; + private InputStream inputStream; + + public ConnectionImpl(HttpURLConnection httpConn, String fileUrl) { + this.httpConn = httpConn; + this.fileUrl = fileUrl; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + if (endPos < startPos) { + throw new IllegalArgumentException("argument endPos[" + endPos + "] less than startPos[" + startPos + "]"); + } + int bytesNeed2Read = endPos - startPos + 1; + if (bytesNeed2Read > getContentLength()) { + throw new IllegalArgumentException( + "endPos[" + endPos + "] is bigger than content length[" + getContentLength() + "]"); + } + + inputStream = httpConn.getInputStream(); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byte[] buffer = new byte[Math.min(bytesNeed2Read, BUFFER_SIZE)]; + int read; + + long startTime = System.currentTimeMillis(); + final long progressInterval = 2000; + while ((read = inputStream.read(buffer)) != -1) { + byteArrayOutputStream.write(buffer, 0, read); + + if (System.currentTimeMillis() - startTime > progressInterval) { + startTime = System.currentTimeMillis(); + System.out.println(String.format(Thread.currentThread().getName() + + " [%.2f%%]", byteArrayOutputStream.size() * 100.0 / bytesNeed2Read) + ); + } + } + System.out.println(String.format(Thread.currentThread().getName() + " [%.2f%%]", 100.0)); + System.out.println("bytes read: " + byteArrayOutputStream.size()); + + return byteArrayOutputStream.toByteArray(); + } + + @Override + public int getContentLength() { + if (httpConn != null) { + return httpConn.getContentLength(); + } + return 0; + } + + @Override + public void close() { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (httpConn != null) { + httpConn.disconnect(); + } + } + + @Override + public String getFileName() { + String disposition = httpConn.getHeaderField("Content-Disposition"); + if (disposition != null) { + // extracts file name from header field + int index = disposition.indexOf("filename="); + if (index > 0) { + return disposition.substring(index + 10, + disposition.length() - 1); + } + } + // extracts file name from URL + return fileUrl.substring(fileUrl.lastIndexOf("/") + 1, + fileUrl.length()); + } +} diff --git a/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..e88aa35647 --- /dev/null +++ b/group27/1252327158/task3_20170326/download/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,61 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String fileURL, int startPos, int endPos) throws ConnectionException { + try { + System.out.println("try to open file url: " + fileURL); + + URL url = new URL(fileURL); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + + // 设定读取range + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + System.out.println("Range: bytes=" + startPos + "-" + endPos); + + int responseCode = httpConn.getResponseCode(); + + System.out.println("server replied HTTP code: " + responseCode); + if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) { + System.out.println("return new ConnectionImpl"); + return new ConnectionImpl(httpConn, fileURL); + } else { + throw new ConnectionException("server replied HTTP code: " + responseCode); + } + } catch (IOException e) { + throw new ConnectionException(e); + } + } + + @Override + public int getContentLength(String fileURL) throws ConnectionException { + try { + System.out.println("try to open file url: " + fileURL); + + URL url = new URL(fileURL); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + int responseCode = httpConn.getResponseCode(); + + System.out.println("server replied HTTP code: " + responseCode); + if (responseCode == HttpURLConnection.HTTP_OK) { + System.out.println("return contentLength: " + httpConn.getContentLength()); + int contentLength = httpConn.getContentLength(); + httpConn.disconnect(); + return contentLength; + } else { + throw new ConnectionException("server replied HTTP code: " + responseCode); + } + } catch (IOException e) { + throw new ConnectionException(e); + } + } +} diff --git "a/group27/1252327158/task3_20170326/\345\217\246\344\270\200\344\270\252linkedlist\344\275\234\344\270\232\345\234\250task1\351\207\214" "b/group27/1252327158/task3_20170326/\345\217\246\344\270\200\344\270\252linkedlist\344\275\234\344\270\232\345\234\250task1\351\207\214" new file mode 100644 index 0000000000..710566766d --- /dev/null +++ "b/group27/1252327158/task3_20170326/\345\217\246\344\270\200\344\270\252linkedlist\344\275\234\344\270\232\345\234\250task1\351\207\214" @@ -0,0 +1 @@ +另一个linkedlist作业在task1里 diff --git a/group27/383117348/src/com/coderising/download/DownloadThread.java b/group27/383117348/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..e5068a4e44 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,61 @@ +package com.coderising.download; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + FileDownloader fileDown; + + public DownloadThread( Connection conn, int startPos, int endPos){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + FileOutputStream fos = null; + ByteArrayInputStream bis =null; + try { + byte[] bt = conn.read(startPos, endPos); + File file = new File("C:\\Users\\Adminstater\\Desktop\\test"+Math.ceil(Math.random()*100)+".jpg"); + if(!file.exists()){ + file.createNewFile(); + } + fos = new FileOutputStream(file); + bis = new ByteArrayInputStream(bt); + int i = 0; + byte[] copy = new byte[1024]; + while((i=bis.read(copy))!=-1){ + fos.write(copy, 0, i); + fos.flush(); + } + + DownloadListener listener = fileDown.getListener(); + listener.notifyFinished(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + try { + fos.close(); + bis.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } + public void setFileDown(FileDownloader fileDown) { + this.fileDown = fileDown; + } + +} diff --git a/group27/383117348/src/com/coderising/download/FileDownloader.java b/group27/383117348/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..7ede4cbb34 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,70 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm ; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + DownloadThread thread = new DownloadThread(conn,0,length-1); + thread.setFileDown(this); + thread.start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group27/383117348/src/com/coderising/download/FileDownloaderTest.java b/group27/383117348/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..c9c7b05c7d --- /dev/null +++ b/group27/383117348/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,55 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + String url = "http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"; + FileDownloader downloader = new FileDownloader(url); + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group27/383117348/src/com/coderising/download/api/Connection.java b/group27/383117348/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group27/383117348/src/com/coderising/download/api/ConnectionException.java b/group27/383117348/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group27/383117348/src/com/coderising/download/api/ConnectionManager.java b/group27/383117348/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group27/383117348/src/com/coderising/download/api/DownloadListener.java b/group27/383117348/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java b/group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..26477be3eb --- /dev/null +++ b/group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,71 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URL url; + private HttpURLConnection connection; + + public ConnectionImpl(String url) { + try { + this.url = new URL(url); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + private HttpURLConnection initConnection(){ + HttpURLConnection con = null; + try { + con = (HttpURLConnection)url.openConnection(); + con.setConnectTimeout(2000); + con.setRequestMethod("GET"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return con; + } + @Override + public byte[] read(int startPos, int endPos) throws IOException { + connection = initConnection(); + connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); //nullPointException + InputStream input = connection.getInputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int i=0; + byte[] bt = new byte[1024]; + while((i=input.read(bt))!=-1){ + bos.write(bt,0,i); + } + return bos.toByteArray(); + } + + @Override + public int getContentLength() { + HttpURLConnection con = initConnection(); + try { + if (con.getResponseCode() == 200){ + //服务器返回内容的长度,本质就是文件的长度 + return con.getContentLength(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return 0; + } + + @Override + public void close() { + this.connection=null; + } + +} diff --git a/group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..18836b4a28 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group27/383117348/src/com/coderising/jvm/loader/ClassFileLoader.java b/group27/383117348/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..a1eb5cc155 --- /dev/null +++ b/group27/383117348/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,77 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + className = className.replace(".", "\\"); + File file = null; + for(String classPath : clzPaths){ + file = new File(classPath + "\\" + className + ".class"); + if(file.exists()){ + break; + } + } + if(!file.exists()){ + try { + throw new ClassNotFoundException(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length()); + BufferedInputStream in = null; + try { + in = new BufferedInputStream(new FileInputStream(file)); + int buf_size = 1024; + byte[] buffer = new byte[buf_size]; + int len = 0; + while (-1 != (len = in.read(buffer, 0, buf_size))) { + bos.write(buffer, 0, len); + } + return bos.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + bos.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return null; + } + + public void addClassPath(String path) { + if (path != null && path.length() > 0) { + if (!clzPaths.contains(path)) { + clzPaths.add(path); + } + } + } + + public String getClassPath() { + String paths = ""; + for (String s : clzPaths) { + paths += s + ";"; + } + paths = paths.substring(0, paths.length() - 1); + return paths; + } + +} diff --git a/group27/383117348/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group27/383117348/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..2ac63afcd9 --- /dev/null +++ b/group27/383117348/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,76 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + +public class ClassFileloaderTest { + + static String path1 = "E:\\MyGit\\coding2017\\group27\\383117348\\bin"; + static String path2 = "C:\\temp"; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + System.out.println(clzPath); + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java b/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..67735a92b0 --- /dev/null +++ b/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group27/383117348/src/com/coding/basic/LinkedList.java b/group27/383117348/src/com/coding/basic/LinkedList.java deleted file mode 100644 index faeafffca6..0000000000 --- a/group27/383117348/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,382 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -import org.junit.Test; - -public class LinkedList implements List { - - private Node first; - - private Node last; - - private int size; - - /** - * 头部添加节点方法 - */ - private void linkFirst(Object o) { - final Node node = first; - final Node firstNode = new Node(o, node, null); - this.first = firstNode; - if (node == null) { - last = firstNode; - } else { - node.prev = firstNode; - } - size++; - } - - /** - * 末端添加节点方法 - * - * @param o - */ - private void linkLast(Object o) { - final Node node = last; - final Node lastNode = new Node(o, null, node); - last = lastNode; - if (node == null) { - first = lastNode; - } else { - node.next = lastNode; - } - size++; - } - - /** - * 在链表末端添加节点 - */ - public void add(Object o) { - if (o != null) - linkLast(o); - } - - /** - * 在指定下标处添加节点 - */ - public void add(int index, Object o) { - checkIndex(index); - Node befo = getNodeByIndex(index); - linkBefore(o, befo); - } - - /** - * 根据下标获取节点 - */ - public Object get(int index) { - checkIndex(index); - return getNodeByIndex(index).data; - } - - /** - * 根据下标移除节点 - */ - public Object remove(int index) { - checkIndex(index); - Node current = getNodeByIndex(index); - final Node node = current; - final Node prev = node.prev; - final Node next = node.next; - if (prev == null) { - first = next; - } else { - prev.next = next; - next.prev = prev; - current = null; - } - if (next == null) { - last = prev; - } else { - prev.next = next; - next.prev = prev; - current = null; - } - size--; - return node.data; - } - - /** - * 返回链表长度 - */ - public int size() { - return size; - } - - /** - * 在链表头添加节点 - * - * @param o - */ - public void addFirst(Object o) { - linkFirst(o); - } - - /** - * 在链表最后添加节点 - * - * @param o - */ - public void addLast(Object o) { - linkLast(o); - } - - /** - * 移除链表首个元素 - * - * @return - */ - public Object removeFirst() { - Node node = first; - if (node == null) - throw new NoSuchElementException(); - else { - Node next = node.next; - if (next == null) - first = null; - else { - first = next; - first.prev = null; - } - } - size--; - return node.data; - } - - /** - * 移除链表最后一个元素 - * - * @return - */ - public Object removeLast() { - Node node = last; - if (last == null) - throw new NoSuchElementException(); - else { - Node prev = node.prev; - if (prev == null) - last = null; - else { - last = prev; - last.next = null; - } - } - size--; - return node.data; - } - - /** - * 迭代方法 - * - * @return - */ - public Iterator iterator() { - return new Iter(); - } - - /** - * 在节点之前插入一个新的节点 - * - * @param data - * @param befo - */ - private void linkBefore(Object data, Node befo) { - final Node prev = befo.prev; - final Node node = new Node(data, befo, prev); - befo.prev = node; - if (prev == null) - first = node; - else - prev.next = node; - size++; - } - - /** - * 根据下标获取节点 - * - * @param index - * @return - */ - private Node getNodeByIndex(int index) { - checkIndex(index); - - Node node = first; - for (int x = 0; x < index; x++) { - node = node.next; - } - return node; - } - - /** - * 检查下标是否越界 - * - * @param index - */ - private void checkIndex(int index) { - // TODO Auto-generated method stub - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("链表下标越界"); - } - } - - /** - * 迭代器内部类 - * - * @author Adminstater - * - */ - private class Iter implements Iterator { - private int nextIndex = 0; - - @Override - public boolean hasNext() { - // TODO Auto-generated method stub - return nextIndex != size; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - int i = nextIndex++; - if (i > size - 1) - throw new IndexOutOfBoundsException(); - return getNodeByIndex(i).data; - } - - } - - /** - * 节点内部类 - * - * @author Adminstater - * - */ - private static class Node { - Object data; - Node next; - Node prev; - - private Node(Object data, Node next, Node prev) { - this.data = data; - this.next = next; - this.prev = prev; - } - - private Node() { - - } - } - - /*------------------------------------------------------单元测试----------------------------------------------------*/ - - /** - * 测试添加方法功能 - */ - // add(Object o) - @Test - public void TestAddFunction() { - - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - System.out.println("添加完毕"); - - } - - // add(int index,Object o) - @Test - public void TestAddIndexFunction() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - for (int x = 0; x < list.size(); x++) { - System.out.println(list.get(x)); - } - list.add(3, 111); - System.out.println(list.get(3)); - } - - // addFirst(Object o) - @Test - public void TestAddFirstFunction() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - System.out.println(list.get(0)); - list.addFirst(20); - System.out.println(list.get(0)); - } - - // addLast(Object o) - @Test - public void TestAddLastFunction() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - System.out.println(list.get(list.size() - 1)); - list.addLast(111); - System.out.println(list.get(list.size() - 1)); - } - - /** - * remove方法测试 - */ - // removeFirst() - @Test - public void TestRemoveFirst() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - System.out.println(list.get(0)); - list.removeFirst(); - System.out.println(list.get(0)); - } - - // removeLast() - @Test - public void TestRemoveLast() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - System.out.println(list.get(list.size() - 1)); - list.removeLast(); - System.out.println(list.get(list.size() - 1)); - } - - // remove(int index) - @Test - public void TestRemoveFunction() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - System.out.println(list.get(50)); - list.remove(50); - System.out.println(list.get(50)); - } - - /** - * Iterator测试 - */ - @Test - public void TestIterator() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.add(i); - } - Iterator ite = list.iterator(); - while (ite.hasNext()) { - System.out.println(ite.next()); - } - } - - public static void main(String[] args) { - java.util.LinkedList list = null; - } -} diff --git a/group27/383117348/src/com/coding/basic/Queue.java b/group27/383117348/src/com/coding/basic/Queue.java index 84cb43e3db..4bd32c067b 100644 --- a/group27/383117348/src/com/coding/basic/Queue.java +++ b/group27/383117348/src/com/coding/basic/Queue.java @@ -2,6 +2,8 @@ import org.junit.Test; +import com.coding.basic.linklist.LinkedList; + public class Queue { private int size = 0; private LinkedList linkedList = new LinkedList(); diff --git a/group27/383117348/src/com/coding/basic/linklist/LRUPageFrame.java b/group27/383117348/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..b50f3df5e8 --- /dev/null +++ b/group27/383117348/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,139 @@ +package com.coding.basic.linklist; + + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + /** + * LRU算法: + */ + private int capacity; + private int size; + + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + Node node = checkExist(pageNum); + //如果这个页面在缓存中存在 + if (node != null) { + // 将当前节点移动至第一个 + moveTofirst(node); + } else { + //不存在后比较当前是否已经满队列了 + if (size < capacity) { + //如果还有空闲队列 + // 添加一个节点在栈顶 + final Node n = first; + final Node firstNode = new Node(); + firstNode.next = n; + firstNode.pageNum = pageNum; + firstNode.prev = null; + this.first = firstNode; + if (n == null) { + last = firstNode; + } else { + n.prev = firstNode; + } + size++; + } else { + //否则,添加一个节点在栈顶,栈底的节点移除 + addNode(pageNum); + } + } + } + //如果超出了缓存范围,则添加到栈顶,栈底的节点移除 + private void addNode(int pageNum) { + Node node = new Node(); + node.pageNum = pageNum; + node.next = first; + first.prev = node; + first = node; + last = last.prev; + last.next = null; + } + /** + * 如果在队列中存在,则移动至首位 + * @param node + */ + private void moveTofirst(Node node) { + if(node==first){ + return; + } + if(node==last){ + first.prev = node; + node.next = first; + first = node; + last = node.prev; + last.next = null; + first.prev = null; + + }else{ + node.prev.next = node.next; + node.next.prev = node.prev; + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + + } + + /** + * 检查是否在队列中存在页数 + * @param pageNum + * @return + */ + private Node checkExist(int pageNum) { + Node node = first; + for(int i=0;i size) { + throw new IndexOutOfBoundsException("链表下标越界"); + } + } + + /** + * 迭代器内部类 + * + * @author Adminstater + * + */ + private class Iter implements Iterator { + private int nextIndex = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return nextIndex != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + int i = nextIndex++; + if (i > size - 1) + throw new IndexOutOfBoundsException(); + return getNodeByIndex(i).data; + } + + } + + /** + * 节点内部类 + * + * @author Adminstater + * + */ + private static class Node { + Object data; + Node next; + Node prev; + + private Node(Object data, Node next, Node prev) { + this.data = data; + this.next = next; + this.prev = prev; + } + + private Node() { + + } + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (size == 0) + return; + + for (int i = 1; i < size; i++) { + addFirst(get(i)); + remove(i + 1); + } + + } + + @Test + public void testReverse() { + LinkedList list = getList(); + Iterator ite = list.iterator(); + while (ite.hasNext()) { + System.out.print(ite.next() + " "); + } + list.reverse(); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.print("----"); + System.out.print(it.next() + " "); + } + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + if (size == 0) + return; + + int removeNum = size / 2; + for (int i = 0; i < removeNum; i++) { + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (length + i > size || i < 0 || i >= size) + return; + + for (int k = i; k < (length + i); k++) { + remove(i); + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + int[] array = new int[list.size]; + for (int i = 0; i < array.length; i++) { + int element = (int) list.get(i); + array[i] = ((Integer) get(element)); + } + + return array; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + int length = list.size(); + for (int i = size - 1; i >= 0; i--) { + for (int j = 0; j < length; j++) { + if (get(i) == list.get(j)) { + remove(i); + break; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + for (int i = size - 1; i > 0; i--) { + if (get(i) == get(i - 1)) { + remove(i); + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + for (int i = size - 1; i >= 0; i--) { + int element = ((int) get(i)); + if ((element > min) && element < max) { + remove(i); + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList newList = new LinkedList(); + for (int i = 0; i < size; i++) { + for (int j = 0; j < list.size; j++) { + if (get(i).equals(list.get(j))) { + newList.add(list.get(j)); + break; + } + } + } + return newList; + } + + /*------------------------------------------------------单元测试----------------------------------------------------*/ + + /** + * 测试添加方法功能 + */ + // add(Object o) + @Test + public void TestAddFunction() { + + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println("添加完毕"); + + } + + // add(int index,Object o) + @Test + public void TestAddIndexFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + for (int x = 0; x < list.size(); x++) { + System.out.println(list.get(x)); + } + list.add(3, 111); + System.out.println(list.get(3)); + } + + // addFirst(Object o) + @Test + public void TestAddFirstFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(0)); + list.addFirst(20); + System.out.println(list.get(0)); + } + + // addLast(Object o) + @Test + public void TestAddLastFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(list.size() - 1)); + list.addLast(111); + System.out.println(list.get(list.size() - 1)); + } + + /** + * remove方法测试 + */ + // removeFirst() + @Test + public void TestRemoveFirst() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(0)); + list.removeFirst(); + System.out.println(list.get(0)); + } + + // removeLast() + @Test + public void TestRemoveLast() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(list.size() - 1)); + list.removeLast(); + System.out.println(list.get(list.size() - 1)); + } + + // remove(int index) + @Test + public void TestRemoveFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(50)); + list.remove(50); + System.out.println(list.get(50)); + } + + /** + * Iterator测试 + */ + @Test + public void TestIterator() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + Iterator ite = list.iterator(); + while (ite.hasNext()) { + System.out.println(ite.next()); + } + } + + private LinkedList getList() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + return list; + } +} diff --git a/group27/513274874/data-structure/down.jpg b/group27/513274874/data-structure/down.jpg new file mode 100644 index 0000000000..d7fccb49d2 Binary files /dev/null and b/group27/513274874/data-structure/down.jpg differ diff --git a/group27/513274874/data-structure/src/com/coderising/download/DownloadThread.java b/group27/513274874/data-structure/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..605f1ba10c --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,30 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + // 将下载到的字节输出到raf中 + private RandomAccessFile raf ; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + try { + byte[] buff = conn.read(startPos,endPos); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/FileDownloader.java b/group27/513274874/data-structure/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..347c6bc93c --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.api.ConnectionManager; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/FileDownloaderTest.java b/group27/513274874/data-structure/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..85aeeff9fc --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/api/Connection.java b/group27/513274874/data-structure/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/api/ConnectionException.java b/group27/513274874/data-structure/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/api/ConnectionManager.java b/group27/513274874/data-structure/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/api/DownloadListener.java b/group27/513274874/data-structure/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/demo/DownThread.java b/group27/513274874/data-structure/src/com/coderising/download/demo/DownThread.java new file mode 100644 index 0000000000..18b21da57c --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/demo/DownThread.java @@ -0,0 +1,72 @@ +package com.coderising.download.demo; + +import java.io.InputStream; +import java.io.RandomAccessFile; + +public class DownThread extends Thread { + + // 定义字节数组(取水的竹筒)的长度 + private final int BUFF_LEN = 32; + + // 定义下载的起始点 + private long start; + + // 定义下载的结束点 + private long end; + + // 下载资源对应的输入流 + private InputStream is; + + // 将下载到的字节输出到raf中 + private RandomAccessFile raf; + + + // 构造器,传入输入流,输出流和下载起始点、结束点 + public DownThread(long start, long end, InputStream is, RandomAccessFile raf) { + // 输出该线程负责下载的字节位置 + System.out.println(start + "---->" + end); + this.start = start; + this.end = end; + this.is = is; + this.raf = raf; + } + + @Override + public void run() { + try { + is.skip(start); + raf.seek(start); + // 定义读取输入流内容的的缓存数组(竹筒) + byte[] buff = new byte[BUFF_LEN]; + // 本线程负责下载资源的大小 + long contentLen = end - start; + // 定义最多需要读取几次就可以完成本线程的下载 + long times = contentLen / BUFF_LEN + 4; + // 实际读取的字节数 + int hasRead = 0; + for (int i = 0; i < times; i++) { + hasRead = is.read(buff); + // 如果读取的字节数小于0,则退出循环! + if (hasRead < 0) { + break; + } + raf.write(buff, 0, hasRead); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + // 使用finally块来关闭当前线程的输入流、输出流 + finally { + try { + if (is != null) { + is.close(); + } + if (raf != null) { + raf.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/group27/513274874/data-structure/src/com/coderising/download/demo/MutilDown.java b/group27/513274874/data-structure/src/com/coderising/download/demo/MutilDown.java new file mode 100644 index 0000000000..6358700d4f --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/demo/MutilDown.java @@ -0,0 +1,72 @@ +package com.coderising.download.demo; + +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.URL; +import java.net.URLConnection; + +public class MutilDown { + + public static void main(String[] args) { + //定义几个线程去下载 + final int DOWN_THREAD_NUM = 4; + final String OUT_FILE_NAME = "down.jpg"; + InputStream[] isArr = new InputStream[DOWN_THREAD_NUM]; + RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM]; + try { + // 创建一个URL对象 + URL url = new URL("http://hiphotos.baidu.com/240728057/pic/item/6a50e38242aad8f60cf4d2b3.jpg"); + // 以此URL对象打开第一个输入流 + isArr[0] = url.openStream(); + long fileLen = getFileLength(url); + System.out.println("网络资源的大小" + fileLen); + // 以输出文件名创建第一个RandomAccessFile输出流 + //创建从中读取和向其中写入(可选)的随机存取文件流,第一个参数:文件名,第二个参数是:参数指定用以打开文件的访问模式 + //"rw"可能是可读可写, + outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw"); + // 创建一个与下载资源相同大小的空文件 + for (int i = 0; i < fileLen; i++) { + outArr[0].write(0); + } + // 每线程应该下载的字节数 + long numPerThred = fileLen / DOWN_THREAD_NUM; + // 整个下载资源整除后剩下的余数取模 + long left = fileLen % DOWN_THREAD_NUM; + for (int i = 0; i < DOWN_THREAD_NUM; i++) { + // 为每个线程打开一个输入流、一个RandomAccessFile对象, + // 让每个线程分别负责下载资源的不同部分。 + //isArr[0]和outArr[0]已经使用,从不为0开始 + if (i != 0) { + // 以URL打开多个输入流 + isArr[i] = url.openStream(); + // 以指定输出文件创建多个RandomAccessFile对象 + outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw"); + } + // 分别启动多个线程来下载网络资源 + if (i == DOWN_THREAD_NUM - 1) { + // 最后一个线程下载指定numPerThred+left个字节 + new DownThread(i * numPerThred, (i + 1) * numPerThred + + left, isArr[i], outArr[i]).start(); + } else { + // 每个线程负责下载一定的numPerThred个字节 + new DownThread(i * numPerThred, (i + 1) * numPerThred, + isArr[i], outArr[i]).start(); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + // 定义获取指定网络资源的长度的方法 + public static long getFileLength(URL url) throws Exception { + long length = 0; + // 打开该URL对应的URLConnection + URLConnection con = url.openConnection(); + // 获取连接URL资源的长度 + long size = con.getContentLength(); + length = size; + return length; + } + +} \ No newline at end of file diff --git a/group27/513274874/data-structure/src/com/coderising/download/impl/ConnectionImpl.java b/group27/513274874/data-structure/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..01ef73bfa5 --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,100 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; + +import java.io.*; +import java.net.URL; +import java.net.URLConnection; + +public class ConnectionImpl implements Connection { + private URL url; + + // 定义字节数组(取水的竹筒)的长度 + private final int BUFF_LEN = 32; + + // 下载资源对应的输入流 + private InputStream is; + + + ByteArrayOutputStream bos; + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + this.is = url.openStream(); + + is.skip(startPos); + // 定义读取输入流内容的的缓存数组(竹筒) + byte[] buff = new byte[BUFF_LEN]; + // 本线程负责下载资源的大小 + long contentLen = endPos - startPos; + bos = new ByteArrayOutputStream((int) contentLen); + BufferedInputStream in = new BufferedInputStream(is); + int len = 0; + while (-1 != (len = in.read(buff, 0, BUFF_LEN))) { + bos.write(buff, 0, len); + } + return bos.toByteArray(); + } +// @Override +// public byte[] read(int startPos, int endPos) throws IOException { +// raf = new RandomAccessFile("newfile.jpg", "rw"); +// this.is = url.openStream(); +// +// is.skip(startPos); +// raf.seek(startPos); +// // 定义读取输入流内容的的缓存数组(竹筒) +// byte[] buff = new byte[BUFF_LEN]; +// // 本线程负责下载资源的大小 +// long contentLen = endPos - startPos; +// ByteArrayOutputStream bos = new ByteArrayOutputStream((int) contentLen); +// // 定义最多需要读取几次就可以完成本线程的下载 +// long times = contentLen / BUFF_LEN + 4; +// // 实际读取的字节数 +// int hasRead = 0; +// for (int i = 0; i < times; i++) { +// hasRead = is.read(buff); +// // 如果读取的字节数小于0,则退出循环! +// if (hasRead < 0) { +// break; +// } +// raf.write(buff, 0, hasRead); +// } +// +// return null; +// } + + @Override + public int getContentLength() { + int length = 0; + // 打开该URL对应的URLConnection + URLConnection con = null; + try { + con = url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + // 获取连接URL资源的长度 + length = con.getContentLength(); + return length; + } + + @Override + public void close() { + try { + if (is != null) { + is.close(); + } + if (bos != null) { + bos.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public ConnectionImpl(URL url) { + this.url = url; + } +} diff --git a/group27/513274874/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/513274874/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..f9bbeb5a4f --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,29 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; + +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + Connection connection = null; + try { + if(url == null || "".equals(url.trim())) return null; + + URL urlO = new URL(url); + connection = new ConnectionImpl(urlO); + + + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return connection; + } + +} diff --git a/group27/513274874/data-structure/src/com/coderising/litestruts/LoginAction.java b/group27/513274874/data-structure/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group27/513274874/data-structure/src/com/coderising/litestruts/Struts.java b/group27/513274874/data-structure/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..02a8146b0c --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,254 @@ +package com.coderising.litestruts; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.File; +import java.io.FileNotFoundException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + + +public class Struts { + + private static Struts instance = null; + private static Map strutsXml; + + private Struts() { + } + + /** + * 单例模式初始化struts.xml,而不是每次跑runAction的时候都要初始化一次 + * + * @return + */ + public static Struts init() throws FileNotFoundException { + + if (instance == null) { + /** + * 0. 读取配置文件struts.xml + */ + //创建SAXReader对象 + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + Document document = null; + try { + document = reader.read(new File("src/com/coding/coderising/litestruts/struts.xml")); + } catch (DocumentException e) { + e.printStackTrace(); + } + //获取根节点元素对象 + Element root = document.getRootElement(); + if ("struts".equals(root.getName())) { + strutsXml = new HashMap(); + + Iterator actions = root.elementIterator(); + while (actions.hasNext()) { + Element action = actions.next(); + List attrList = action.attributes(); + + String actionName = null; + StrutsXml xml = null; + if (!"action".equals(action.getName())) { + continue; + } + //遍历属性节点 + for (Attribute attribute : attrList) { + xml = new StrutsXml(); + if ("name".equals(attribute.getName())) { + actionName = attribute.getValue(); + } + if ("class".equals(attribute.getName())) { + xml.setClazz(attribute.getValue()); + //获取result信息 + Iterator results = action.elementIterator(); + while (results.hasNext()) { + Element result = results.next(); + List resultList = result.attributes(); + for (Attribute resultAttr : resultList) { + //System.out.println(resultAttr.getValue() + " ,"+result.getText()); + xml.getResult().put(resultAttr.getValue(), result.getText()); + } + } + + } + //System.out.println("属性"+attribute.getName() +":" + attribute.getValue()); + } + + strutsXml.put(actionName, xml); + } + } else { + throw new FileNotFoundException("not a struts XML file !"); + } + + + instance = new Struts(); + } + return instance; + } + + public static View runAction(String actionName, Map parameters) { + + if (instance == null) return null; + if (actionName == null || "".equals(actionName.trim())) return null; + View view = new View(); + StrutsXml struts = strutsXml.get(actionName); + + Class clazz = null; + /** + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + * ("name"="test" , "password"="1234") ,那就应该调用 setName和setPassword方法 + */ + //获取相应处理的action + if (struts != null) { + String className = struts.getClazz(); + try { + clazz = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } else { + throw new NullPointerException("action not found in struts file !"); + } + + if (clazz != null) { + Object action = null; + try { + action = clazz.newInstance(); + + //反射调用设置参数 + for (Map.Entry entry : parameters.entrySet()) { + String para = entry.getKey(); + if (!checkField(clazz, para)) continue; + //根据习惯,类的属性首字母在调用时大写,例如属性名是 age,则类方法为getAge + PropertyDescriptor pd = new PropertyDescriptor(para, clazz); + + Method setMethod = pd.getWriteMethod();//获得set方法 + + setMethod.invoke(action, entry.getValue()); + + } + /** + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + */ + //执行execute() + Method excuteMethod = clazz.getDeclaredMethod("execute"); + String result = (String) excuteMethod.invoke(action); + //通过xml文件获取返回值 + String jsp = struts.getResult().get(result); + + if (jsp == null || jsp.trim().equals("")) { + throw new NullPointerException("the requested file is not found !"); + } + /** + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + * 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + * 放到View对象的parameters + */ + //执行get方法 + Map viewMap = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields();//获得属性 + + for (Field field : fields) { + String getMethodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1); + Method getMethod = clazz.getDeclaredMethod(getMethodName); + String returnVal = (String) getMethod.invoke(action); + viewMap.put(field.getName(), returnVal); + } + /** + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + view.setJsp(jsp); + view.setParameters(viewMap); + + } catch (IntrospectionException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + } + + return view; + + + } + + private static boolean checkField(Class clazz, String fieldName) { + if (fieldName == null || fieldName.trim().equals("")) return false; + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + if (fieldName.equals(field.getName())) return true; + } + return false; + } + + + public static void main(String args[]) { + try { + Struts.init(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + Map paras = new HashMap<>(); + paras.put("name", "test"); + paras.put("password", "1234"); + View view = Struts.runAction("login", paras); + } +} + +class StrutsXml { + private String actionName; + private String clazz; + private Map result = new HashMap<>(); + + public StrutsXml(String actionName, String clazz, Map result) { + this.actionName = actionName; + this.clazz = clazz; + this.result = result; + } + + public StrutsXml() { + } + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public Map getResult() { + return result; + } + + public void setResult(Map result) { + this.result = result; + } +} \ No newline at end of file diff --git a/group27/513274874/data-structure/src/com/coderising/litestruts/StrutsTest.java b/group27/513274874/data-structure/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..c6341d662d --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.litestruts; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + @Before + public void before() throws Exception { + Struts.init(); + } + + @After + public void after() throws Exception { + } + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group27/513274874/data-structure/src/com/coderising/litestruts/View.java b/group27/513274874/data-structure/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group27/513274874/data-structure/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group27/513274874/data-structure/src/com/coding/basic/ArrayList.java b/group27/513274874/data-structure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9e55e92529 --- /dev/null +++ b/group27/513274874/data-structure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,124 @@ + +package com.coding.basic; + +import java.util.Arrays; + +/** + * @autor zhougd 20170306 + * 数组实现ArrayList + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + //扩容默认值 + private static final int INCREAMENT_CAP = 10; + + //含参数的构造函数 + public ArrayList(int size, Object[] elementData) { + this.size = size; + this.elementData = elementData; + } + + //默认100容量的构造函数 + public ArrayList() { + this.size = 0; + this.elementData = new Object[100]; + } + + @Override + public void add(Object o) { + //判断超过容量自动扩容 + if (this.size + 1 > this.elementData.length) { + increase(); + } + this.elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index out of bound!"); + } + //判断超过容量自动扩容 + if (this.size + 1 > this.elementData.length) { + increase(); + } + this.size++; + //index后面数组后移一位 + for (int cur = this.size; cur > index; cur--) { + this.elementData[cur] = this.elementData[cur - 1]; + } + + this.elementData[index] = o; + + } + + public Object get(int index) { + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index out of bound!"); + } + return this.elementData[index]; + } + + public Object remove(int index) { + Object o = this.get(index); + + //index后面的数向前移动一位 + for (int cur = index + 1; cur < this.size; cur++) { + this.elementData[cur] = this.elementData[cur + 1]; + } + //最后一个元素删除 + this.elementData[this.size-1] = null; + + this.size--; + return o; + } + + public int size() { + return this.size + 1; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + @Override + public String toString() { + String arrayStr = "ArrayList{ size = " + this.size() + " , "; + + arrayStr += "elementData=["; + for(int i = 0 ;i 0 && newArray[in - 1] >= temp) { + //右移 + newArray[in] = newArray[in - 1]; + --in; + } + newArray[in] = temp; + } + return newArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int oldSize = oldArray.length; + if (oldSize == 0) return new int[size]; + + if (size <= 0) return oldArray; + + int[] newArray = new int[oldSize + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldSize); + + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + //先确定数组长度 + if (max == 1) return new int[]{}; + //这里的cur指的是数组的下标,从0开始,而不是数学函数1开始 + int cur = 2; + int val_1 = 1; + int val_2 = 1; + while (val_1 + val_2 <= max) { + int temp = val_1; + val_1 = val_2; + val_2 += temp; + ++cur; + } + + int[] newArray = new int[cur]; + for (int i = 0; i < cur; i++) { + if (i == 0 || i == 1) { + newArray[i] = 1; + continue; + } + newArray[i] = newArray[i - 1] + newArray[i - 2]; + + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + //先确定数组长度 + //判断质数循环 + int count = 0; + for (int i = 1; i < max; i++) { + //去掉偶数 + if (i == 1 || (i % 2 == 0 && i != 2)) continue; + boolean flag = true; + for (int j = 3; j <= Math.sqrt(i); j += 2) { + if (i % j == 0) { + flag = false; + break; + } + } + if (flag) count++; + } + int[] newArray = new int[count]; + int cur = 0; + for (int i = 1; i < max; i++) { + //去掉偶数 + if (i == 1 || (i % 2 == 0 && i != 2)) continue; + //判断到开根号即可 + boolean flag = true; + for (int j = 3; j <= Math.sqrt(i); j += 2) { + if (i % j == 0) { + flag = false; + + } + } + if (flag) { + newArray[cur] = i; + ++cur; + } + + } + + + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + //求数组长度 + int count = 0; + for (int a = 1; a <= max; a++) { + int sum = 0; + for (int i = 1; i <= a / 2; i++) + if (a % i == 0) + sum += i; + if (a == sum) + ++count; + } + + int[] newArray = new int[count]; + int cur = 0; + for (int a = 1; a <= max; a++) { + int sum = 0; + for (int i = 1; i <= a / 2; i++) + if (a % i == 0) + sum += i; + if (a == sum) { + newArray[cur] = a; + ++cur; + } + } + + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + int size = array.length; + if (size == 0) return ""; + StringBuffer sb = new StringBuffer(""); + for (int i = 0; i < size - 1; i++) { + sb.append(array[i]).append(seperator); + } + sb.append(array[size - 1]); + return sb.toString(); + } + + + /** + * 类私有函数,复制返回一个新的数组 + */ + private static int[] copyOf(int[] source) { + int size = source.length; + if (size <= 0) return null; + + int[] newArray = new int[size]; + //int[] ints = Arrays.copyOf(origin, size); + System.arraycopy(source, 0, newArray, 0, size); + return newArray; + } + + +} diff --git a/group27/513274874/data-structure/src/com/coding/basic/array/ArrayUtilTest.java b/group27/513274874/data-structure/src/com/coding/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..7918ae41fe --- /dev/null +++ b/group27/513274874/data-structure/src/com/coding/basic/array/ArrayUtilTest.java @@ -0,0 +1,110 @@ +package com.coding.basic.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * ArrayUtil Tester. + * + * @author + * @version 1.0 + * @since
三月 14, 2017
+ */ +public class ArrayUtilTest { + + int[] testArray ; + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + testArray = new int[]{}; + } + + /** + * Method: reverseArray(final int[] origin) + */ + @Test + public void testReverseArray() throws Exception { + testArray = new int[]{1,3,5,7,9,4,6}; + ArrayUtil.reverseArray(testArray); + Assert.assertArrayEquals(new int[]{6,4,9,7,5,3,1},testArray); + } + + + /** + * Method: removeZero(int[] oldArray) + */ + @Test + public void testRemoveZero() throws Exception { + testArray = new int[]{1,3,0,7,0,4,6}; + int[] newArray = ArrayUtil.removeZero(testArray); + Assert.assertArrayEquals(new int[]{1,3,7,4,6}, newArray); + } + + /** + * Method: merge(int[] array1, int[] array2) + */ + @Test + public void testMerge() throws Exception { + int[] testArray1 = new int[]{1,3,6,8,9}; + int[] testArray2 = new int[]{2,3,3,10,12}; + + int[] mergedArray = ArrayUtil.merge(testArray1,testArray2); + Assert.assertArrayEquals(new int[]{1,2,3,3,3,6,8,9,10,12},mergedArray); + } + + /** + * Method: grow(int[] oldArray, int size) + */ + @Test + public void testGrow() throws Exception { + testArray = new int[]{1,2,3,4,5,6}; + int[] grewArray = ArrayUtil.grow(testArray,4); + Assert.assertArrayEquals(new int[]{1,2,3,4,5,6,0,0,0,0},grewArray); + + } + + /** + * Method: fibonacci(int max) + */ + @Test + public void testFibonacci() throws Exception { + int[] fibArray = ArrayUtil.fibonacci(20); + Assert.assertArrayEquals(new int[]{1,1,2,3,5,8,13},fibArray); + } + + /** + * Method: getPrimes(int max) + */ + @Test + public void testGetPrimes() throws Exception { + testArray = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(new int[]{2,3,5,7,11,13,17,19},testArray); + } + + /** + * Method: getPerfectNumbers(int max) + */ + @Test + public void testGetPerfectNumbers() throws Exception { + testArray = ArrayUtil.getPerfectNumbers(1000); + Assert.assertArrayEquals(new int[]{6,28,496},testArray); + } + + /** + * Method: join(int[] array, String seperator) + */ + @Test + public void testJoin() throws Exception { + testArray = new int[]{1,2,3,5,7,9,12}; + String seperated = ArrayUtil.join(testArray,"-"); + Assert.assertEquals("1-2-3-5-7-9-12",seperated); + } + + +} diff --git a/group27/513274874/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java b/group27/513274874/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..4414a8eb6f --- /dev/null +++ b/group27/513274874/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,158 @@ +package com.coding.basic.linklist; + +import java.util.Objects; + +/** + * 用双向链表实现LRU算法 + * + * @author liuxin + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + + public boolean hasNext() { + return next != null; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Node)) return false; + Node node = (Node) o; + return Objects.equals(pageNum, node.pageNum); + } + + @Override + public int hashCode() { + return Objects.hash(pageNum); + } + } + + private int capacity;//最大存储个数 + private int cur;//当前存储个数 + + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + } + + /** + * 获取缓存中对象 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if(this.cur == 0 ) { + Node node = new Node(); + node.pageNum = pageNum; + addFirst(node); + return; + } + + if (get(pageNum) != null) { + pop(pageNum); + } else { + Node node = new Node(); + node.pageNum = pageNum; + add(node); + } + } + + private void add(Node node) { + addFirst(node); + } + + private Node get(int pageNum) { + Node val = this.first; + while (val.hasNext()) { + if (val.pageNum == pageNum) { + return val; + } + val = val.next; + } + return null; + } + + private void addFirst(Node node) { + if(cur == 0){ + this.first = node; + this.last = node; + }else { + Node oldFirst = this.first; + this.first = node; + node.prev = null; + node.next = oldFirst; + + oldFirst.prev = node; + } + this.cur++; + + if (cur > capacity) { + removeLast(); + } + } + private void removeLast(){ + Node oldLast = this.last; + this.last = oldLast.prev; + oldLast.prev.next = null; + + oldLast = null; + } + + /** + * 将节点变成first + * + * @param pageNum + */ + private void pop(int pageNum) { + Node node = this.get(pageNum); + + //根据node的位置确定如何位移 + if (node.equals(this.first)) { + return; + } else { + Node oldPre = node.prev; + Node oldNext = node.next; + Node oldFirst = this.first; + + this.first = node; + node.prev = null; + node.next = oldFirst; + + oldPre.next = oldNext; + oldNext.prev = oldPre; + + + } + + } + + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group27/513274874/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group27/513274874/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..374cfafecf --- /dev/null +++ b/group27/513274874/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess3() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + + @Test + public void testAccess5() { + LRUPageFrame frame = new LRUPageFrame(5); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0,7", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1,7", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1,7", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2,1,7", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2,1,7", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3,2,1", frame.toString()); + } + +} diff --git a/group27/513274874/data-structure/src/com/coding/basic/linklist/LinkedList.java b/group27/513274874/data-structure/src/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..e6d400f483 --- /dev/null +++ b/group27/513274874/data-structure/src/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,268 @@ + +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +import java.util.NoSuchElementException; + +/** + * @author zhougd 20170306 + * 单向链表实现LinkedList + */ + +public class LinkedList implements List { + java.util.LinkedList a; + /** + * 第一个元素 + */ + private Node head; + + /** + * 最后一个元素 + */ + private Node tail; + + /** + * 元素容量 + */ + private int size = 0; + + public void add(Object o){ + Node node = new Node(o); + //判断是否链表为空 + if(this.size() == 0){ + this.addFirst(node); + }else{ + this.addLast(node); + } + + } + public void add(int index , Object o){ + checkIndex(index); + + Node oldNode= this.getNode(index); + Object oldObject = oldNode.getData(); + Node next = oldNode.getNext(); + + //将原位置修改为新元素 + oldNode.setData(o); + //设置下一个元素 + oldNode.setNext(new Node(oldObject)); + //设置下一个元素的下一个元素 + oldNode.getNext().setNext(next); + + size ++; + } + + public Object get(int index){ + checkIndex(index); + return this.getNode(index).getData(); + } + + public Object remove(int index){ + checkIndex(index); + //获取到当前元素和下一个元素 + //把当前元素的值设置成下一个元素的值,删除掉下一个元素,这样的话,不必管上一个元素是什么,是不是第一个元素 + Node node = this.getNode(index); + Object data = node.getData(); + Node nextNode = this.getNode(index + 1); + node.setData(nextNode.getData()); + node.setNext(nextNode.getNext()); + + return data; + } + + public int size(){ + return this.size(); + } + + public void addFirst(Object o){ + Node node = new Node(o); + //原头变为第二 + Node temp = this.head; + this.head = node; + node.next = temp; + size++; + } + public void addLast(Object o){ + Node node = new Node(o); + Node t = this.tail; + if(t == null){ + this.head = node; + }else{ + this.tail.next = node; + this.tail = node; + } + size++; + } + public Object removeFirst(){ + Node head = this.head; + if(head == null){ + throw new NoSuchElementException("No such element !"); + } + this.head = this.head.getNext(); + size--; + return head ; + } + + public Object removeLast(){ + Node node ; + if(this.tail == null){ + throw new NoSuchElementException("No such element !"); + } + node = this.tail; + if(this.head ==this.tail){ + node = this.head; + this.head = null; + this.size = 0; + }else{ + //获取尾元素的上一个元素 + this.tail = this.getNode(this.size-2); + this.tail.setNext(null); + this.size--; + } + + return node; + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private void checkIndex(int index){ + if(index < 0 || index >size()){ + throw new IndexOutOfBoundsException("Index out of bound !"); + } + } + + private Node getNode(int index ){ + + Node node = this.head; + for(int i = 0 ;i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + +} \ No newline at end of file diff --git a/group27/513274874/data-structure/test/com/coding/basic/ArrayListTest.java b/group27/513274874/data-structure/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..251f5a8d92 --- /dev/null +++ b/group27/513274874/data-structure/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,151 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Random; + +/** +* ArrayList Tester. +* +* @author +* @since
三月 6, 2017
+* @version 1.0 +*/ +public class ArrayListTest { + +@Before +public void before() throws Exception { + +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: add(Object o) +* +*/ +@Test +public void testAddO() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(100); + arrayList.add(20); + + for(int i = 1 ;i <= 200 ;i++){ + arrayList.add(new Random().nextInt(100)); + } + + System.out.println(arrayList); + + assert(arrayList.size() == 202); +} + +/** +* +* Method: add(int index, Object o) +* +*/ +@Test +public void testAddForIndexO() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + + arrayList.add(3,"添加"); + //arrayList.add(100,3); + assert(arrayList.size() == 6); + System.out.println(arrayList); +} + +/** +* +* Method: get(int index) +* +*/ +@Test +public void testGet() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + + assert(((Integer)arrayList.get(3)).intValue() == 4); +} + +/** +* +* Method: remove(int index) +* +*/ +@Test +public void testRemove() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + + arrayList.remove(3); + //arrayList.add(100,3); + assert(arrayList.size() == 4); + System.out.println(arrayList); +} + +/** +* +* Method: size() +* +*/ +@Test +public void testSize() throws Exception { +//TODO: Test goes here... + + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(100); + arrayList.add(20); + + for(int i = 1 ;i <= 200 ;i++){ + arrayList.add(new Random().nextInt(100)); + } + + System.out.println(arrayList); + + assert(arrayList.size() == 202); +} + +/** +* +* Method: iterator() +* +*/ +@Test +public void testIterator() throws Exception { +//TODO: Test goes here... + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(100); + arrayList.add(20); + + for(int i = 1 ;i <= 200 ;i++){ + arrayList.add(new Random().nextInt(100)); + } + System.out.println(arrayList); + + Iterator iterator = arrayList.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + ","); + } + + assert(arrayList.size() == 202); +} + +} diff --git a/group27/513274874/homework/.project b/group27/513274874/homework/.project index b6d8ce6204..8a1c96cafa 100644 --- a/group27/513274874/homework/.project +++ b/group27/513274874/homework/.project @@ -1,6 +1,6 @@ - coding2017 + ThirdHomework diff --git a/group27/513274874/homework/src/com/coding/basic/LinkedList.java b/group27/513274874/homework/src/com/coding/basic/LinkedList.java index d66be49758..1d574e8aa3 100644 --- a/group27/513274874/homework/src/com/coding/basic/LinkedList.java +++ b/group27/513274874/homework/src/com/coding/basic/LinkedList.java @@ -185,4 +185,81 @@ public void setNext(Node next) { } + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + } \ No newline at end of file diff --git a/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java b/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java index 1456314140..04cf3e4fe7 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java @@ -1,20 +1,38 @@ -package com.coderising.download; +package com.coding.coderising.download; + + +import com.coding.coderising.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; -import com.coderising.download.api.Connection; public class DownloadThread extends Thread{ + + Connection conn; int startPos; int endPos; + // 将下载到的字节输出到raf中 + private RandomAccessFile raf; + public DownloadThread( Connection conn, int startPos, int endPos){ this.conn = conn; + this.startPos = startPos; this.endPos = endPos; + this.filePath = filePath; } - public void run(){ - + + public void run(){ + try { + conn.read(startPos,endPos); + } catch (IOException e) { + e.printStackTrace(); + + } } } diff --git a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java index f5d7999eb4..7a010983f8 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java @@ -1,9 +1,11 @@ -package com.coderising.download; +package com.coding.coderising.download; + + +import com.coding.coderising.download.api.Connection; +import com.coding.coderising.download.api.ConnectionException; +import com.coding.coderising.download.api.ConnectionManager; +import com.coding.coderising.download.api.DownloadListener; -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; public class FileDownloader { @@ -14,7 +16,10 @@ public class FileDownloader { ConnectionManager cm; - + private final static int thread_count = 3; + + private final static String BASE_PATH = "E:\\"; + public FileDownloader(String _url) { this.url = _url; @@ -23,13 +28,18 @@ public FileDownloader(String _url) { public void execute(){ // 在这里实现你的代码, 注意: 需要用多线程实现下载 // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // (1) ConnectionManager , 可以打开一个连接, + //通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, + //调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, + //这样客户端就能收到通知。 // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 1. 需要调用ConnectionManager的open方法打开连接, + //然后通过Connection.getContentLength方法获得文件的长度 // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, + //返回值是byte[]数组 // 3. 把byte数组写入到文件中 // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 @@ -41,10 +51,54 @@ public void execute(){ int length = conn.getContentLength(); - new DownloadThread(conn,0,length-1).start(); + String targetPath = BASE_PATH + "targetfile." + url.substring(url.lastIndexOf(".") + 1); + + int startPos = 0; + + int endPos = 0; + + List list = new ArrayList(); + + for(int i = 0; i < thread_count; i++){ + + conn = cm.open(url); + + startPos = i * (length / thread_count); + + endPos = (i == thread_count - 1) ? length - 1 : (i + 1) * (length / thread_count) - 1; + + DownloadThread thread = new DownloadThread(conn, startPos, endPos, targetPath); + + list.add(thread); + + thread.start(); + } + + // 调用线程的join方法,保证所有的线程都结束后再发出结束通知 + + for (int i = 0; i < list.size(); i++) { + + try { + + list.get(i).join(); + + } catch (InterruptedException e) { + + // TODO Auto-generated catch block + + e.printStackTrace(); + + } + + } + + + + listener.notifyFinished(); } catch (ConnectionException e) { e.printStackTrace(); + }finally{ if(conn != null){ conn.close(); diff --git a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java index 8171ee5763..60aa517b5b 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java @@ -1,12 +1,12 @@ -package com.coderising.download; +package com.coding.coderising.download; import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; +import com.coding.coderising.download.api.ConnectionManager; +import com.coding.coderising.download.api.DownloadListener; +import com.coding.coderising.download.impl.ConnectionManagerImpl; public class FileDownloaderTest { boolean downloadFinished = false; @@ -21,7 +21,7 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "http://localhost:8080/test.jpg"; + String url = "C:/Users/苏磊/Desktop/R%T[DQRU~@$6TKZ7)TD81JW.png"; FileDownloader downloader = new FileDownloader(url); diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java b/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java index 9710e270e1..1b00c983d6 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; import java.io.IOException; diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java index 8dbfe95dda..e08c73bd98 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; public class ConnectionException extends Exception { diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java index fb44ede457..f95561e023 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; public interface ConnectionManager { /** diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java b/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java index 4cd0b3eab1..32c399a204 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; public interface DownloadListener { public void notifyFinished(); diff --git a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java index 32f03efdc7..a56ee74918 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java @@ -1,27 +1,104 @@ -package com.coderising.download.impl; +package com.coding.coderising.download.impl; -import java.io.IOException; -import com.coderising.download.api.Connection; +import com.coding.coderising.download.api.Connection; -public class ConnectionImpl implements Connection{ - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } +import java.io.*; +import java.net.URL; +import java.net.URLConnection; - @Override - public int getContentLength() { - - return 0; - } +public class ConnectionImpl implements Connection { + private URL url; - @Override - public void close() { - - - } + // 定义字节数组(取水的竹筒)的长度 + private final int BUFF_LEN = 32; + + // 下载资源对应的输入流 + private InputStream is; + + + + ByteArrayOutputStream bos; + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + this.is = url.openStream(); + + is.skip(startPos); + // 定义读取输入流内容的的缓存数组(竹筒) + byte[] buff = new byte[BUFF_LEN]; + // 本线程负责下载资源的大小 + long contentLen = endPos - startPos; + bos = new ByteArrayOutputStream((int) contentLen); + BufferedInputStream in = new BufferedInputStream(is); + int len = 0; + while (-1 != (len = in.read(buff, 0, BUFF_LEN))) { + bos.write(buff, 0, len); + } + return bos.toByteArray(); + } +// @Override +// public byte[] read(int startPos, int endPos) throws IOException { +// raf = new RandomAccessFile("newfile.jpg", "rw"); +// this.is = url.openStream(); +// +// is.skip(startPos); +// raf.seek(startPos); +// // 定义读取输入流内容的的缓存数组(竹筒) +// byte[] buff = new byte[BUFF_LEN]; +// // 本线程负责下载资源的大小 +// long contentLen = endPos - startPos; +// ByteArrayOutputStream bos = new ByteArrayOutputStream((int) contentLen); +// // 定义最多需要读取几次就可以完成本线程的下载 +// long times = contentLen / BUFF_LEN + 4; +// // 实际读取的字节数 +// int hasRead = 0; +// for (int i = 0; i < times; i++) { +// hasRead = is.read(buff); +// // 如果读取的字节数小于0,则退出循环! +// if (hasRead < 0) { +// break; +// } +// raf.write(buff, 0, hasRead); +// } +// +// return null; +// } + + @Override + public int getContentLength() { + int length = 0; + // 打开该URL对应的URLConnection + URLConnection con = null; + try { + con = url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + // 获取连接URL资源的长度 + length = con.getContentLength(); + return length; + } + + @Override + public void close() { + try { + if (is != null) { + is.close(); + } + if (bos != null) { + bos.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public ConnectionImpl(URL url) { + this.url = url; + } } diff --git a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java index 046f7c49a4..5250910126 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java @@ -1,15 +1,33 @@ -package com.coderising.download.impl; +package com.coding.coderising.download.impl; + + +import com.coding.coderising.download.api.Connection; +import com.coding.coderising.download.api.ConnectionException; +import com.coding.coderising.download.api.ConnectionManager; + +import java.net.MalformedURLException; +import java.net.URL; -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - - return null; + + + Connection connection = null; + try { + if(url == null || "".equals(url.trim())) return null; + + URL urlO = new URL(url); + connection = new ConnectionImpl(urlO); + + + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return connection; + } } diff --git a/group27/513274874/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/group27/513274874/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..15986ec3d2 --- /dev/null +++ b/group27/513274874/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,57 @@ +package com.coderising.jvm.loader; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + private static final String CLASS_SUFFIX = ".class"; + private static final String PATH_SEPARATOR = System.getProperty("file.separator"); + + public byte[] readBinaryCode(String className) { + + if (className == null || className.trim().equals("")) { + throw new IllegalArgumentException("package and file name can't be blank!"); + } + //扫描classpath,找到文件即停止扫描,找不到就报错 + String packageName = className.replace(".", PATH_SEPARATOR); + String clazzURL = packageName + CLASS_SUFFIX; + File file = null; + for (String path : clzPaths) { + file = new File(path + clazzURL); + if (file.isDirectory() && file.length() > 0) break; + } + byte[] clazzByte = new byte[0]; + try { + FileInputStream fis = new FileInputStream(file); + DataInputStream data_in = new DataInputStream(fis); + clazzByte = new byte[(int) file.length()]; + data_in.read(clazzByte, 0, (int) file.length()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return clazzByte; + } + + + public void addClassPath(String path) { + if (null == path || path.trim().equals("")) return; + clzPaths.add(path); + } + + + public String getClassPath() { + String clazzPaths = ""; + for (String clazzPath : clzPaths) { + clazzPaths += clazzPath + ";"; + } + return clazzPaths; + } + + } diff --git a/group27/513274874/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group27/513274874/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..0737dd702e --- /dev/null +++ b/group27/513274874/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,91 @@ +package com.coderising.jvm.test; + +import com.coderising.jvm.loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + + + +public class ClassFileloaderTest { + + + static String path1 = "/Users/guodongchow/Desktop/coding2017/projects/mini-jvm/bin/"; + static String path2 = "/Users/guodongchow/bin"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2+";",clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i 0){ + randomAccessFile.write(buffer, 0, length); + total += length; + /* + * 将当前现在到的位置保存到文件中 + */ + downThreadStream.seek(0); + downThreadStream.write((startIndex + total + "").getBytes("UTF-8")); + } + + downThreadStream.close(); + inputStream.close(); + randomAccessFile.close(); + cleanTemp(downThreadFile);//删除临时文件 + System.out.println("线程"+ threadId + "下载完毕"); + }else{ + System.out.println("响应码是" +connection.getResponseCode() + ". 服务器不支持多线程下载"); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + //删除线程产生的临时文件 + private synchronized void cleanTemp(File file){ + file.delete(); + } + + //获取下载文件的名称 + private String getFileName(URL url){ + String filename = url.getFile(); + return filename.substring(filename.lastIndexOf("/")+1); + } + + public static void main(String[] args) { + try { + new Test().download(); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java b/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..7a8c9e7600 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java @@ -0,0 +1,25 @@ +package com.coderising.download.api; + +import java.io.IOException; +import java.net.HttpURLConnection; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java b/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2d1053ebae --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,44 @@ +package com.coderising.download.impl; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private HttpURLConnection conn = null; + + public ConnectionImpl(HttpURLConnection conn) { + super(); + this.conn = conn; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + conn.setRequestProperty("Range", "bytes="+ startPos + "-" + endPos); + InputStream is = conn.getInputStream(); + byte[] buffer = new byte[endPos - startPos + 1]; + is.read(buffer, 0, endPos - startPos + 1); + + return buffer; + + } + + @Override + public int getContentLength(){ + return conn.getContentLength(); + } + + @Override + public void close() { + + if(conn != null){ + conn.disconnect(); + } + } + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..69817bd783 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,47 @@ +package com.coderising.download.impl; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + try { + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection)urlObj.openConnection(); + //超时 + conn.setConnectTimeout(3*1000); + //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + conn.setRequestMethod("GET"); + return new ConnectionImpl(conn); + } catch (Exception e) { + throw new ConnectionException(); + } + + } + + public static void main(String[] args) throws ConnectionException, IOException { + ConnectionManager cm = new ConnectionManagerImpl(); + Connection conn = cm.open("http://localhost:8080/ForDownload/test.jpg"); + + System.out.println(conn.getContentLength()); + + byte[] content = conn.read(0, conn.getContentLength()-1); + OutputStream os = new FileOutputStream("d:test.jpg"); + os.write(content); + os.flush(); + + conn.close(); + os.close(); + } + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java index 6f2fbf1f5b..abb912f644 100644 --- a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java +++ b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -3,7 +3,6 @@ public class LinkedList implements List { private Node head; - private Node tail; private int size; @@ -11,100 +10,90 @@ public void add(Object o){ this.addLast(o); } - public void add(int index , Object o) throws Exception{ + public void add(int index , Object o){ + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } if(index==0){ this.addFirst(o); + size++; return; - }else if(index==size-1){ + }else if(index==size){ this.addLast(o); + size++; return; - }else{ - - - Node curNode = this.getNode(index); - Node pre = curNode.previous; -// Node next = curNode.next; - // - Node newNode = new Node(o, pre, curNode); - curNode.previous = newNode; - pre.next = newNode; } + Node preNode = this.getNode(index-1); + Node curNode = this.getNode(index); + Node newNode = new Node(o, curNode); + preNode.next = newNode; + + size++; } - private Node getNode(int index) throws Exception{ - if(index>=size){ - throw new Exception("±곬"); + + private Node getNode(int index){ + if(index <0 || index>=size){ + throw new IndexOutOfBoundsException(); } if(index ==0){ return head; - }else if(index==size-1){ - return tail; - - }else{ - Node temp = head; - for(int i =1;i<=index;i++){ - temp = temp.next; - } - return temp; } + Node curNode = head; + for(int i=1;i<=index;i++){ + curNode = curNode.next; + } + return curNode; } - public Object get(int index) throws Exception{ - if(index>=size){ - throw new Exception("±곬"); + + public Object get(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); } - if(index ==0){ - return head.data; - }else if(index==size-1){ - return tail.data; - - }else{ - Node temp = head; - for(int i =1;i<=index;i++){ - temp = temp.next; - } - return temp.data; + + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; } + return temp.data; } - public Object remove(int index) throws Exception{ - if(index>=size){ - throw new Exception("±곬"); + public Object remove(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); } Object o = null; if(size == 1){ o = head.data; + size--; + return o; + } + if(index==0){ + o = head.data; + Node afterHead = head.next; + head = afterHead; + + }else if(index==size-1){ + Node preTail = getNode(index-1); + Node tail = preTail.next; + o = tail.data; + preTail.next = null; }else{ - if(index==0){ - - Node afterHead = head.next; - afterHead.previous = null; - head = afterHead; - o = head.data; - - }else if(index == size-1){ - Node beforeTail = tail.previous; - beforeTail.next = null; - tail = beforeTail; - o = tail.data; - }else{ - Node curNode = this.getNode(index); - Node pre = curNode.previous; - Node next = curNode.next; - //мڶϿָ - Node temp = new Node(next.data, pre, next.next); - pre.next = temp; - next = temp; - o = curNode.data; - - } + Node preCur = getNode(index-1); + Node cur = preCur.next; + Node nextCur = cur.next; + o = cur.data; + preCur.next = nextCur; + + } + size--; + return o; - } - size--; - return o; + } @@ -114,39 +103,39 @@ public int size(){ } public void addFirst(Object o){ - Node node = new Node(o, null, head); + Node node = new Node(o,null); if(head == null){ head = node; - tail = node; - }else{ - head.previous = node; - head = node; + size++; + return; } + head = new Node(o, head); size++; } public void addLast(Object o){ - //½ڵpreviousָָtail - Node curNode = new Node(o, tail, null); - if(tail==null){ - //ǰΪʱýڵͷβΪýڵ - head = curNode; - tail = curNode; - }else{ - //Ϊʱһڵnextָָ¼Ľڵ㼴 - tail.next = curNode; - //¼ڵΪtail - tail = curNode; - + //½ڵnextָָtail + Node add = new Node(o, null); + if(head==null){ + head = add; + size++; + return; + } + Node curNode = head; + while(curNode.next != null){ + curNode = curNode.next; } - size++; + curNode.next = add; + size++; } - public Object removeFirst() throws Exception{ + + + public Object removeFirst(){ return this.remove(0); } - public Object removeLast() throws Exception{ + public Object removeLast(){ return this.remove(size-1); } @@ -186,7 +175,9 @@ public String toString() { } } - sb.deleteCharAt(sb.lastIndexOf(",")); + if(sb.indexOf(",") != -1){ + sb.deleteCharAt(sb.lastIndexOf(",")); + } sb.append("]"); return sb.toString(); @@ -199,12 +190,10 @@ public String toString() { private static class Node{ private Object data; - private Node previous; private Node next; - public Node(Object data, Node previous, Node next) { + public Node(Object data, Node next) { super(); this.data = data; - this.previous = previous; this.next = next; } @@ -228,9 +217,225 @@ public static void main(String[] args) throws Exception { ll.add(4); System.out.println(ll); - Iterator itr = ll.iterator(); + + ll.reverse(); + System.out.println(ll); + /*System.out.println(ll.get(0)); + System.out.println(ll.get(1)); + System.out.println(ll.get(2));*/ + + LinkedList ll2 = new LinkedList(); + ll2.add(1); +// ll2.add(1); + ll2.add(2); +// ll2.add(3); + ll2.add(3); +// ll2.add(4); + ll2.add(4); + ll2.add(5); +// ll2.removeFirstHalf(); +// ll2.remove(2,3); +// ll2.removeDuplicateValues(); + + System.out.println(ll2); + +// ll2.removeRange(2, 6); +// ll2.remove(3); + System.out.println(ll2); + + LinkedList ll3 = new LinkedList(); + ll3.add(2); + ll3.add(4); + ll2.subtract(ll3); + System.out.println(ll2); + + + + + + } + + + /** + * Ѹ + * Ϊ 3->7->10 , úΪ 10->7->3 + */ + public void reverse(){ + LinkedList temp = new LinkedList(); + for(int i = size - 1;i >= 0; i--){ + temp.add(this.get(i)); + } + System.out.println("---"+temp.toString()+"---"); + //ԭ + this.clear(); + + System.out.println("---"+this.toString()+"---"); + for(int i = 0; i < temp.size();i++){ + Object o = temp.get(i); + this.add(o); + } + + } + + /** + * ɾһǰ벿 + * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + + */ + public void removeFirstHalf(){ + if(this.size() == 0){ + return; + } + int temp = this.size(); + for(int i = 1; i <= temp/2; i++){ + this.removeFirst(); + } + + + } + + public void clear(){ + + Iterator itr = this.iterator(); while(itr.hasNext()){ - System.out.println(itr.next()); + this.removeFirst(); + } + this.head = null; + } + + /** + * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * @param i + * @param length + */ + public void remove(int i, int length){ + + for(int j = 0;j < length; j++){ + this.remove(i); + } + + } + /** + * ٶǰlistBе + * ӵǰȡЩlistBָԪ + * 統ǰ = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * صĽӦ[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result = new int[list.size()]; + for(int i = 0; i min && countForMin == 0){ + indexMin = i; + countForMin++; + } + if(eleBack < max && countForMax == 0){ + indexMax = this.size()-1-i; + countForMax++; + } + } + + if(indexMin != -1 && indexMax != -1){ + for(int i = indexMin; i <= indexMax; i++){ + this.remove(indexMin); + } + + } + + } + + /** + * 赱ǰͲlistָԪֵУͬһеԪֵͬ + * ҪCԪΪǰlistԪصĽұCеԪֵ + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList result = new LinkedList(); + for(int i = 0; i < this.size(); i++){ + Integer temp1 = (Integer)this.get(i); + for(int j = 0; j < list.size(); j++){ + Integer temp2 = (Integer)list.get(j); + if(temp1 == temp2){ + result.add(temp2); + } + + } } + return result; } } diff --git a/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java index a4f2c14606..24b9d8b155 100644 --- a/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java +++ b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java @@ -1,10 +1,6 @@ package com.coding.basic.linklist; -/** - * 用双向链表实现LRU算法 - * @author liuxin - * - */ + public class LRUPageFrame { private static class Node { @@ -19,13 +15,13 @@ private static class Node { private int capacity; - + private int currentSize; private Node first;// 链表头 private Node last;// 链表尾 public LRUPageFrame(int capacity) { - + this.currentSize = 0; this.capacity = capacity; } @@ -38,11 +34,117 @@ public LRUPageFrame(int capacity) { */ public void access(int pageNum) { + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + + moveExistingNodeToHead(node); + + } else{ + + node = new Node(); + node.pageNum = pageNum; + + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + + + + } + } + private void addNewNodetoHead(Node node) { + + if(isEmpty()){ + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + } + + + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + public String toString(){ StringBuilder buffer = new StringBuilder(); Node node = first; @@ -57,4 +159,6 @@ public String toString(){ return buffer.toString(); } + + } diff --git a/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java index 67cf36067b..7fd72fc2b4 100644 --- a/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java +++ b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -26,6 +26,9 @@ public void testAccess() { Assert.assertEquals("0,3,2", frame.toString()); frame.access(4); Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + } } diff --git a/liuxin/data-structure/src/com/coding/basic/stack/Stack.java b/liuxin/data-structure/src/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/liuxin/data-structure/src/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/liuxin/data-structure/src/com/coding/basic/stack/StackUtil.java b/liuxin/data-structure/src/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..70bd34e37f --- /dev/null +++ b/liuxin/data-structure/src/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,136 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + public static void bad_reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Integer top = s.pop(); + reverse(s); + addToBottom(s,top); + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() stack = new Stack(); + for(int i=0;i s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/liuxin/data-structure/src/com/coding/basic/stack/expr/InfixExpr.java b/liuxin/data-structure/src/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..a089a2b2c0 --- /dev/null +++ b/liuxin/data-structure/src/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + return 0.0f; + } + + + + +} diff --git a/liuxin/data-structure/src/com/coding/basic/stack/expr/InfixExprTest.java b/liuxin/data-structure/src/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20f52b4c63 --- /dev/null +++ b/liuxin/data-structure/src/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..88f60c77f6 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java @@ -0,0 +1,19 @@ +package com.coderising.jvm.attr; + +public abstract class AttributeInfo { + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + int attrNameIndex; + int attrLen ; + public AttributeInfo(int attrNameIndex, int attrLen) { + + this.attrNameIndex = attrNameIndex; + this.attrLen = attrLen; + } + + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..ee7d423c3b --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java @@ -0,0 +1,56 @@ +package com.coderising.jvm.attr; + +import com.coderising.jvm.clz.ClassFile; +import com.coderising.jvm.constant.ConstantPool; +import com.coderising.jvm.loader.ByteCodeIterator; + + +public class CodeAttr extends AttributeInfo { + private int maxStack ; + private int maxLocals ; + private int codeLen ; + private String code; + public String getCode() { + return code; + } + + //private ByteCodeCommand[] cmds ; + //public ByteCodeCommand[] getCmds() { + // return cmds; + //} + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + + public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code /*ByteCodeCommand[] cmds*/) { + super(attrNameIndex, attrLen); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.codeLen = codeLen; + this.code = code; + //this.cmds = cmds; + } + + public void setLineNumberTable(LineNumberTable t) { + this.lineNumTable = t; + } + + public void setLocalVariableTable(LocalVariableTable t) { + this.localVarTable = t; + } + + public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ + + + return null; + } + private void setStackMapTable(StackMapTable t) { + this.stackMapTable = t; + + } + + + + + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..d88752e323 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java @@ -0,0 +1,42 @@ +package com.coderising.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.jvm.loader.ByteCodeIterator; + +public class LineNumberTable extends AttributeInfo { + List items = new ArrayList(); + + private static class LineNumberItem{ + int startPC; + int lineNum; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + public void addLineNumberItem(LineNumberItem item){ + this.items.add(item); + } + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + + } + + public static LineNumberTable parse(ByteCodeIterator iter){ + + return null; + } + + + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/LocalVariableItem.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..962c3b8bc4 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/LocalVariableItem.java @@ -0,0 +1,39 @@ +package com.coderising.jvm.attr; + +public class LocalVariableItem { + private int startPC; + private int length; + private int nameIndex; + private int descIndex; + private int index; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getNameIndex() { + return nameIndex; + } + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + public int getDescIndex() { + return descIndex; + } + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/LocalVariableTable.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..e4d3df0e77 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/LocalVariableTable.java @@ -0,0 +1,28 @@ +package com.coderising.jvm.attr; + + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.jvm.constant.ConstantPool; + +import com.coderising.jvm.loader.ByteCodeIterator; + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LocalVariableTable parse(ByteCodeIterator iter){ + + return null; + } + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/StackMapTable.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..18f2ad0360 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/StackMapTable.java @@ -0,0 +1,30 @@ +package com.coderising.jvm.attr; + + +import com.coderising.jvm.loader.ByteCodeIterator; + +public class StackMapTable extends AttributeInfo{ + + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.nextU2ToInt(); + int len = iter.nextU4ToInt(); + StackMapTable t = new StackMapTable(index,len); + + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + String code = iter.nextUxToHexString(len); + t.setOriginalCode(code); + + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/clz/AccessFlag.java b/liuxin/mini-jvm/src/com/coderising/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..faae056835 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package com.coderising.jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + +} \ No newline at end of file diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/clz/ClassFile.java b/liuxin/mini-jvm/src/com/coderising/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..4755266987 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/clz/ClassFile.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.clz; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.jvm.constant.ClassInfo; +import com.coderising.jvm.constant.ConstantPool; +import com.coderising.jvm.field.Field; +import com.coderising.jvm.method.Method; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + private List fields = new ArrayList(); + private List methods = new ArrayList(); + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public void addField(Field f){ + this.fields.add(f); + } + public List getFields(){ + return this.fields; + } + public void addMethod(Method m){ + this.methods.add(m); + } + public List getMethods() { + return methods; + } + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + + System.out.println("Super Class Name:"+ getSuperClassName()); + + + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/clz/ClassIndex.java b/liuxin/mini-jvm/src/com/coderising/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..e424f284b3 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package com.coderising.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/ClassInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..aea9048ea4 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/ClassInfo.java @@ -0,0 +1,24 @@ +package com.coderising.jvm.constant; + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/ConstantInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..466b072244 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.coderising.jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + protected ConstantPool constantPool; + + public ConstantInfo(){ + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/ConstantPool.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..86c0445695 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/ConstantPool.java @@ -0,0 +1,29 @@ +package com.coderising.jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/FieldRefInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..65475e194c --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.coderising.jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/MethodRefInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..7f05870020 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.coderising.jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/NameAndTypeInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..402f9dec86 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.coderising.jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/NullConstantInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..936736016f --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.coderising.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/StringInfo.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..f1f8eb4ed4 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.coderising.jvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/constant/UTF8Info.java b/liuxin/mini-jvm/src/com/coderising/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..5cac9f04f7 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.coderising.jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/field/Field.java b/liuxin/mini-jvm/src/com/coderising/jvm/field/Field.java new file mode 100644 index 0000000000..0d1c64587c --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/field/Field.java @@ -0,0 +1,33 @@ +package com.coderising.jvm.field; + +import com.coderising.jvm.constant.ConstantPool; +import com.coderising.jvm.constant.UTF8Info; +import com.coderising.jvm.loader.ByteCodeIterator; + + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + + + private ConstantPool pool; + + public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + + + public static Field parse(ConstantPool pool,ByteCodeIterator iter){ + + return null; + } + +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..6fb5570dff --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,57 @@ +package com.coderising.jvm.loader; + +import java.util.Arrays; + +import com.coderising.jvm.util.Util; + +public class ByteCodeIterator { + byte[] codes; + int pos = 0; + + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1toInt() { + + return Util.byteToInt(new byte[] { codes[pos++] }); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++] }); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] }); + } + + public String nextU4ToHexString() { + return Util.byteToHexString((new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] })); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + + } + + public void back(int n) { + this.pos -= n; + } +} diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java index 86d4619407..9ff27b16cf 100644 --- a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java +++ b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -1,8 +1,20 @@ package com.coderising.jvm.loader; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.coderising.jvm.clz.ClassFile; + + + public class ClassFileLoader { @@ -11,24 +23,118 @@ public class ClassFileLoader { public byte[] readBinaryCode(String className) { - return null; + className = className.replace('.', File.separatorChar) +".class"; + + for(String path : this.clzPaths){ + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } + } + + return null; + } + private byte[] loadClassFile(String clzFileName) { + + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); } public String getClassPath(){ - return null; + return StringUtils.join(this.clzPaths,";"); } + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + + } + + + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + /** + * 下面是第三次JVM课应实现的测试用例 + */ + @Test + public void testReadFields(){ + + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + @Test + public void testMethods(){ + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab7000c2a2bb5000f2a1cb50011b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb5000fb1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50011b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b2001c1222b60024b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb000159122b101db7002d4c2bb6002fb1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } + } diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/util/Util.java b/liuxin/mini-jvm/src/com/coderising/jvm/util/Util.java new file mode 100644 index 0000000000..0c4cc8c57c --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/util/Util.java @@ -0,0 +1,24 @@ +package com.coderising.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i