Skip to content

Commit

Permalink
Merge pull request bigdata-mx#2 from justavo/master
Browse files Browse the repository at this point in the history
Common interface for Loaders
  • Loading branch information
elmer-garduno committed Dec 20, 2011
2 parents 352b242 + 9697bfb commit c89e8be
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -62,8 +62,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/mx/bigdata/anyobject/Loader.java
@@ -0,0 +1,28 @@
/*
* Copyright 2011 BigData Mx
*
* 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.
*/
package mx.bigdata.anyobject;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.io.IOException;

public interface Loader {
AnyObject load(File file) throws IOException;
AnyObject load(InputStream in) throws IOException;
AnyObject load(Reader in) throws IOException;
AnyObject load(String in) throws IOException;
}
20 changes: 15 additions & 5 deletions src/main/java/mx/bigdata/anyobject/impl/JacksonJSONLoader.java
Expand Up @@ -24,14 +24,24 @@
import java.util.Arrays;
import java.util.Map;

import mx.bigdata.anyobject.Loader;
import org.codehaus.jackson.map.ObjectMapper;

import mx.bigdata.anyobject.MapBasedAnyObject;
import mx.bigdata.anyobject.AnyObject;

public final class JacksonJSONLoader {
public final class JacksonJSONLoader implements Loader {

public static AnyObject load(File file) throws IOException {
private static final JacksonJSONLoader instance = new JacksonJSONLoader();

public static JacksonJSONLoader getInstance() {
return instance;
}

private JacksonJSONLoader() {
}

public AnyObject load(File file) throws IOException {
InputStream in = new FileInputStream(file);
try {
return load(in);
Expand All @@ -40,19 +50,19 @@ public static AnyObject load(File file) throws IOException {
}
}

public static AnyObject load(InputStream in) throws IOException {
public AnyObject load(InputStream in) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(in, Map.class);
return new MapBasedAnyObject(map);
}

public static AnyObject load(Reader in) throws IOException {
public AnyObject load(Reader in) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(in, Map.class);
return new MapBasedAnyObject(map);
}

public static AnyObject load(String in) throws IOException {
public AnyObject load(String in) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(in, Map.class);
return new MapBasedAnyObject(map);
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/mx/bigdata/anyobject/impl/SnakeYAMLLoader.java
Expand Up @@ -24,14 +24,24 @@
import java.util.Arrays;
import java.util.Map;

import mx.bigdata.anyobject.Loader;
import org.yaml.snakeyaml.Yaml;

import mx.bigdata.anyobject.MapBasedAnyObject;
import mx.bigdata.anyobject.AnyObject;

public final class SnakeYAMLLoader {
public final class SnakeYAMLLoader implements Loader {

public static AnyObject load(File file) throws IOException {
private static final SnakeYAMLLoader instance = new SnakeYAMLLoader();

public static SnakeYAMLLoader getInstance() {
return instance;
}

private SnakeYAMLLoader() {
}

public AnyObject load(File file) throws IOException {
InputStream in = new FileInputStream(file);
try {
return load(in);
Expand All @@ -40,19 +50,19 @@ public static AnyObject load(File file) throws IOException {
}
}

public static AnyObject load(InputStream in) throws IOException {
public AnyObject load(InputStream in) throws IOException {
Yaml yaml = new Yaml();
Map<String, Object> map = (Map) yaml.load(in);
return new MapBasedAnyObject(map);
}

public static AnyObject load(Reader in) throws IOException {
public AnyObject load(Reader in) throws IOException {
Yaml yaml = new Yaml();
Map<String, Object> map = (Map) yaml.load(in);
return new MapBasedAnyObject(map);
}

public static AnyObject load(String in) throws IOException {
public AnyObject load(String in) throws IOException {
Yaml yaml = new Yaml();
Map<String, Object> map = (Map) yaml.load(in);
return new MapBasedAnyObject(map);
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class MapBasedAnyObjectTest {
@Before
public void init() throws Exception {
InputStream in = getClass().getResourceAsStream("/test.yaml");
this.yaml = (MapBasedAnyObject) SnakeYAMLLoader.load(in);
this.yaml = (MapBasedAnyObject) SnakeYAMLLoader.getInstance().load(in);
}

@Test
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class JacksonJSONLoaderTest {
@Before
public void init() throws Exception {
InputStream in = getClass().getResourceAsStream("/test.json");
this.json = JacksonJSONLoader.load(in);
this.json = JacksonJSONLoader.getInstance().load(in);
}

@Test
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class SnakeYamlLoaderTest {
@Before
public void init() throws Exception {
InputStream in = getClass().getResourceAsStream("/test.yaml");
this.yaml = SnakeYAMLLoader.load(in);
this.yaml = SnakeYAMLLoader.getInstance().load(in);
}

@Test
Expand Down

0 comments on commit c89e8be

Please sign in to comment.