📄 data.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jorphan.collections;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
/**
* Use this class to store database-like data. This class uses rows and columns
* to organize its data. It has some convenience methods that allow fast loading
* and retrieval of the data into and out of string arrays. It is also handy for
* reading CSV files.
*
* @author Michael Stover (mstover1 at apache.org)
* @version $Revision: 582659 $
*/
public class Data implements Serializable {
private static Logger log = LoggingManager.getLoggerForClass();
Map data;
// Map iterators = new HashMap();
// Hashtable dataLine;
ArrayList header;
// saves current position in data Vector
int currentPos, size;
/**
* Constructor - takes no arguments.
*/
public Data() {
header = new ArrayList();
data = new HashMap();
currentPos = -1;
size = currentPos + 1;
}
/**
* Replaces the given header name with a new header name.
*
* @param oldHeader
* Old header name.
* @param newHeader
* New header name.
*/
public void replaceHeader(String oldHeader, String newHeader) {
List tempList;
int index = header.indexOf(oldHeader);
header.set(index, newHeader);
tempList = (List) data.remove(oldHeader);
data.put(newHeader, tempList);
}
/**
* Adds the rows of the given Data object to this Data object.
*
* @param d
* data object to be appended to this one
*/
public void append(Data d) {
boolean valid = true;
String[] headers = getHeaders();
String[] dHeaders = d.getHeaders();
if (headers.length != dHeaders.length) {
valid = false;
} else {
for (int count = 0; count < dHeaders.length; count++) {
if (!header.contains(dHeaders[count])) {
valid = false;
}
}
}
if (valid) {
currentPos = size;
d.reset();
while (d.next()) {
for (int count = 0; count < headers.length; count++) {
addColumnValue(headers[count], d.getColumnValue(headers[count]));
}
}
}
}
/**
* Get the number of the current row.
*
* @return integer representing the current row
*/
public int getCurrentPos() {
return currentPos;
}
/**
* Removes the current row.
*/
public void removeRow() {
List tempList;
Iterator it = data.keySet().iterator();
log.debug("removing row, size = " + size);
if (currentPos > -1 && currentPos < size) {
log.debug("got to here");
while (it.hasNext()) {
tempList = (List) data.get(it.next());
tempList.remove(currentPos);
}
if (currentPos > 0) {
currentPos--;
}
size--;
}
}
public void removeRow(int index) {
log.debug("Removing row: " + index);
if (index < size) {
setCurrentPos(index);
log.debug("Setting currentpos to " + index);
removeRow();
}
}
public void addRow() {
String[] headers = getHeaders();
List tempList = new ArrayList();
for (int i = 0; i < headers.length; i++) {
if ((tempList = (ArrayList) data.get(header.get(i))) == null) {
tempList = new ArrayList();
data.put(headers[i], tempList);
}
tempList.add("");
}
size = tempList.size();
setCurrentPos(size - 1);
}
/**
* Sets the current pos. If value sent to method is not a valid number, the
* current position is set to one higher than the maximum.
*
* @param r
* position to set to.
*/
public void setCurrentPos(int r) {
currentPos = r;
}
/**
* Sorts the data using a given row as the sorting criteria. A boolean value
* indicates whether to sort ascending or descending.
*
* @param column
* name of column to use as sorting criteria.
* @param asc
* boolean value indicating whether to sort ascending or
* descending. True for asc, false for desc. Currently this
* feature is not enabled and all sorts are asc.
*/
public void sort(String column, boolean asc) {
sortData(column, 0, size);
}
private void swapRows(int row1, int row2) {
List temp;
Object o;
Iterator it = data.keySet().iterator();
while (it.hasNext()) {
temp = (List) data.get(it.next());
o = temp.get(row1);
temp.set(row1, temp.get(row2));
temp.set(row2, o);
}
}
/**
* Private method that implements the quicksort algorithm to sort the rows
* of the Data object.
*
* @param column
* name of column to use as sorting criteria.
* @param start
* starting index (for quicksort algorithm).
* @param end
* ending index (for quicksort algorithm).
*/
private void sortData(String column, int start, int end) {
int x = start, y = end - 1;
String basis = ((List) data.get(column)).get((x + y) / 2).toString();
if (x == y) {
return;
}
while (x <= y) {
while (x < end && ((List) data.get(column)).get(x).toString().compareTo(basis) < 0) {
x++;
}
while (y >= (start - 1) && ((List) data.get(column)).get(y).toString().compareTo(basis) > 0) {
y--;
}
if (x <= y) {
swapRows(x, y);
x++;
y--;
}
}
if (x == y) {
x++;
}
y = end - x;
if (x > 0) {
sortData(column, start, x);
}
if (y > 0) {
sortData(column, x, end);
}
}
/**
* Gets the number of rows in the Data object.
*
* @return number of rows in Data object.
*/
public int size() {
return size;
} // end method
/**
* Adds a value into the Data set at the current row, using a column name to
* find the column in which to insert the new value.
*
* @param column
* the name of the column to set.
* @param value
* value to set into column.
*/
public void addColumnValue(String column, Object value) {
ArrayList tempList;
if ((tempList = (ArrayList) data.get(column)) == null) {
tempList = new ArrayList();
data.put(column, tempList);
}
int s = tempList.size();
if (currentPos == -1) {
currentPos = size;
}
if (currentPos >= size) {
size = currentPos + 1;
}
while (currentPos > s) {
s++;
tempList.add(null);
}
if (currentPos == s) {
tempList.add(value);
} else {
tempList.set(currentPos, value);
}
}
/**
* Returns the row number where a certain value is.
*
* @param column
* column to be searched for value.
* @param value
* object in Search of.
* @return row # where value exists.
*/
public int findValue(String column, Object value) {
List list = (List) data.get(column);
int ret = -1;
ret = list.indexOf(value);
return ret;
}
/**
* Sets the value in the Data set at the current row, using a column name to
* find the column in which to insert the new value.
*
* @param column
* the name of the column to set.
* @param value
* value to set into column.
*/
public void setColumnValue(String column, Object value) {
List tempList;
if ((tempList = (List) data.get(column)) == null) {
tempList = new ArrayList();
data.put(column, tempList);
}
if (currentPos == -1) {
currentPos = 0;
}
if (currentPos >= size) {
size++;
tempList.add(value);
} else if (currentPos >= tempList.size()) {
tempList.add(value);
} else {
tempList.set(currentPos, value);
}
}
/**
* Checks to see if a column exists in the Data object.
*
* @param column
* Name of column header to check for.
* @return True or False depending on whether the column exists.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -