You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
294 lines
11 KiB
294 lines
11 KiB
15 years ago
|
// Column.java
|
||
17 years ago
|
// (C) 2006 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||
19 years ago
|
// first published 24.05.2006 on http://www.anomic.de
|
||
|
//
|
||
|
// This is a part of the kelondro database,
|
||
|
// which is a part of YaCy, a peer-to-peer based web search engine
|
||
|
//
|
||
|
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
||
|
// $LastChangedRevision: 1986 $
|
||
|
// $LastChangedBy: orbiter $
|
||
|
//
|
||
|
// LICENSE
|
||
|
//
|
||
|
// 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; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
15 years ago
|
package net.yacy.kelondro.index;
|
||
19 years ago
|
|
||
16 years ago
|
import de.anomic.kelondro.util.kelondroException;
|
||
16 years ago
|
|
||
|
public class Column {
|
||
19 years ago
|
|
||
19 years ago
|
public static final int celltype_undefined = 0;
|
||
|
public static final int celltype_boolean = 1;
|
||
19 years ago
|
public static final int celltype_binary = 2;
|
||
19 years ago
|
public static final int celltype_string = 3;
|
||
|
public static final int celltype_cardinal = 4;
|
||
18 years ago
|
public static final int celltype_bitfield = 5;
|
||
19 years ago
|
|
||
19 years ago
|
public static final int encoder_none = 0;
|
||
|
public static final int encoder_b64e = 1;
|
||
19 years ago
|
public static final int encoder_b256 = 2;
|
||
19 years ago
|
public static final int encoder_bytes = 3;
|
||
19 years ago
|
|
||
16 years ago
|
public int cellwidth;
|
||
|
public final String nickname;
|
||
|
protected final int celltype;
|
||
|
protected final int encoder;
|
||
|
protected final String description;
|
||
19 years ago
|
|
||
16 years ago
|
public Column(final String nickname, final int celltype, final int encoder, final int cellwidth, final String description) {
|
||
19 years ago
|
this.celltype = celltype;
|
||
19 years ago
|
this.cellwidth = cellwidth;
|
||
19 years ago
|
this.encoder = encoder;
|
||
19 years ago
|
this.nickname = nickname;
|
||
|
this.description = description;
|
||
|
}
|
||
|
|
||
16 years ago
|
public Column(String celldef) {
|
||
19 years ago
|
// define column with column syntax
|
||
|
// example: <UDate-3>
|
||
|
|
||
|
// cut quotes etc.
|
||
19 years ago
|
celldef = celldef.trim();
|
||
19 years ago
|
if (celldef.startsWith("<")) celldef = celldef.substring(1);
|
||
|
if (celldef.endsWith(">")) celldef = celldef.substring(0, celldef.length() - 1);
|
||
|
|
||
|
// parse type definition
|
||
|
int p = celldef.indexOf(' ');
|
||
|
String typename = "";
|
||
|
if (p < 0) {
|
||
|
// no typedef
|
||
|
this.celltype = celltype_undefined;
|
||
|
this.cellwidth = -1;
|
||
|
} else {
|
||
|
typename = celldef.substring(0, p);
|
||
|
celldef = celldef.substring(p + 1).trim();
|
||
|
|
||
|
if (typename.equals("boolean")) {
|
||
|
this.celltype = celltype_boolean;
|
||
|
this.cellwidth = 1;
|
||
|
} else if (typename.equals("byte")) {
|
||
|
this.celltype = celltype_cardinal;
|
||
|
this.cellwidth = 1;
|
||
|
} else if (typename.equals("short")) {
|
||
|
this.celltype = celltype_cardinal;
|
||
|
this.cellwidth = 2;
|
||
|
} else if (typename.equals("int")) {
|
||
|
this.celltype = celltype_cardinal;
|
||
|
this.cellwidth = 4;
|
||
|
} else if (typename.equals("long")) {
|
||
|
this.celltype = celltype_cardinal;
|
||
|
this.cellwidth = 8;
|
||
|
} else if (typename.equals("byte[]")) {
|
||
|
this.celltype = celltype_binary;
|
||
|
this.cellwidth = -1; // yet undefined
|
||
|
} else if (typename.equals("char")) {
|
||
|
this.celltype = celltype_string;
|
||
|
this.cellwidth = 1;
|
||
|
} else if (typename.equals("String")) {
|
||
|
this.celltype = celltype_string;
|
||
|
this.cellwidth = -1; // yet undefined
|
||
|
} else if (typename.equals("Cardinal")) {
|
||
|
this.celltype = celltype_cardinal;
|
||
|
this.cellwidth = -1; // yet undefined
|
||
18 years ago
|
} else if (typename.equals("Bitfield")) {
|
||
|
this.celltype = celltype_bitfield;
|
||
|
this.cellwidth = -1; // yet undefined
|
||
19 years ago
|
} else {
|
||
|
throw new kelondroException("kelondroColumn - undefined type def '" + typename + "'");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// parse length
|
||
|
p = celldef.indexOf('-');
|
||
|
if (p < 0) {
|
||
|
// if the cell was defined with a type, we dont need to give an explicit with definition
|
||
|
if (this.cellwidth < 0) throw new kelondroException("kelondroColumn - no cell width definition given");
|
||
17 years ago
|
final int q = celldef.indexOf(' ');
|
||
19 years ago
|
if (q < 0) {
|
||
19 years ago
|
this.nickname = celldef;
|
||
|
celldef = "";
|
||
|
} else {
|
||
|
this.nickname = celldef.substring(0, p);
|
||
19 years ago
|
celldef = celldef.substring(q + 1);
|
||
19 years ago
|
}
|
||
|
} else {
|
||
19 years ago
|
this.nickname = celldef.substring(0, p);
|
||
17 years ago
|
final int q = celldef.indexOf(' ');
|
||
19 years ago
|
if (q < 0) {
|
||
|
try {
|
||
|
this.cellwidth = Integer.parseInt(celldef.substring(p + 1));
|
||
17 years ago
|
} catch (final NumberFormatException e) {
|
||
19 years ago
|
throw new kelondroException("kelondroColumn - cellwidth description wrong:" + celldef.substring(p + 1));
|
||
|
}
|
||
|
celldef = "";
|
||
|
} else {
|
||
|
try {
|
||
|
this.cellwidth = Integer.parseInt(celldef.substring(p + 1, q));
|
||
17 years ago
|
} catch (final NumberFormatException e) {
|
||
19 years ago
|
throw new kelondroException("kelondroColumn - cellwidth description wrong:" + celldef.substring(p + 1, q));
|
||
|
}
|
||
|
celldef = celldef.substring(q + 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// check length constraints
|
||
19 years ago
|
if (this.cellwidth < 0) throw new kelondroException("kelondroColumn - no cell width given for " + this.nickname);
|
||
19 years ago
|
if (((typename.equals("boolean")) && (this.cellwidth > 1)) ||
|
||
|
((typename.equals("byte")) && (this.cellwidth > 1)) ||
|
||
|
((typename.equals("short")) && (this.cellwidth > 2)) ||
|
||
|
((typename.equals("int")) && (this.cellwidth > 4)) ||
|
||
|
((typename.equals("long")) && (this.cellwidth > 8)) ||
|
||
|
((typename.equals("char")) && (this.cellwidth > 1))
|
||
|
) throw new kelondroException("kelondroColumn - cell width " + this.cellwidth + " too wide for type " + typename);
|
||
16 years ago
|
/*
|
||
19 years ago
|
if (((typename.equals("short")) && (this.cellwidth <= 1)) ||
|
||
|
((typename.equals("int")) && (this.cellwidth <= 2)) ||
|
||
|
((typename.equals("long")) && (this.cellwidth <= 4))
|
||
|
) throw new kelondroException("kelondroColumn - cell width " + this.cellwidth + " not appropriate for type " + typename);
|
||
16 years ago
|
*/
|
||
19 years ago
|
// parse/check encoder type
|
||
|
if ((celldef.length() > 0) && (celldef.charAt(0) == '{')) {
|
||
|
p = celldef.indexOf('}');
|
||
17 years ago
|
final String expf = celldef.substring(1, p);
|
||
19 years ago
|
celldef = celldef.substring(p + 1).trim();
|
||
|
if (expf.equals("b64e")) this.encoder = encoder_b64e;
|
||
19 years ago
|
else if (expf.equals("b256")) this.encoder = encoder_b256;
|
||
|
else if (expf.equals("bytes")) this.encoder = encoder_bytes;
|
||
19 years ago
|
else {
|
||
|
if (this.celltype == celltype_undefined) this.encoder = encoder_bytes;
|
||
|
else if (this.celltype == celltype_boolean) this.encoder = encoder_bytes;
|
||
|
else if (this.celltype == celltype_binary) this.encoder = encoder_bytes;
|
||
|
else if (this.celltype == celltype_string) this.encoder = encoder_bytes;
|
||
15 years ago
|
else throw new kelondroException("kelondroColumn - encoder missing for cell '" + this.nickname + "'");
|
||
19 years ago
|
}
|
||
|
} else {
|
||
|
if (this.celltype == celltype_cardinal) throw new kelondroException("kelondroColumn - encoder missing for cell " + this.nickname);
|
||
|
this.encoder = encoder_bytes;
|
||
|
}
|
||
|
|
||
18 years ago
|
assert (this.celltype != celltype_cardinal) || (this.encoder == encoder_b64e) || (this.encoder == encoder_b256);
|
||
|
|
||
19 years ago
|
// parse/check description
|
||
|
if ((celldef.length() > 0) && (celldef.charAt(0) == '"')) {
|
||
|
p = celldef.indexOf('"', 1);
|
||
|
this.description = celldef.substring(1, p);
|
||
17 years ago
|
//unused: celldef = celldef.substring(p + 1).trim();
|
||
19 years ago
|
} else {
|
||
|
this.description = this.nickname;
|
||
|
}
|
||
|
}
|
||
|
|
||
19 years ago
|
public String toString() {
|
||
16 years ago
|
final StringBuilder s = new StringBuilder();
|
||
19 years ago
|
switch (celltype) {
|
||
18 years ago
|
case celltype_undefined:
|
||
|
s.append(nickname);
|
||
|
s.append('-');
|
||
|
s.append(cellwidth);
|
||
|
break;
|
||
19 years ago
|
case celltype_boolean:
|
||
|
s.append("boolean ");
|
||
19 years ago
|
s.append(nickname);
|
||
19 years ago
|
break;
|
||
|
case celltype_binary:
|
||
|
s.append("byte[] ");
|
||
19 years ago
|
s.append(nickname);
|
||
|
s.append('-');
|
||
|
s.append(cellwidth);
|
||
19 years ago
|
break;
|
||
|
case celltype_string:
|
||
|
s.append("String ");
|
||
19 years ago
|
s.append(nickname);
|
||
|
s.append('-');
|
||
|
s.append(cellwidth);
|
||
19 years ago
|
break;
|
||
|
case celltype_cardinal:
|
||
|
s.append("Cardinal ");
|
||
19 years ago
|
s.append(nickname);
|
||
|
s.append('-');
|
||
|
s.append(cellwidth);
|
||
19 years ago
|
break;
|
||
18 years ago
|
case celltype_bitfield:
|
||
|
s.append("Bitfield ");
|
||
|
s.append(nickname);
|
||
|
s.append('-');
|
||
|
s.append(cellwidth);
|
||
|
break;
|
||
19 years ago
|
}
|
||
19 years ago
|
|
||
19 years ago
|
switch (encoder) {
|
||
|
case encoder_b64e:
|
||
|
s.append(" {b64e}");
|
||
|
break;
|
||
|
case encoder_b256:
|
||
|
s.append(" {b256}");
|
||
|
break;
|
||
|
}
|
||
|
return new String(s);
|
||
|
}
|
||
19 years ago
|
|
||
17 years ago
|
/* (non-Javadoc)
|
||
|
* @see java.lang.Object#hashCode()
|
||
|
*/
|
||
|
@Override
|
||
|
public int hashCode() {
|
||
|
final int prime = 31;
|
||
|
int result = 1;
|
||
|
result = prime * result + celltype;
|
||
|
result = prime * result + cellwidth;
|
||
|
result = prime * result + encoder;
|
||
|
result = prime * result
|
||
|
+ ((nickname == null) ? 0 : nickname.hashCode());
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/* (non-Javadoc)
|
||
|
* @see java.lang.Object#equals(java.lang.Object)
|
||
|
*/
|
||
|
@Override
|
||
|
public boolean equals(Object obj) {
|
||
|
if (this == obj)
|
||
|
return true;
|
||
|
if (obj == null)
|
||
|
return false;
|
||
16 years ago
|
if (!(obj instanceof Column))
|
||
17 years ago
|
return false;
|
||
16 years ago
|
Column other = (Column) obj;
|
||
17 years ago
|
if (celltype != other.celltype)
|
||
|
return false;
|
||
|
if (cellwidth != other.cellwidth)
|
||
|
return false;
|
||
|
if (encoder != other.encoder)
|
||
|
return false;
|
||
|
if (nickname == null) {
|
||
|
if (other.nickname != null)
|
||
|
return false;
|
||
|
} else if (!nickname.equals(other.nickname))
|
||
|
return false;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// public boolean equals(final kelondroColumn otherCol) {
|
||
|
// return
|
||
|
// (this.celltype == otherCol.celltype) &&
|
||
|
// (this.cellwidth == otherCol.cellwidth) &&
|
||
|
// (this.encoder == otherCol.encoder) &&
|
||
|
// (this.nickname.equals(otherCol.nickname));
|
||
|
// }
|
||
19 years ago
|
|
||
19 years ago
|
}
|