Dépôt officiel du code source de l'ERP OpenConcerto
Rev 144 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.pos.model;
import org.openconcerto.utils.CompareUtils;
import java.text.Format;
import java.util.Date;
import net.jcip.annotations.Immutable;
/**
* Below the ordered list of possible states :
* <ol>
* <li>{@link Status#OBSOLETE} : there remains obsolete receipts to import (not possible for the
* DB)</li>
* <li>{@link Status#CLOSED} with <code>null</code> date : never been opened (nor obviously closed).
* For FS : no remaining obsolete receipts remain, for DB : first possible state</li>
* <li>{@link Status#OPEN} (date cannot be null)</li>
* <li>{@link Status#CLOSED} with non-<code>null</code> date</li>
* </ol>
*
* @author sylvain
*/
@Immutable
public final class RegisterState {
static public enum Status {
// there remains obsolete receipts to import
OBSOLETE {
@Override
protected void checkDate(Date date) {
if (date != null)
throw new IllegalArgumentException("Cannot have date for " + this);
}
},
CLOSED {
@Override
protected void checkDate(Date date) {
// null means never been opened nor closed but no remaining obsolete receipts remain
}
},
OPEN {
@Override
protected void checkDate(Date date) {
if (date == null)
throw new IllegalArgumentException("Missing date for " + this);
}
};
protected abstract void checkDate(Date date);
}
private final Status status;
// TODO as RegisterLogEntry, use Instant
private final Date date;
public RegisterState(Status status, Date date) {
super();
this.status = status;
this.status.checkDate(date);
this.date = date == null ? null : new Date(date.getTime());
}
public final Status getStatus() {
return this.status;
}
public boolean hasDate() {
return this.date != null;
}
public final int compareDateTo(RegisterState o) {
return this.compareDateTo(o.date);
}
public final int compareDateTo(Date cal) {
return this.date.compareTo(cal);
}
public final String formatDate(final Format f) {
return f.format(this.date);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.date == null) ? 0 : this.date.hashCode());
result = prime * result + this.status.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final RegisterState other = (RegisterState) obj;
return this.status.equals(other.status) && CompareUtils.equals(this.date, other.date);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder(250);
sb.append(this.getClass().getSimpleName());
if (this.getStatus() == Status.OBSOLETE) {
sb.append(' ');
sb.append(this.getStatus());
} else if (this.getStatus() == Status.CLOSED && !this.hasDate()) {
sb.append(" never opened");
} else {
assert this.date != null;
sb.append(' ');
sb.append(this.getStatus());
sb.append(" at ");
sb.append(this.date);
}
return sb.toString();
}
}