"Array.java"의 두 판 사이의 차이

185번째 줄: 185번째 줄:
}
}
</source>
</source>
==같이 보기==
* [[DBService.java]]
[[분류: java]]

2019년 4월 20일 (토) 15:19 판

1 개요

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.yaml.snakeyaml.Yaml;

import com.google.gson.Gson; // https://mvnrepository.com/artifact/com.google.code.gson/gson

public class Array extends TreeMap<String, Object> {
	private static final long serialVersionUID = 1L;
	private Object value = null;

	public static Array fromJson(String jsonString) {
		Array arr = new Array();
		JsonParser parser = JsonParserFactory.getJsonParser();
		try {
			List<Object> list = parser.parseList(jsonString);
			for( Object obj: list ) arr.push(obj);
		}
		catch(Exception e){
			LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) parser.parseMap(jsonString);
			for( java.util.Map.Entry<String, Object> m: map.entrySet() ) arr.put(m.getKey(), m.getValue());
		}
		return arr;
	}
	public static Array fromYaml(String yamlString) {
		@SuppressWarnings("unchecked")
		Map<String,Object> map = (Map<String, Object>) new Yaml().load(yamlString);
		Array arr = new Array();
		for (Map.Entry<String, Object> entry : map.entrySet()) {
		    arr.put(entry.getKey(), entry.getValue());
		}
		return arr;
	}
	
	public Array() {}
	public Array(Object obj) {
		this.value = obj;
	}

	public Array(Object[] objs) {
		for( Object obj: objs ) this.push(obj);
	}
	public Array(List<Object> objs) {
		for( Object obj: objs ) this.push(obj);
	}
	public Array(Set<String> objs) {
		for( Object obj: objs ) this.push(obj);
	}
	public Array(Collection<Object> objs) {
		for( Object obj: objs ) this.push(obj);
	}
	
	public Array get(int i) {
		return get(String.valueOf(i));
	}
	public Array get(String str) {
		Object item = super.get(str);
		if( item.getClass() == Array.class ) return (Array) item;
		return new Array(item);
	}

	public Integer getInteger() {
		return (int) value;
	}
	public String getString() {
		return (String) value;
	}
	public Object getObject() {
		return value;
	}	
	
	public List<Integer> getIntegerList() {
		List<Integer> list = new ArrayList<Integer>();
		for( Object obj: this.values() )  {
			list.add((Integer) obj);
		}
		return list;
	}
	public List<String> getStringList() {
		List<String> list = new ArrayList<String>();
		for( Object obj: this.values() )  {
			list.add(obj.toString());
		}
		return list;
	}
	public List<Object> getObjectList() {
		List<Object> list = new ArrayList<Object>();
		for( Object obj: this.values() )  {
			list.add(obj);
		}
		return list;
	}	
	
	public Array push(Object obj) {
		int max = -1;
		for (String indexString : this.keySet()) {
			if( isInt(indexString) ) {
				int index = Integer.parseInt(indexString);
				if( index > max ) max = index; 
			}
		}
		return put(max + 1, obj);
	}
	public Array put(int i, Object obj) {
		return put(String.valueOf(i), obj);
	}
	public Array put(String key, Object obj) {
		super.put(key, obj);
		return this;
	}

	public Array keys() {
		return new Array(this.keySet());
	}
	public Array values(boolean dummy) {
		return new Array(this.values());
	}

	public String toString() {
		if( value != null ) return value.toString();
		if( isList() ) return this.values().toString();
		return super.toString();
	}
	public String toJson() {
		return new Gson().toJson( toMapAndList() );		
	}
	public String toYaml() {
		Yaml yaml = new Yaml();
		@SuppressWarnings("unchecked")
		Map<String,Object> map = (Map<String, Object>) yaml.load( toJson() );
		return yaml.dump(map);
	}
	public Boolean contains(String value) {
		for( Object obj: this.values() )  {
			if( obj.toString().equals(value) ) return true;
		}
		return false;
	}
	public Array pluck(String key) {
		if( size()<1 ) return null;
		if( !this.get(0).keySet().contains(key) ) return null;
		Array res = new Array();
		for (String indexString : this.keySet()) {
			res.push( this.get(indexString).get(key) );
		}
		return res;
	}

	// private
	private static boolean isInt(String input) {
		try {
			Integer.parseInt(input);
			return true;
		}
		catch (NumberFormatException e) {
			return false;
		}
	}
	private boolean isList() {
		int i=0;
		for (String key : this.keySet()) {
			if( !key.equals( String.valueOf(i) ) ) return false;
			i++;
		}
		return true;
	}
	private Object toMapAndList() {
		if( value != null ) return value;
		if( !isList() ) return this; 
		List<Object> list = new ArrayList<Object>();
		for( String key: this.keySet() ) {
			list.add( get(key).toMapAndList() );
		}
		return list;
	}
}

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}