PersistableNwnFile.java
// <editor-fold defaultstate="collapsed" desc="license">
/*
* Copyright (c) 2009, Karl H. Beckers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
// </editor-fold>
package net.jarre_de_the.griffin.file;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import net.jarre_de_the.griffin.Util;
import net.jarre_de_the.griffin.types.data.DWordData;
/**
*
* @author charly4711
*/
public abstract class PersistableNwnFile implements Cloneable {
private Map<String, DWordData> headerFields
= new HashMap<String, DWordData>();
public interface HeaderField {
String field();
}
protected enum CommonHeaderField implements HeaderField {
FILE_TYPE("fileType"),
FILE_VERSION("fileVersion");
private final String field;
private CommonHeaderField(String field) {
this.field = field;
}
public String field() {
return field;
}
}
// abstract methods
//
public abstract byte[] persist() throws IOException;
// implementations
//
protected long getHeaderNumber(String fieldKey, Collection c) {
long count = 0L;
DWordData data = getHeaderFields().get(fieldKey);
if (null != data) {
count = data.getValueAsNumber();
} else if (null != c) {
count = c.size();
}
return count;
}
@Override
public PersistableNwnFile clone() throws CloneNotSupportedException {
PersistableNwnFile clone = (PersistableNwnFile) super.clone();
clone.setHeaderFields(new HashMap<String, DWordData>());
clone.setFileType(this.getFileType());
clone.setFileVersion(this.getFileVersion());
return clone;
}
@Override
public boolean equals(Object compare) {
if (compare == this) {
return true;
}
if (!(compare instanceof PersistableNwnFile)) {
return false;
}
PersistableNwnFile file = (PersistableNwnFile) compare;
return areNullablePropertiesEqual(this.getFileType(), file.getFileType())
&& areNullablePropertiesEqual(this.getFileVersion(), file.getFileVersion());
}
@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.headerFields);
return hash;
}
public String getFileType() {
DWordData data = headerFields.get(CommonHeaderField.FILE_TYPE.field());
if (null != data) {
return new String(data.getValueAsByteArray(), Util.CHARSET_US_ASCII);
} else {
return null;
}
}
public void setFileType(String ft) {
if (null != ft) {
headerFields.put(CommonHeaderField.FILE_TYPE.field(),
new DWordData(Util.stringToBitField(ft)));
} else {
headerFields.remove(CommonHeaderField.FILE_TYPE.field());
}
}
public String getFileVersion() {
DWordData data = headerFields.get(CommonHeaderField.FILE_VERSION.field());
if (null != data) {
return new String(data.getValueAsByteArray(), Util.CHARSET_US_ASCII);
} else {
return null;
}
}
public void setFileVersion(String fv) {
if (null != fv) {
headerFields.put(CommonHeaderField.FILE_VERSION.field(),
new DWordData(Util.stringToBitField(fv)));
} else {
headerFields.remove(CommonHeaderField.FILE_VERSION.field());
}
}
protected Map<String, DWordData> getHeaderFields() {
return headerFields;
}
protected void setHeaderFields(Map<String, DWordData> fields) {
this.headerFields = fields;
}
// static methods
public static boolean areNullablePropertiesEqual(Object one, Object two) {
if (null == one && null != two || null != one && null == two) {
return false;
}
// if one == null, then two must be null, too, by now
if (null != one) {
if (one instanceof List && two instanceof List) {
List lOne = (List) one;
List lTwo = (List) two;
if (!(lOne.size() == lTwo.size())) {
return false;
}
for (int i = 0; i < lOne.size(); i++) {
boolean equal = lOne.get(i).equals(lTwo.get(i));
if (!equal) {
return false;
}
}
} else if (one instanceof List && !(two instanceof List)) {
return false;
} else if (one instanceof byte[]) {
return two instanceof byte[] && Arrays.equals((byte[]) one, (byte[]) two);
} else if (one.getClass().isArray()) {
return Arrays.deepEquals((Object[]) one, (Object[]) two);
} else {
return one.equals(two);
}
}
return true;
}
}