Posted on Apr 2, 2011

Solution for “cannot find any android targets in this sdk” (IntelliJ IDEA)

This week I’ve been playing with the Android SDK using IntelliJ IDEA. One of the problems that I encountered when I was creating an Android project in IDEA was the error “cannot find any Android targets in this sdk.” After surfing the net for a bit, I found the solution to this problem here. To paraphrase a bit, the solution is to simply run tools/android, under android_sdk_YOUR_OS, to retrieve the targets, check the option that says “force https to be fetched using http” under settings. And finally, under “Available packages,” select the version of Android you want.

Posted on Jan 27, 2009

Reading files a la for-each loop.

This post will show you how to use the for-each loop for reading files.  This approach tried to replace the following approach


    try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
            process(str);
        }
        in.close();
    } catch (IOException e) {
    }

with this one:


    final IterableFileReader e = new IterableFileReader(new File("SomeFile.WithSomeExtension"));
    for(String eachLine: e){
        process(eachLine);
    }

The source code of my solution is illustrated herein:


public class IterableFileReader implements Iterable<String>{
    private final BufferedReader reader;
    public IterableFileReader(InputStream is, Charset encoding) throws IOException {
        reader = new BufferedReader(new InputStreamReader(is, encoding.toString()));
    }

    public IterableFileReader(File file) throws IOException {
        this(new FileInputStream(file), Charset.UTF_8);
    }

    public Iterator<String> iterator() {
        return new Iterator<String>(){
            private String line;
            public boolean hasNext() {
                try {
                    line = reader.readLine();
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
                return line != null;
            }

            public String next() {
                return line;
            }

            public void remove() {
                throw new UnsupportedOperationException("remove is not supported. sorry dude!");
            }
        };
    }
}

I hope you will find this approach useful. Ok, time is up. I gotta go, my daughter is crying. Till next time.

Posted on Apr 26, 2008

A live template for Java 5 Locks

Every time I used Java 5 new locks I found myself writing the same lock.lock(); try {…} finally {lock.unlock();} over and over again. Sometimes I’ve even coded, unintentionally, a lock.lock() instead of a lock.unlock(). Because of this and other reasons which I won’t share (i.e., I don’t like to copy & paste stuff) :) I decided to automate the addition of the Java 5 locks via IntelliJ live templates. How is this going work? Simply type lock[tab] and voila you will have a new a Java 5 synchronized block written for you


$LOCK$.lock();
try {
    //tocode
} finally {
    $LOCK$.unlock();
}

p.s.,
I almost forgot. In order to make the previous live template to work for both write and read locks, you will have to set the $LOCK$ variable as a variable of the type Lock (i.e., variableOfType(“Lock”)).