Skip to content

Instantly share code, notes, and snippets.

@ilguzin
ilguzin / MyMapActivity.java
Created November 29, 2012 13:28
Simple way of making long tap event work in MapView. MapActivity class
public class MyMapActivity extends MapActivity
implements OnGestureListener {
...
private GestureDetector gestureDetector;
@Override
@ilguzin
ilguzin / ShowMapActivity.java
Created November 29, 2012 13:30
Simple way of making long tap event work in MapView. ShowMapActivity class
public class ShowMapActivity extends MapActivity {
...
private long touchActionDownStartTime;
private float histX;
@ilguzin
ilguzin / set_height_to_ListView_wrapped_by_ScrollView.java
Last active December 12, 2015 04:28
The snippet gives you an ability to set ListView height according to elements number in it, when the ListView is placed inside ScrollView. Stolen from SO.
class HackUtils {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
@ilguzin
ilguzin / pass_java_list_to_scala.scala
Created April 4, 2013 13:53
Calling Scala code from Java with java.util.List when Scala's List is expected
// Implicit
import scala.collection.JavaConverters._
// For method:
def doSomething(things: List[Thing]): List[Result] = { ... }
// add the same method with another signature:
def doSomething(things: java.util.List[Thing]): java.util.List[Result] =
doSomething(things.asScala.toList).asJava
@ilguzin
ilguzin / regexp_unpackaging.scala
Created April 8, 2013 18:48
regular expression vs patterns matching in Scala
scala> val s = "Me Moscow 2012"
s: String = some text 2012
scala> val PersonLocation = """(.+) (.+) (.+)""".r
PersonLocation: scala.util.matching.Regex = (.+) (.+) (.+)
scala> val PersonLocation(person, city, year) = s
person: String = Me
city: String = Moscow
year: String = 2012
Here are a few alternatives you might wish to consider:
1. Use a view bound
If it's possible to change the function that takes a List of Bs, this would be the simplest solution. Modify it to accept a List of things that can be converted to Bs. That is,
def myfun(l: List[B]) = ...
would become
def myfun[X <% B](l: List[X]) = ...
@ilguzin
ilguzin / mongo_unique_index_on_embedded_document
Last active December 16, 2015 02:59
As comes from official mongo documentation you cannot create unique index on fields from embedded documents. Here is a workaround for those using $push to append values to array of embedded documents.
Mongo shell command
> db.users.update(
{ community:'Coomunity1',
'users.login': {'$ne': 'Login1'}
},
{ $push:
{ users: {login:'Login1', pwd:'*****'} }
},
{upsert: true})
@ilguzin
ilguzin / implicits_create_and_import.scala
Created April 12, 2013 07:12
Useful way of injecting implicits in the scope. We add here a companion object with implicits for mongo database requests. Then make the object nested in the desired class by importing.
object UserDaoImpl {
object Implicits {
implicit object AccountWriter extends BSONDocumentWriter[Account] {
def write(acc: Account) = BSONDocument(
"email" -> acc.email)
}
implicit object AccountReader extends BSONDocumentReader[Account] {
@ilguzin
ilguzin / akka_custom_ser_config
Created April 25, 2013 20:46
Akka config for remoting + serialization cutomization
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
serialize-messages = on
serializers {
proto = "akka.remote.serialization.ProtobufSerializer"
}
serialization-bindings {
"scala.collection.immutable.List" = proto
@ilguzin
ilguzin / socket_idle_timeout.java
Created June 5, 2013 08:44
The way to organize read timeout (read idle timeout) for socket connection with Netty. See. http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/timeout/ReadTimeoutHandler.html
public class MyPipelineFactory implements ChannelPipelineFactory {
private final Timer timer;
private final ChannelHandler timeoutHandler;
public MyPipelineFactory(Timer timer) {
this.timer = timer;
this.timeoutHandler = new ReadTimeoutHandler(timer, 30), // timer must be shared.
}