tag:blogger.com,1999:blog-38726490655723777802023-11-15T06:53:51.811-08:00Vlad's Programming BlogAnonymoushttp://www.blogger.com/profile/14554738267784468811noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-3872649065572377780.post-20621246129556135132014-02-18T17:21:00.000-08:002014-02-18T17:22:35.989-08:00Map and Reduce methods in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<br /></div>
For those of you, who have been using Scala for a while and now came back to Java its hard to accept the fact that there is no <i>map</i> and <i>reduce</i> methods available in Java.
Lets say you want to write a function that takes a List, and returns the product (of multiplication) of all the Ints in the list. It will be similar to this:<br />
<pre class="prettyprint">public static int product(List<integer> seq){
int product = 0;
for(int i : seq){
product *= i;
}
return product;
}
</integer></pre>
<br />
In Scala you can write the same code in one line:
<br />
<pre class="prettyprint">val result = (1 to 10).fold(0)(_ * _)
</pre>
How about a method that counts a letters of words in a sentence?
<br />
<pre class="prettyprint">public Collection<integer> countWords(Collection<string> words){
Collection<integer> result = new ArrayList<integer>();
for(String word : words){
result.add(word.length());
}
return result;
}
</integer></integer></string></integer></pre>
<br />
Here is the same logic written in Scala:
<br />
<pre class="prettyprint">words.map(_.length)
</pre>
So is there a way to do <i>map</i> and <i>reduce</i> in Java?
<br />
<br />
Yes, that's how I solved this problem. First, you'll need two classes that represents high-order functions of one and two arguments.
<br />
<pre class="prettyprint">public interface Function1<r in=""> {
public R run(IN param);
}
public interface Function2<r in1="" in2=""> {
public R run(IN1 param1, IN2 param2);
}
</r></r></pre>
<br />
Next, lets write our <i>map</i> and <i>reduce</i> methods.
<br />
<pre class="prettyprint">public class CollectionUtils {
public static <r in=""> R fold(Collection<in> sequence, R initialValue, Function2<r in="" r=""> action) {
for (IN element : sequence) {
initialValue = action.run(initialValue, element);
}
return initialValue;
}
public static <r in=""> Collection<r> map(Collection<in> sequence, Function1<r in=""> action) {
Collection<r> result = new ArrayList<>();
for (IN element : sequence) {
result.add(action.run(element));
}
return result;
}
}
</r></r></in></r></r></r></in></r></pre>
<br />
Now lets get back to our examples. Here is how you are going to do the product and word count examples with help of those methods. First you'll need two helper functions.
<br />
<pre class="prettyprint">public class Functions {
public static Function2<integer integer=""> product() {
return new Function2<integer integer="">() {
@Override
public Integer run(Integer param1, Integer param2) {
return param1 * param2;
}
};
}
public static Function1<integer string=""> countLetters() {
return new Function1<integer string="">() {
@Override
public Integer run(String param1) {
return param1.length();
}
};
}
}
</integer></integer></integer></integer></pre>
Finally, one-liner that does product of number of letters of all words in a sentence.
<br />
<pre class="prettyprint">
CollectionUtils.fold(CollectionUtils.map(Arrays.asList("The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"), Functions.countLetters()), 1, Functions.product());
</pre>
</div>
Anonymoushttp://www.blogger.com/profile/14554738267784468811noreply@blogger.com0tag:blogger.com,1999:blog-3872649065572377780.post-22022260620711262722014-01-14T15:14:00.003-08:002014-01-14T15:25:18.377-08:00Play Start/Stop Script.<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Here is a start/stop script that I use for my Play! 2.2.x applications.<br />
<br /></div>
<pre class="prettyprint">#!/bin/bash
println() {
echo
echo $1
echo
}
get_or_else() {
if [ "x" = "x$1" ]
then echo $2
else echo $1
fi
}
check_status() {
if [ -f $PID_FILE_PATH ]
then
kill -0 $( cat $PID_FILE_PATH ) 2> /dev/null
echo $?
else
echo 1
fi
}
STARTUP_CONFIGURATION_FILE_PATH=/etc/default/play
if [ ! -f "$STARTUP_CONFIGURATION_FILE_PATH" ]
then
println "Please set STARTUP_CONFIGURATION_FILE_PATH variable in the $0"
exit 1
fi
source $STARTUP_CONFIGURATION_FILE_PATH
if [ ! -d "$BASE_DIR" ]
then
println "Please set BASE_DIR variable in the $STARTUP_CONFIGURATION_FILE_PATH"
exit 1
fi
OPTS="-server"
START_DELAY=3
STOP_DELAY=3
CMD=$BASE_DIR/bin/myapp
PORT=$(get_or_else "$PORT" '9000' )
LISTEN_IP=$(get_or_else "$LISTEN_IP" '0.0.0.0' )
PID_FILE_PATH=$(get_or_else "$PID_FILE_PATH" "$BASE_DIR/play.pid" )
STDOUT_LOG=$(get_or_else "$STDOUT_LOG" '/dev/null' )
HEAP_START_SIZE=$(get_or_else "$HEAP_START_SIZE" '128M' )
HEAP_MAX_SIZE=$(get_or_else "$HEAP_MAX_SIZE" '1024M' )
OPTS="$JAVA_OPTS -Dpidfile.path=$PID_FILE_PATH -J-Xms$HEAP_START_SIZE -J-Xmx$HEAP_MAX_SIZE"
if [ "x" != "x$CONFIGURATION_FILE_PATH" ]
then
OPTS="$OPTS -Dconfig.file=$CONFIGURATION_FILE_PATH"
fi
if [ "x" != "x$LOG_CONFIGURATION_FILE_PATH" ]
then
OPTS="$OPTS -Dlogger.file=$LOG_CONFIGURATION_FILE_PATH"
fi
COMMAND="$CMD $OPTS -Dhttp.address=$LISTEN_IP -Dhttp.port=$PORT"
status() {
if [ 0 -eq $(check_status) ]
then
println "Play! is running: [$( cat $PID_FILE_PATH )]."
else
println "Play! is not running."
fi
}
start() {
if [ 0 -eq $(check_status) ]
then
println "Play! is running. [$( cat $PID_FILE_PATH )]."
else
rm $STDOUT_LOG >/dev/null 2>&1
nohup $COMMAND >>$STDOUT_LOG 2>&1 &
sleep $START_DELAY
if [ 0 -eq $(check_status) ]
then
println "Started."
else
rm $PID_FILE_PATH >/dev/null 2>&1
println "Could not start Play!, please see [$STDOUT_LOG] for more details."
fi
fi
}
stop() {
if [ 0 -eq $(check_status) ]
then
if kill $( cat $PID_FILE_PATH )
then
sleep $STOP_DELAY
if [ 0 -lt $(check_status) ]
then
println "Stopped."
else
println "Could not stop Play! process. Please stop it manually."
fi
else
println "Could not stop process $( cat $PID )."
fi
else
println "Play! is not running."
fi
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop ;
start
;;
'status')
status
;;
*)
echo
echo "Usage: $0 { start | stop | restart | status }"
echo
exit 1
;;
esac
exit 0</pre>
<br />
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
To run this script just change value of the <i>CMD=$BASE_DIR/bin/myapp</i> variable in the script and create your configuration file <i>/etc/default/play. </i>Here is an example of configuration file I have.
</div>
<br />
<br />
<pre class="prettyprint">#Base directory of the play project
BASE_DIR=/opt/app/myapp
#Path to the file with process ID
#PID_FILE_PATH=$BASE_DIR/play.pid
#Path to the file, where process output will be redirected
STDOUT_LOG=$BASE_DIR/play.out
#Play! configuration file
CONFIGURATION_FILE_PATH=$BASE_DIR/../myapp.conf
#Logging configuration file
LOG_CONFIGURATION_FILE_PATH=$BASE_DIR/../logger.xml
#Play! port
PORT=9090
#Play! host
#LISTEN_IP='127.0.0.1'
#Additional JVM options
#JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
JAVA_OPTS="-DapplyEvolutions.default=true -DapplyDownEvolutions.default=true"
#Start size of JVM heap
HEAP_START_SIZE=1024M
#Maximum size of JVM heap
HEAP_MAX_SIZE=1024M
</pre>
</div>Anonymoushttp://www.blogger.com/profile/14554738267784468811noreply@blogger.com1tag:blogger.com,1999:blog-3872649065572377780.post-37523597614311683602013-01-09T09:26:00.000-08:002014-01-14T15:22:32.391-08:00How to Convert scala.io.Source to InputStream<div dir="ltr" style="text-align: left;" trbidi="on">
Usually you convert Java's <i>InputStream</i> to Scala's <i>Source</i> and this kind of conversion is not a problem for an average Scala developer. However, if you need an opposite direction you are stuck, because there is no appropriate API in Scala that you can use for this purpose.<br />
<br />
One way to convert <i>Source</i> to <i>InputStream</i> is to implement an <i>InputStream</i>, that will wrap Scala's <i>Source</i> and will transform obtained characters into a stream of raw bytes using given encoding.<br />
<br />
Here is a simple Scala class that demonstrates this idea:<br />
<br />
<pre class="prettyprint">package com.codeminders.scalaws.helpers.io
import scala.io.Source
import java.io.InputStream
import org.apache.commons.io.IOUtils
import java.nio.charset.Charset
import java.nio.CharBuffer
import java.nio.ByteBuffer
/**
* A <code>SourceInputStream</code> obtains input bytes
* from a <code>scala.io.Source</code> and encodes obtained character stream
* into a stream of raw bytes using given encoding.
*
* @constructor create a new instance of SourceInputStream.
* @param source underlying <code>scala.io.Source</code>.
* @param encoding encoding, that will be used to transform character stream into a stream of bytes.
* @see scala.io.Source
* @see java.io.InputStream
*/
class SourceInputStream(source: Source, encoding: String = "UTF-8") extends InputStream {
private val encoder = Charset.forName(encoding).newEncoder()
private var buffer = Array.ofDim[Byte](1024 * 16)
private var bufferEnd = 0
private var bufferPos = 0
/**
* Reads a byte of data from underlying <code>scala.io.Source</code>.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* file is reached.
*/
override def read: Int = {
if (availableBytes < 1) {
readNextChunk()
}
if (availableBytes < 1) return -1
else {
val result = buffer(bufferPos)
bufferPos = math.min(bufferPos + 1, bufferEnd)
result
}
}
/**
* Reads a subarray as a sequence of bytes.
* @param b the data to be written
* @param off the start offset in the data
* @param len the number of bytes that are written
*/
override def read(b: Array[Byte], off: Int, len: Int): Int = {
require(off >= 0, "argument off of the %s.read should be >= 0".format(getClass().getName()))
require(len > 0, "argument len of the %s.read should be > 0".format(getClass().getName()))
val bytesNeeded = math.min(len, b.size - off)
if (buffer.size < len * 2) resizeBuffer(len * 2)
if (availableBytes < 1) {
readNextChunk()
}
if (availableBytes < 1) -1
else {
if (availableBytes < bytesNeeded) {
val firstHalfLength = availableBytes
System.arraycopy(buffer, bufferPos, b, off, math.max(firstHalfLength, 0))
readNextChunk()
if (availableBytes < 0) firstHalfLength
else {
val secondHalfLength = math.min(availableBytes, bytesNeeded - firstHalfLength)
System.arraycopy(buffer, bufferPos, b, off + firstHalfLength, secondHalfLength)
bufferPos += secondHalfLength
math.max(firstHalfLength + secondHalfLength, -1)
}
} else {
System.arraycopy(buffer, bufferPos, b, off, bytesNeeded)
bufferPos = math.min(bufferPos + bytesNeeded, bufferEnd)
bytesNeeded
}
}
}
private def resizeBuffer(len: Int) {
val newBuffer = Array.ofDim[Byte](len * 2)
buffer.copyToArray(newBuffer)
buffer = newBuffer
}
private def availableBytes: Int = bufferEnd - bufferPos
private def readNextChunk() {
if (source.hasNext) {
val chars = source.take(buffer.length / 2).toArray
if (chars.length < 1) bufferEnd = bufferPos - 1
else {
bufferEnd = charArray2ByteArray(chars, buffer)
bufferPos = 0
}
} else {
bufferEnd = bufferPos - 1
}
}
private def charArray2ByteArray(chars: Array[Char], bytes: Array[Byte]): Int = {
val bb = ByteBuffer.wrap(bytes, 0, bytes.size)
var cr = encoder.encode(CharBuffer.wrap(chars, 0, chars.length), bb, true)
if (!cr.isUnderflow())
cr.throwException();
cr = encoder.flush(bb);
if (!cr.isUnderflow())
cr.throwException();
encoder.reset()
bb.position()
}
}
</pre>
</div>
Anonymoushttp://www.blogger.com/profile/14554738267784468811noreply@blogger.com1