Multi-Startup Set
其实lab3中关于时间段的更详细描述还是应该看看mit实验的问题集。
但在lab2中我们已经练习过了泛型编程,这次就一般到位好了
首先是接口
package IntervalSet;
import java.util.ArrayList;
import java.util.Set;
public interface IntervalSet<L> {
//创建一个空对象:
public static <L> IntervalSet<L> empty(){
return (IntervalSet<L>) new CommonIntervalSet();
}
//返回某个标签对应的时间段的开始时间:
public long start(L label);
//返回某个标签对应的时间段的结束时间:
public long end(L label);
//在当前对象中插入新的时间段和标签:
public void insert(long start, long end, L label);
//获得当前对象中的标签集合:
public Set<L> labels();
//从当前对象中移除某个标签所关联的时间段:
public boolean remove(L label);
//获得与某个标签所关联的时间段
//形式为字符串形式
public String getinterval(L label);
}
我们利用Interval来表示某标签的一段时间
package IntervalSet;
//immutability类
// Abstraction function:
// AF(Interval)=时间段
public class Interval<L> {
private L label;
private long min;
private long max;
private boolean empty;
// checkRep:label!=null min<max;时间段不允许有负数
private boolean checkrep() {
if (min >= max || label == null || min < 0) {
// System.out.println("Min cannot be greater or equal to Max");
return false;
}
return true;
}
/**
* Create a new Interval, if min is greater than or equal to max throws illegal argument exception.
*immutability类
* @param
* @param
* @throws IllegalArgumentException if min is greater or equal to max
*/
public Interval(long min, long max, L label) {
/*RI:(min >= max||label==null)
/*;*/
checkrep();
this.min = min;
this.max = max;
this.label = label;
this.empty = false;
}
/**
* Return the start
*
* @return min
* @throws UnsupportedOperationException if you try to get the min value when the interval in an empty Interval
*/
public long getMin() throws UnsupportedOperationException {
if (this.empty)
throw new UnsupportedOperationException("Empty set does not have min");
else return this.min;
}
public L getlabel() throws UnsupportedOperationException {
if (this.empty)
throw new UnsupportedOperationException("Empty set does not have min");
else return this.label;
}
/**
* Return the end value of the interval, if the set is empty the return a null value
*
* @return max
* @throws UnsupportedOperationException if you try to get the max value when the interval in an empty Interval
*/
public long getMax() throws UnsupportedOperationException {
if (this.empty)
throw new UnsupportedOperationException("Empty set does not have max");
else return this.max;
}
/**
* Returns true if empty set
*
* @return true if empty set, false otherwise
*/
public boolean isEmpty() {
return this.empty;
}
}
最后就是IntervalSet的具体实现
package IntervalSet;
import java.util.*;
public class CommonIntervalSet<L> implements IntervalSet<L> {
List<Interval<L>> intervals=new ArrayList<>();
//AF:
//
public void insert(long start, long end, L label) {
// intervals.add(Interval(start.end));
Interval<L> l= new Interval(start,end, label);
//labels.put(label,l);
for(int i=0;i<intervals.size();i++) {
if(label==intervals.get(i).getlabel()) {
System.out.println("this label has exited");
}
}
intervals.add(l);
//Label.
}
//返回某个标签对应的时间段的开始时间:
public long start (L label) {
for (int i = 0; i < intervals.size(); i++) {
if (intervals.get(i).getlabel() == label) {
return intervals.get(i).getMin();
}
}
System.out.println("the label is not exited");
return -1;
}
//
public long end (L label) {
for (int i = 0; i < intervals.size(); i++) {
if (intervals.get(i).getlabel() == label) {
return intervals.get(i).getMax();
}
}
System.out.println("the label is not exited");
return -1;
}
//获得当前对象中的标签集合
public Set<L> labels(){
Set<L> labels=new HashSet<L>();
for (int i = 0; i < intervals.size(); i++) {
labels.add((L) intervals.get(i).getlabel());
}
return labels;
//return null;
}
// 从当前对象中移除某个标签所关联的时间段
//存在移除并返回true,不存在返回false
public boolean remove(L label) {
for (int i = 0; i < intervals.size(); i++) {
if (intervals.get(i).getlabel() == label) {
intervals.remove(i);
return true;
}
}
return false;
}
//获得与某个标签所关联的时间段
//形式为字符串形式?
public java.lang.String getinterval(L label) {
String str;
for (int i = 0; i < intervals.size(); i++) {
if (intervals.get(i).getlabel() == label) {
return (java.lang.String) (label+"="+ "[" +intervals.get(i).getMin()+","+intervals.get(i).getMax()+"]");
}
}
return (java.lang.String) "the label is not exited";
}
}
测试
因为在insert函数中我们已经考虑过不能重复加入同一个标签问题,所以我们只需测试insert函数是否成功加入即可
package IntervalSet;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.assertEquals;
public class CommonIntervalSetTest {
//标签是否成功加入
@Test
public void testinsert() {
IntervalSet test1=IntervalSet.empty();
test1.insert(1,5,"xyn");
assertEquals("xyn=[1,5]",test1.getinterval("xyn"));
}
//标签是否存在:存在:return start 不存在:return -1
@Test
public void teststart(){
IntervalSet test2=IntervalSet.empty();
test2.insert(1,5,"xyn");
assertEquals(1,test2.start("xyn"));
assertEquals(-1,test2.start("xy"));
}
@Test
//标签是否存在:存在:return start 不存在:return -1
public void testend(){
IntervalSet test3=IntervalSet.empty();
test3.insert(1,5,"xyn");
assertEquals(5,test3.end("xyn"));
assertEquals(-1,test3.end("xy"));
}
//标签是否存在:存在:return true 不存在:return false
@Test
public void testremove(){
IntervalSet test4=IntervalSet.empty();
test4.insert(1,5,"xyn");
assertEquals(true,test4.remove("xyn"));
assertEquals(false,test4.remove("xyn"));
}
@Test
public void testlabels(){
IntervalSet test5=IntervalSet.empty();
test5.insert(1,5,"xyn");
test5.insert(1,5,"xy");
Set<String> test=new HashSet<>();
test.add("xyn");
test.add("xy");
assertEquals(test,test5.labels());
}
}