diff --git a/group03/617187912/Learning02/.classpath b/group03/617187912/Learning02/.classpath index fceb4801b5..07c9acc328 100644 --- a/group03/617187912/Learning02/.classpath +++ b/group03/617187912/Learning02/.classpath @@ -2,5 +2,11 @@ + + + + + + diff --git a/group03/617187912/Learning02/src/com/coderising/array/ArrayUtil.java b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..28f7f0ccc0 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,197 @@ +package com.coderising.array; + +import java.awt.Dialog.ModalExclusionType; +import java.awt.Dimension; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringJoiner; +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 int[] reverseArray(int[] origin) { + int length = origin.length; + int[] nArr = new int[length]; + for (int i = 0; i < length; i++) { + nArr[i] = origin[length - i - 1]; + } + return nArr; + } + + /** + * 现在有如下的一个数组: 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[] newArray = new int[oldArray.length]; + int size = 0; + for (int i : oldArray) { + if (i != 0) { + newArray[size] = i; + size++; + } + } + return Arrays.copyOf(newArray, size); + } + + /** + * 给定两个已经排序好的整形数组, 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) { + Set set = new TreeSet(); + for (int i = 0; i < array1.length; i++) { + set.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + set.add(array2[i]); + } + int[] newArray = new int[set.size()]; + int j = 0; + for (Integer i : set) { + newArray[j++] = i; + } + 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[] 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 static int[] fibonacci(int max) { + if (max <= 1) { + return null; + } + int[] arrFib = new int[max / 2 + 2]; + int current = 0; + int temp = 1; + int pre = 1; + int next = 2; + arrFib[current++] = 1; + arrFib[current++] = 1; + do { + arrFib[current++] = next; + temp = pre; + pre = next; + next = next + temp; + } while (next <= max); + return Arrays.copyOf(arrFib, current); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] arrPrimes = new int[max]; + int current = 0; + arrPrimes[current++] = 2; + Boolean isPrime = true; + for (int i = 3; i < max; i++) { + isPrime = true; + for (Integer j : arrPrimes) { + if (j == 0) { + break; + } + if (i % j == 0) { + isPrime = false; + break; + } + } + if (isPrime == true) { + arrPrimes[current++] = i; + } + } + System.out.println(current); + return Arrays.copyOf(arrPrimes, current); + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 6) { + return null; + } + int[] arrPerNums = new int[max / 6 + 1]; + int current = 0; + arrPerNums[current++] = 6; + for (int i = 7; i < max; i++) { + int sum = 1; + for (int j = 2; j < i / 2+1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + arrPerNums[current++] = i; + } + } + return Arrays.copyOf(arrPerNums, current); + } + + /** + * 用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 = 0; i < array.length; i++) { + sb.append("-"); + sb.append(array[i]); + } + return sb.toString().substring(1); + } + +} diff --git a/group03/617187912/Learning02/src/com/coderising/array/ArrayUtilTest.java b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..64a7bf9553 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,85 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = { 7, 9, 30, 3 }; + int[] b = ArrayUtil.reverseArray(a); + int[] c = ArrayUtil.reverseArray(b); + assertArrayEquals(a, c); + } + + @Test + public void testRemoveZero() { + int[] arrOld={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arrTarget={1,3,4,5,6,6,5,4,7,6,7,5}; + int[] arrResult =ArrayUtil.removeZero(arrOld); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrTarget = {3,4,5,6,7,8}; + int[] arrResult = ArrayUtil.merge(a1, a2); + assertArrayEquals(arrTarget, arrResult); + } + + @Test + public void testGrow() { + int[] arrOld = {2,3,6}; + int[] arrTarget = {2,3,6,0,0,0}; + int[] arrResult = ArrayUtil.grow(arrOld, 3); + assertArrayEquals(arrTarget, arrResult); + } + + @Test + public void testFibonacci() { + int max = 1; + assertNull(ArrayUtil.fibonacci(max)); + max =15; + int[] arrTarget = {1,1,2,3,5,8,13}; + int[] arrResult = ArrayUtil.fibonacci(max); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testGetPrimes() { + int max = 1; + assertNull(ArrayUtil.getPrimes(max)); + max =23; + int[] arrTarget = {2,3,5,7,11,13,17,19}; + int[] arrResult = ArrayUtil.getPrimes(max); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testGetPerfectNumbers() { + int max = 5; + assertNull(ArrayUtil.getPerfectNumbers(max)); + max =8129; + int[] arrTarget = {6,28,496,8128}; + int[] arrResult = ArrayUtil.getPerfectNumbers(max); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testJoin() { + int[] arrOld ={3,8,9}; + String target = "3-8-9"; + String result = ArrayUtil.join(arrOld, "-"); + assertEquals(target, result); + } + +} diff --git a/group03/617187912/Learning02/src/com/coderising/litestruts/LoginAction.java b/group03/617187912/Learning02/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group03/617187912/Learning02/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/group03/617187912/Learning02/src/com/coderising/litestruts/Struts.java b/group03/617187912/Learning02/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..175bb2ecad --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,119 @@ +package com.coderising.litestruts; + +import java.io.File; +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 org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + public static View runAction(String actionName, Map parameters) + throws ClassNotFoundException, DocumentException, InstantiationException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { + + /* + * + * 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[] methodNames = createSetMethodNames(parameters); + Struts.class.getResourceAsStream("/struts.xml"); + Element element = getTargetElement(actionName); + String className = element.attribute(1).getValue(); + Class clz = Class.forName(className); + Object obj = clz.newInstance(); + invokeObjectSetter(parameters, methodNames, clz, obj); + View view = new View(); + view.setParameters(createGetterMap(clz, obj)); + setViewJsp(view, element, clz, obj); + return view; + } + + private static String[] createSetMethodNames(Map parameters) { + String[] methodNames = new String[parameters.size()]; + int i = 0; + for (String key : parameters.keySet()) { + methodNames[i++] = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + } + return methodNames; + } + + private static void setViewJsp(View view, Element element, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + view.setJsp(getJsp(element, executeToGetResult(clz, obj))); + } + + private static Map createGetterMap(Class clz, Object obj) throws IllegalAccessException, InvocationTargetException { + Map map = new HashMap(); + Method[] methods = clz.getMethods(); + for (Method item : methods) { + if (item.getName().contains("get")) { + String key = item.getName().substring(3).toLowerCase(); + Object value = item.invoke(obj); + map.put(key, value); + } + } + return map; + } + + private static String executeToGetResult(Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = clz.getMethod("execute"); + String result = (String) method.invoke(obj); + return result; + } + + private static void invokeObjectSetter(Map parameters, + String[] methodNames, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (String key : methodNames) { + Method method = clz.getMethod(key, String.class); + method.invoke(obj, parameters.get(key)); + } + } + + private static Element getTargetElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + InputStream inputStream =Struts.class.getResourceAsStream("/struts.xml"); + Document document = reader.read(inputStream); + Element rootNode = document.getRootElement(); + List elements = rootNode.elements(); + for (Element item : elements) { + if (actionName.equals(item.attribute(0).getValue())) { + return item; + } + } + return null; + } + + private static String getJsp(Element element, String result) { + List elements = element.elements(); + for (Element e : elements) { + if (result.equals(e.attribute(0).getValue())) { + return e.getTextTrim(); + } + } + return null; + } +} diff --git a/group03/617187912/Learning02/src/com/coderising/litestruts/StrutsTest.java b/group03/617187912/Learning02/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..66dc565ed3 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,49 @@ +package com.coderising.litestruts; + +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 ClassNotFoundException, + InstantiationException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException, DocumentException { + + 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 ClassNotFoundException, + InstantiationException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException, DocumentException { + 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/group03/617187912/Learning02/src/com/coderising/litestruts/View.java b/group03/617187912/Learning02/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group03/617187912/Learning02/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/group03/617187912/Learning02/src/com/coderising/litestruts/struts.xml b/group03/617187912/Learning02/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java b/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java index 353fb84fa4..7e5c0356be 100644 --- a/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java +++ b/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java @@ -17,15 +17,15 @@ public class ArrayUntil { * @return */ public void reverseArray(int[] origin) { - int size=origin.length; - if (size==0||size==1) { + int size = origin.length; + if (size == 0 || size == 1) { return; } - int temp=0; - for (int i = 0; i < size/2; i++) { - temp=origin[i]; - origin[i]=origin[size-1-i]; - origin[size-1-i]=temp; + int temp = 0; + for (int i = 0; i < size / 2; i++) { + temp = origin[i]; + origin[i] = origin[size - 1 - i]; + origin[size - 1 - i] = temp; } } @@ -38,32 +38,31 @@ public void reverseArray(int[] origin) { */ public int[] removeZero(int[] oldArray) { - int size=oldArray.length; - if (size==0) { + int size = oldArray.length; + if (size == 0) { return new int[0]; } for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i]==0) { + if (oldArray[i] == 0) { size--; } } - if (size==0) { + if (size == 0) { return new int[0]; } - int[] noZero=new int[size]; - for (int i = 0,j=0; i < oldArray.length; i++) { - if (oldArray[i]!=0) { - noZero[j]=oldArray[i]; + int[] noZero = new int[size]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + noZero[j] = oldArray[i]; j++; } - i++; } return noZero; } /** * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = - * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * [3,5,7,8] a2 = [4,5,6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 * * @param array1 * @param array2 @@ -71,36 +70,41 @@ public int[] removeZero(int[] oldArray) { */ public int[] merge(int[] array1, int[] array2) { - if (array1.length==0) { + if (array1.length == 0) { return array2; - }if (array2.length==0) { + } + if (array2.length == 0) { return array1; } - int size=array1.length+array2.length; - int[] merged=new int[size]; - int k=0; - for (int i = 0; i < array1.length; i++) { - for (int j = 0; j < array2.length; j++) { - if (array1[i]array2[j]) { - merged[k]=array2[j]; - k++; - j++; - } - else { - merged[k]=array1[i]; - k++; - i++; - j++; - } + int size = array1.length + array2.length; + int[] merged = new int[size]; + int i=0,j=0,k=0; + while (iarray2[j]) { + merged[k]=array2[j]; + j++; } + else { + merged[k]=array1[i]; + i++; + j++; + } + k++; + } + if (i==array1.length-1) { + System.arraycopy(array2, j, merged, k, array2.length-j); + k=k+array2.length-j; + } + if (i==array2.length-1) { + System.arraycopy(array1, i, merged, k, array1.length-i); + k=k+array1.length-i; } - size=k-1; - int[] newArray=new int[size]; + size = k; + int[] newArray = new int[size]; System.arraycopy(merged, 0, newArray, 0, size); return newArray; } @@ -115,36 +119,37 @@ else if (array1[i]>array2[j]) { * @return */ public int[] grow(int[] oldArray, int size) { - int[] newArray=new int[oldArray.length+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, 则返回空数组 [] + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] * * @param max * @return */ public int[] fibonacci(int max) { - if (max==1) { + if (max == 1) { return new int[0]; } - int[] fib={1,1}; - int size=2; - int capacity=2; - int i=1; - while (fib[i] - - - - - - + + + + + + + diff --git a/liuxin/src/com/coderising/download/DownloadThread.java b/liuxin/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..1456314140 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/FileDownloader.java b/liuxin/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f5d7999eb4 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/FileDownloaderTest.java b/liuxin/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..8171ee5763 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/api/Connection.java b/liuxin/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..9710e270e1 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/api/ConnectionException.java b/liuxin/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8dbfe95dda --- /dev/null +++ b/liuxin/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/liuxin/src/com/coderising/download/api/ConnectionManager.java b/liuxin/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..fb44ede457 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/api/DownloadListener.java b/liuxin/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..4cd0b3eab1 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/impl/ConnectionImpl.java b/liuxin/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..32f03efdc7 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java b/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..046f7c49a4 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml index dd598a3664..4c6eeabbd4 100644 --- a/liuxin/src/com/coderising/litestruts/struts.xml +++ b/liuxin/src/com/coderising/litestruts/struts.xml @@ -1,11 +1,11 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + \ No newline at end of file diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java index e2c4e5e795..d6f6ffebec 100644 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ b/liuxin/src/com/coding/basic/LinkedList.java @@ -1,46 +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; - - } -} +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; + } +}