Template of Windows TypePerf cmd and Impl a CSVParser class+test
This commit is contained in:
parent
216f6dcb9b
commit
315932070a
11 changed files with 349 additions and 18 deletions
BIN
Zutil.jar
BIN
Zutil.jar
Binary file not shown.
|
|
@ -40,9 +40,16 @@ public class Timer {
|
|||
reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Will start or restart the timer if it is already running
|
||||
*/
|
||||
public void start(){
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Will reset the timer so that {@link #hasTimedOut()} returns true
|
||||
*/
|
||||
public void reset(){
|
||||
timestamp = -1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ package zutil.osal;
|
|||
/**
|
||||
* Created by Ziver on 2015-04-07.
|
||||
*/
|
||||
public class HALLinuxImpl implements HardwareAbstractionLayer{
|
||||
public class HalLinuxImpl implements HardwareAbstractionLayer{
|
||||
|
||||
protected HALLinuxImpl(){}
|
||||
protected HalLinuxImpl(){}
|
||||
}
|
||||
|
|
@ -28,7 +28,7 @@ import java.io.File;
|
|||
* User: Ziver
|
||||
*/
|
||||
public class OsalLinuxImpl extends OSAbstractionLayer {
|
||||
private static HALLinuxImpl hal;
|
||||
private static HalLinuxImpl hal;
|
||||
|
||||
@Override
|
||||
public OSType getOSType() {
|
||||
|
|
@ -78,7 +78,7 @@ public class OsalLinuxImpl extends OSAbstractionLayer {
|
|||
@Override
|
||||
public HardwareAbstractionLayer getHAL() {
|
||||
if(hal == null)
|
||||
hal = new HALLinuxImpl();
|
||||
hal = new HalLinuxImpl();
|
||||
return hal;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@
|
|||
package zutil.osal.app.linux;
|
||||
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.Timer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
|
|
@ -46,13 +47,13 @@ public class ProcStat {
|
|||
private static ArrayList<CpuStats> cpus = new ArrayList<CpuStats>();
|
||||
private static long uptime;
|
||||
private static long processes;
|
||||
private static long updateTimestamp;
|
||||
private static Timer updateTimer = new Timer(TTL);
|
||||
|
||||
|
||||
private synchronized static void update(){
|
||||
if(System.currentTimeMillis() - updateTimestamp < TTL)
|
||||
if(updateTimer.hasTimedOut())
|
||||
return;
|
||||
updateTimestamp = System.currentTimeMillis();
|
||||
updateTimer.start();
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(PROC_PATH));
|
||||
String line = null;
|
||||
|
|
|
|||
57
src/zutil/osal/app/windows/TypePerf.java
Normal file
57
src/zutil/osal/app/windows/TypePerf.java
Normal file
File diff suppressed because one or more lines are too long
110
src/zutil/parser/CSVParser.java
Normal file
110
src/zutil/parser/CSVParser.java
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (c) 2015 ezivkoc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser;
|
||||
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Created by Ziver
|
||||
*/
|
||||
public class CSVParser extends Parser{
|
||||
|
||||
private Reader in;
|
||||
private char delimiter;
|
||||
private boolean parseHeader;
|
||||
|
||||
private DataNode headers;
|
||||
|
||||
|
||||
public CSVParser(Reader in){
|
||||
this(in, false, ',');
|
||||
}
|
||||
public CSVParser(Reader in, boolean inclusedHeader){
|
||||
this(in, inclusedHeader, ',');
|
||||
}
|
||||
public CSVParser(Reader in, boolean includesHeader, char delimiter){
|
||||
this.in = in;
|
||||
this.delimiter = delimiter;
|
||||
this.parseHeader = includesHeader;
|
||||
}
|
||||
|
||||
public DataNode getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts parsing from a string
|
||||
*
|
||||
* @param csv is the JSON String to parse
|
||||
* @return a DataNode object representing the JSON in the input String
|
||||
*/
|
||||
public static DataNode read(String csv){
|
||||
try{
|
||||
return new CSVParser(new StringReader(csv)).read();
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}catch (NullPointerException e){}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts parsing from the input.
|
||||
* This method will block until one row has been parsed.
|
||||
*
|
||||
* @return a DataNode object representing a row in the CSV
|
||||
*/
|
||||
@Override
|
||||
public DataNode read() throws IOException {
|
||||
if(parseHeader) {
|
||||
parseHeader = false;
|
||||
headers = read();
|
||||
}
|
||||
|
||||
DataNode data = new DataNode(DataNode.DataType.List);
|
||||
StringBuilder value = new StringBuilder();
|
||||
boolean quoteStarted = false;
|
||||
int c;
|
||||
while((c=in.read()) >= 0 && c != '\n'){
|
||||
if(c == delimiter && !quoteStarted){
|
||||
data.add(value.toString());
|
||||
value.delete(0, value.length()); // Reset StringBuilder
|
||||
}
|
||||
else if(c == '\"' && // Ignored quotes
|
||||
(value.length() == 0 || quoteStarted)){
|
||||
quoteStarted = !quoteStarted;
|
||||
}
|
||||
else
|
||||
value.append((char)c);
|
||||
}
|
||||
if(value.length() > 0)
|
||||
data.add(value.toString());
|
||||
if(data.size() == 0)
|
||||
return null;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
44
src/zutil/parser/Parser.java
Normal file
44
src/zutil/parser/Parser.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2015 ezivkoc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser;
|
||||
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Created by Ziver
|
||||
*/
|
||||
public abstract class Parser {
|
||||
|
||||
/**
|
||||
* Starts parsing data from the input.
|
||||
* This method will block until one {@link DataNode} has been parsed.
|
||||
*
|
||||
* @return a DataNode object representing one item from the input data
|
||||
* or null it it is the end of the stream
|
||||
*/
|
||||
public abstract DataNode read() throws IOException;
|
||||
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ package zutil.parser.json;
|
|||
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.DataNode.DataType;
|
||||
import zutil.parser.Parser;
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -36,7 +37,7 @@ import java.util.regex.Pattern;
|
|||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class JSONParser{
|
||||
public class JSONParser extends Parser {
|
||||
public static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9.]++$");
|
||||
public static final Pattern BOOLEAN_PATTERN = Pattern.compile("^(true|false)$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
|
|
@ -47,11 +48,12 @@ public class JSONParser{
|
|||
}
|
||||
|
||||
/**
|
||||
* Starts parsing from the InputStream.
|
||||
* Starts parsing from the input.
|
||||
* This method will block until one root tree has been parsed.
|
||||
*
|
||||
* @return a DataNode object representing the input JSON
|
||||
*/
|
||||
@Override
|
||||
public DataNode read() throws IOException {
|
||||
return parse(in, new MutableInt());
|
||||
}
|
||||
|
|
@ -80,11 +82,13 @@ public class JSONParser{
|
|||
DataNode node = null;
|
||||
end.i = 0;
|
||||
|
||||
char c = '_';
|
||||
while((c=(char)in.read()) < 0 || Character.isWhitespace(c) ||
|
||||
c == ',' || c == ':');
|
||||
int c = '_';
|
||||
while((c=in.read()) >= 0 &&
|
||||
(Character.isWhitespace(c) || c == ',' || c == ':'));
|
||||
|
||||
switch( c ){
|
||||
// End of stream
|
||||
case -1: break;
|
||||
// This is the end of an Map or List
|
||||
case ']':
|
||||
case '}':
|
||||
|
|
@ -114,19 +118,19 @@ public class JSONParser{
|
|||
case '\"':
|
||||
root = new DataNode(DataType.String);
|
||||
StringBuilder str = new StringBuilder();
|
||||
while((c=(char)in.read()) >= 0 && c != '\"')
|
||||
str.append(c);
|
||||
while((c=in.read()) >= 0 && c != '\"')
|
||||
str.append((char)c);
|
||||
root.set(str.toString());
|
||||
break;
|
||||
// Parse unknown type
|
||||
default:
|
||||
StringBuilder tmp = new StringBuilder().append(c);
|
||||
while((c=(char)in.read()) >= 0 && c != ',' && c != '='){
|
||||
StringBuilder tmp = new StringBuilder().append((char)c);
|
||||
while((c=in.read()) >= 0 && c != ',' && c != '='){
|
||||
if(c == ']' || c == '}'){
|
||||
end.i = 1;
|
||||
break;
|
||||
}
|
||||
tmp.append(c);
|
||||
tmp.append((char)c);
|
||||
}
|
||||
// Check what type of type the data is
|
||||
String data = tmp.toString().trim();
|
||||
|
|
|
|||
108
src/zutil/test/CSVParserTest.java
Normal file
108
src/zutil/test/CSVParserTest.java
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 2015 ezivkoc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.test;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.parser.CSVParser;
|
||||
import zutil.parser.DataNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Created by ezivkoc on 2015-07-30.
|
||||
*/
|
||||
public class CSVParserTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void emptyTest(){
|
||||
DataNode node = CSVParser.read("");
|
||||
assertEquals(null, node);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleTest(){
|
||||
DataNode node = CSVParser.read("hello,world,you");
|
||||
assertEquals(3, node.size());
|
||||
assertEquals("hello", node.get(0).getString());
|
||||
assertEquals("world", node.get(1).getString());
|
||||
assertEquals("you", node.get(2).getString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleHeaderTest() throws IOException {
|
||||
CSVParser parser = new CSVParser(new StringReader("where,what,who\nhello,world,you"), true);
|
||||
DataNode node = parser.read();
|
||||
assertEquals(3, node.size());
|
||||
assertEquals("hello", node.get(0).getString());
|
||||
assertEquals("world", node.get(1).getString());
|
||||
assertEquals("you", node.get(2).getString());
|
||||
node = parser.getHeaders();
|
||||
assertEquals("where", node.get(0).getString());
|
||||
assertEquals("what", node.get(1).getString());
|
||||
assertEquals("who", node.get(2).getString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleMultilineTest() throws IOException {
|
||||
CSVParser parser = new CSVParser(
|
||||
new StringReader("hello,world,you\nhello,world,you\nhello,world,you"));
|
||||
int rows=0;
|
||||
for(DataNode node = parser.read(); node != null; node=parser.read(), ++rows) {
|
||||
assertEquals(3, node.size());
|
||||
assertEquals("hello", node.get(0).getString());
|
||||
assertEquals("world", node.get(1).getString());
|
||||
assertEquals("you", node.get(2).getString());
|
||||
}
|
||||
assertEquals(3, rows);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quotedTest(){
|
||||
DataNode node = CSVParser.read("\"hello\",\"world\",\"you\"");
|
||||
assertEquals(3, node.size());
|
||||
assertEquals("hello", node.get(0).getString());
|
||||
assertEquals("world", node.get(1).getString());
|
||||
assertEquals("you", node.get(2).getString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quotedIncorrectlyTest(){
|
||||
DataNode node = CSVParser.read("hello\",wo\"rl\"d,\"you\"");
|
||||
assertEquals(3, node.size());
|
||||
assertEquals("hello\"", node.get(0).getString());
|
||||
assertEquals("wo\"rl\"d", node.get(1).getString());
|
||||
assertEquals("you", node.get(2).getString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quotedCommaTest(){
|
||||
DataNode node = CSVParser.read("hello,\"world,you\"");
|
||||
assertEquals(2, node.size());
|
||||
assertEquals("hello", node.get(0).getString());
|
||||
assertEquals("world,you", node.get(1).getString());
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertNull;
|
||||
|
||||
|
||||
public class JSONTest{
|
||||
public class JSONParserTest {
|
||||
|
||||
@Test
|
||||
public void nullString(){
|
||||
Loading…
Add table
Add a link
Reference in a new issue