Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package io.github.bloepiloepi.spear.objects;
import io.github.bloepiloepi.spear.exceptions.*;
import io.github.bloepiloepi.spear.parser.SPParser;
import io.github.bloepiloepi.spear.validation.SPPath;
import io.github.bloepiloepi.spear.validation.Validation;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
/**
* The main Spear object. This is the object that represents a data file of Spear, in which you can remove, set and get values.
*/
public class SPData extends SPObject {
private ArrayList separatedNodes;
private ArrayList separatedAssignments;
public SPData(ArrayList nodes) {
separate(nodes);
}
private void separate(ArrayList nodes) {
separatedNodes = new ArrayList<>();
separatedAssignments = new ArrayList<>();
for (SPNodeValue node : nodes) {
if (node instanceof SPAssignment) {
separatedAssignments.add((SPAssignment) node);
} else {
separatedNodes.add((SPNode) node);
}
}
}
private ArrayList merge() {
ArrayList merged = new ArrayList<>();
merged.addAll(separatedNodes);
merged.addAll(separatedAssignments);
return merged;
}
private SPAssignment getAssignment(String name) {
for (SPAssignment assignment : separatedAssignments) {
if (assignment.getName().equals(name)) {
return assignment;
}
}
return null;
}
private SPNode getNode(String name) {
for (SPNode node : separatedNodes) {
if (node.getName().equals(name)) {
return node;
}
}
return null;
}
private void removeNode(String name) {
separatedNodes.removeIf(node -> node.getName().equals(name));
}
private void removeAssignment(String name) {
separatedAssignments.removeIf(assignment -> assignment.getName().equals(name));
}
private SPValue get(SPPath path) {
if (path.isLastNode()) {
SPAssignment assignment = getAssignment(path.getCurrentNode());
if (assignment == null) return null;
return assignment.getValue();
} else {
SPNode node = getNode(path.getCurrentNode());
if (node == null) return null;
path.removeCurrentNode();
return node.get(path);
}
}
private void set(SPPath path, Object value) {
if (path.isLastNode()) {
if (getNode(path.getCurrentNode()) != null) {
removeNode(path.getCurrentNode());
}
SPAssignment assignment = getAssignment(path.getCurrentNode());
if (assignment != null) {
assignment.setValue(new SPValue(value));
} else {
separatedAssignments.add(new SPAssignment(path.getCurrentNode(), new SPValue(value)));
}
} else {
if (getAssignment(path.getCurrentNode()) != null) {
removeAssignment(path.getCurrentNode());
}
SPNode node = getNode(path.getCurrentNode());
if (node != null) {
path.removeCurrentNode();
node.set(path, value);
} else {
SPNode newNode = new SPNode(path.getCurrentNode(), new ArrayList<>());
separatedNodes.add(newNode);
path.removeCurrentNode();
newNode.set(path, value);
}
}
}
/**
* Remove a node or assignment. This is the equivalent of setting it to null (which you can't do because Spear doesn't handle null).
* @param path The path to remove
*/
public void remove(String path) {
SPPath spPath = new SPPath(path);
if (spPath.isLastNode()) {
removeAssignment(spPath.getCurrentNode());
removeNode(spPath.getCurrentNode());
} else {
SPNode node = getNode(spPath.getCurrentNode());
if (node != null) {
spPath.removeCurrentNode();
if (node.remove(spPath)) { //aka is node unused now
removeNode(node.getName());
}
}
}
}
/**
* Gives a list of the subnodes of a path you give. Returns null if the path does not exist.
*
* @param path The path to the node to get the list from
* @return The list of subnodes if the node exists, else null
*/
public ArrayList listNodes(String path) {
SPPath spPath = new SPPath(path);
ArrayList nodes = new ArrayList<>();
if (spPath.isLastNode()) {
for (SPNode node : separatedNodes) {
nodes.add(node.getName());
}
for (SPAssignment assignment : separatedAssignments) {
nodes.add(assignment.getName());
}
return nodes;
} else {
SPNode node = getNode(spPath.getCurrentNode());
if (node != null) {
spPath.removeCurrentNode();
nodes.addAll(node.listNodes(spPath));
return nodes;
} else {
return null;
}
}
}
/**
* Set an Integer in the file.
*
* @param path The path to the new Integer
* @param value The value of the new Integer
*/
public void setInteger(String path, Integer value) {
Validation.notNull(value, "Spear can't handle null values! Use the remove() method instead.");
SPPath spPath = new SPPath(path);
set(spPath, value);
}
/**
* Set a Double in the file.
*
* @param path The path to the new Double
* @param value The value of the new Double
*/
public void setDouble(String path, Double value) {
Validation.notNull(value, "Spear can't handle null values! Use the remove() method instead.");
SPPath spPath = new SPPath(path);
set(spPath, value);
}
/**
* Set a String in the file.
*
* @param path The path to the new String
* @param value The value of the new String
*/
public void setString(String path, String value) {
Validation.notNull(value, "Spear can't handle null values! Use the remove() method instead.");
SPPath spPath = new SPPath(path);
set(spPath, value);
}
/**
* Set a Boolean in the file.
*
* @param path The path to the new Boolean
* @param value The value of the new Boolean
*/
public void setBoolean(String path, Boolean value) {
Validation.notNull(value, "Spear can't handle null values! Use the remove() method instead.");
SPPath spPath = new SPPath(path);
set(spPath, value);
}
/**
* Set a List in the file.
*
* @param path The path to the new List
* @param value The value of the new List
*/
public void setList(String path, ArrayList