
StorageHelper.RetrieveByPath Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of DataStorage Show documentation
Show all versions of DataStorage Show documentation
single container for different data formats. json, xml, Array List, HashMap
package StorageHelper;
import Data.Storage;
import DataTools.Utils;
import org.junit.Assert;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
/**
Copyright 2016 Alianza Inc.
Licensed 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.
*/
public class RetrieveByPath {
HashMap storage;
Storage AllData;
public RetrieveByPath(Storage data) {
storage = data.toMap();
AllData = data;
}
/**
* get a saved object by the path given
* @param path String[] of the path
* @return Object that is found from the given path
*/
public Object findElementByPath(String[] path) {
Object current = storage;
for (int j = 0; j < path.length; ++j) {
boolean array = true;
if (!hasGet(current)) {
current = CreatorHelper.createDataStorage(current);
AllData.put(Arrays.copyOfRange(path, 0, j), current);
}
//will know if it is an array because it will be a number to find the item
for (int i = 0; i < path[j].length() && array; ++i) {
if (!Character.isDigit(path[j].charAt(i)))
array = false;
}
try {
//get next object in path, if it is an array need to give an int to find it
if (array && current.getClass().getTypeName().contains("Array"))
current = current.getClass().getMethod("get", new Class[]{int.class}).invoke(current, Integer.parseInt(path[j]));
else
try {
current = current.getClass().getMethod("get", new Class[]{Object.class}).invoke(current, path[j]);
//sometimes it complains because param Object class and String class are different
} catch (NoSuchMethodException e) {
current = current.getClass().getMethod("get", new Class[]{String.class}).invoke(current, path[j]);
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Utils.debug("PATH INVALID:\nPATH: " + getItemsFromArray(path) + "\nDATA: " + AllData.toJson(), "error");
e.printStackTrace();
Assert.fail();
}
}
return current;
}
private boolean hasGet(Object obj) {
boolean found = false;
try {
for (Method method : obj.getClass().getMethods()) {
String methName = method.getName();
if (methName.equals("get"))
found = true;
}
} catch (NullPointerException e) {
Utils.debug("object not found", "error");
}
return found;
}
private String getItemsFromArray (String[] list) {
String items = "";
for (int i = 0; i < list.length; i++) {
items += i + ":" + list[i] + " ";
}
return items;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy