View Javadoc
1   package io.guixer.logs;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import java.util.Date;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.apache.commons.lang3.NotImplementedException;
10  
11  import com.google.common.collect.Lists;
12  
13  import net.avcompris.commons3.yaml.Yaml;
14  
15  public abstract class GuixerYamlUtils {
16  
17  	public static Yaml bindSnakeYaml(
18  		final Object o
19  	) {
20  		checkNotNull(o, "o");
21  
22  		return new Yaml() {
23  
24  			@Override
25  			public Iterable<Object> keys() {
26  				throw new NotImplementedException();
27  			}
28  
29  			@Override
30  			public Iterable<String> keysAsStrings() {
31  				throw new NotImplementedException();
32  			}
33  
34  			@Override
35  			public Iterable<Yaml> items() {
36  				if (!(o instanceof List<?>)) {
37  					throw new IllegalStateException("object should be a List, but was: " + o.getClass().getName());
38  				}
39  
40  				final List<Yaml> items = Lists.newArrayList();
41  
42  				for (final Object i : (List<?>) o) {
43  
44  					items.add(bindSnakeYaml(i));
45  				}
46  
47  				return items;
48  			}
49  
50  			@Override
51  			public boolean has(
52  				final Object key
53  			) {
54  				if (!(o instanceof Map<?, ?>)) {
55  					throw new IllegalStateException("object should be a Map, but was: " + o.getClass().getName());
56  				}
57  
58  				return ((Map<?, ?>) o).containsKey(key);
59  			}
60  
61  			@Override
62  			public Yaml get(
63  				final Object key
64  			) {
65  				if (!(o instanceof Map<?, ?>)) {
66  					throw new IllegalStateException("object should be a Map, but was: " + o.getClass().getName());
67  				}
68  
69  				return bindSnakeYaml(((Map<?, ?>) o).get(key));
70  			}
71  
72  			@Override
73  			public String asString() {
74  				if (o instanceof String) {
75  					return (String) o;
76  				} else if (o instanceof Integer) {
77  					return o.toString();
78  				} else {
79  					throw new NotImplementedException("o.class: " + o.getClass().getName());
80  				}
81  			}
82  
83  			@Override
84  			public int asInt() {
85  				if (o instanceof Integer) {
86  					return (Integer) o;
87  				} else {
88  					throw new NotImplementedException("o.class: " + o.getClass().getName());
89  				}
90  			}
91  
92  			@Override
93  			public long asLong() {
94  				if (o instanceof Long) {
95  					return (Long) o;
96  				} else {
97  					throw new NotImplementedException("o.class: " + o.getClass().getName());
98  				}
99  			}
100 
101 			@Override
102 			public boolean asBoolean() {
103 				throw new NotImplementedException();
104 			}
105 
106 			@Override
107 			public Date asDate() {
108 				throw new NotImplementedException();
109 			}
110 
111 			@Override
112 			public boolean isBoolean() {
113 				throw new NotImplementedException();
114 			}
115 
116 			@Override
117 			public boolean isString() {
118 				throw new NotImplementedException();
119 			}
120 
121 			@Override
122 			public boolean isInt() {
123 				throw new NotImplementedException();
124 			}
125 
126 			@Override
127 			public boolean isArray() {
128 				throw new NotImplementedException();
129 			}
130 
131 			@Override
132 			public boolean isMap() {
133 				throw new NotImplementedException();
134 			}
135 		};
136 	}
137 }