Created
May 17, 2012 21:42
-
-
Save xuwei-k/2721739 to your computer and use it in GitHub Desktop.
java either monad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies ++= Seq( | |
"org.codehaus.jackson" % "jackson-mapper-asl" % "1.9.7" | |
,"org.functionaljava" % "functionaljava" % "3.1" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fj.data.Either; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.InputStream; | |
import java.net.URL; | |
import org.codehaus.jackson.JsonNode; | |
import org.codehaus.jackson.map.ObjectMapper; | |
public class MainJava{ | |
public static void main(String[] args){ | |
final String url = "http://search.twitter.com/search.json?q=scala"; | |
final Either<Exception,String> str = get(url); | |
if(str.isRight()){ | |
final Either<Exception,JsonNode> json = parse(str.right().value()); | |
if(json.isRight()){ | |
System.out.println(json.right().value()); | |
}else{ | |
json.left().value().printStackTrace(); | |
} | |
}else{ | |
str.left().value().printStackTrace(); | |
} | |
} | |
static final Either<Exception,JsonNode> parse(final String jsonString){ | |
try{ | |
return Either.right(new ObjectMapper().readValue(jsonString,JsonNode.class)); | |
}catch(Exception e){ | |
return Either.left(e); | |
} | |
} | |
static final Either<Exception,String> get(final String urlString){ | |
try{ | |
final URL url = new URL(urlString); | |
InputStream content = (InputStream)url.getContent(); | |
final BufferedReader bf = new BufferedReader(new InputStreamReader(content)); | |
String line; | |
final StringBuilder sb = new StringBuilder(); | |
while ((line = bf.readLine()) != null){ | |
sb.append(line); | |
} | |
return Either.right(sb.toString()); | |
}catch(Exception e){ | |
return Either.left(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader | |
import java.io.InputStreamReader | |
import java.io.InputStream | |
import java.net.URL | |
import org.codehaus.jackson.JsonNode | |
import org.codehaus.jackson.map.ObjectMapper | |
object MainScala{ | |
def main(args:Array[String]){ | |
val url = "http://search.twitter.com/search.json?q=scala" | |
get(url).right.flatMap(parse).fold( | |
_.printStackTrace | |
, | |
println _ | |
) | |
} | |
val parse = (jsonString:String) => | |
try{ | |
Right(new ObjectMapper().readValue(jsonString,classOf[JsonNode])) | |
}catch{ | |
case e => Left(e) | |
} | |
val get = (urlString:String) => | |
try{ | |
val url = new URL(urlString) | |
val content = url.getContent().asInstanceOf[InputStream] | |
val bf = new BufferedReader(new InputStreamReader(content)) | |
var line:String = null | |
val sb = new StringBuilder() | |
while ({line = bf.readLine();line} != null){ | |
sb.append(line) | |
} | |
Right(sb.toString) | |
}catch{ | |
case e => Left(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment