📄 periodimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.entities.domain.internal;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.NoSuchElementException;
import org.rapla.components.util.DateTools;
import org.rapla.entities.RaplaType;
import org.rapla.entities.domain.Period;
import org.rapla.entities.storage.Mementable;
import org.rapla.entities.storage.internal.SimpleEntity;
public class PeriodImpl extends SimpleEntity
implements
Period
,java.io.Serializable
,Mementable
{
// Don't forget to increase the serialVersionUID when you change the fields
private static final long serialVersionUID = 1;
private final static long WEEK_MILLIS= DateTools.MILLISECONDS_PER_WEEK;
String name;
Date start;
Date end;
transient Calendar cal;
public PeriodImpl() {
}
public PeriodImpl(Date start, Date end) {
this.start = start;
this.end = end;
}
final public RaplaType getRaplaType() {return TYPE;}
public Date getStart() {
return start;
}
public void setStart(Date start) {
checkWritable();
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
checkWritable();
this.end = end;
}
public int getWeeks()
{
long diff= end.getTime()-start.getTime();
return (int)(((diff-1)/WEEK_MILLIS )+ 1);
}
public String getName(Locale locale) {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
checkWritable();
this.name = name;
}
public boolean contains(Date date) {
return (date.before(end) && !date.before(start));
}
public int weekOf(Date date) {
Calendar cal = Calendar.getInstance(DateTools.getTimeZone());
if (!contains(date))
throw new NoSuchElementException("Period doesn't contain date " + date);
long duration = date.getTime() - start.getTime();
long weeks = duration / (DateTools.MILLISECONDS_PER_WEEK);
// setTimeInMillis has protected access in JDK 1.3.1
cal.setTime(new Date(date.getTime() - weeks * DateTools.MILLISECONDS_PER_WEEK));
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
cal.setTime(getStart());
return ((int)weeks) + 1
+ (((week_of_year) != cal.get(Calendar.WEEK_OF_YEAR))? 1 :0);
}
public String toString() {
return getName();
}
public int compareTo(Date date) {
int result = getEnd().compareTo(date);
if (result == 0)
return 1;
else
return result;
}
public int compareTo(Period period) {
int result = getStart().compareTo(period.getStart());
if (result != 0) return result;
if (equals(period))
return 0;
return (hashCode() < period.hashCode()) ? -1 : 1;
}
public int compareTo(Object o2) {
if (o2 instanceof Date) {
return compareTo((Date) o2);
}
if (o2 instanceof Period) {
return compareTo((Period) o2);
}
throw new ClassCastException("Object needs to be an instance of Date or Period");
}
static private void copy(PeriodImpl source,PeriodImpl dest) {
dest.start = source.start;
dest.end = source.end;
dest.name = source.name;
}
public void copy(Object obj) {
super.copy((PeriodImpl)obj);
copy((PeriodImpl) obj,this);
}
public Object deepClone() {
PeriodImpl clone = new PeriodImpl();
super.deepClone(clone);
copy(this,clone);
return clone;
}
public Object clone() {
PeriodImpl clone = new PeriodImpl();
super.clone(clone);
copy(this,clone);
return clone;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -