Archive for Java

Ant task generating random number

I created an ant task lately which generates random number and I thought that it might be useful for someone. The ant task will accept three parameters:

  • min
  • max
  • name of ant property to assign value to

Our class will extend org.apache.tools.ant.Task and the only thing which we have to do is really to override the execute method and to add setters for parameters. We can return value to ant environment using org.apache.tools.ant.Project which will give us a way of setting a property using its name. The code is as follows:


package your.package;

import java.util.Random;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class RandomTask extends Task {
    private String min;
    private String max;
    private String property;
    private Random random = new Random(System.currentTimeMillis());

    @Override
    public void execute() throws BuildException {
        if (min == null || min.equals(""))
            throw new BuildException("Min property not specified");

        if (max == null || max.equals(""))
            throw new BuildException("Max property not specified");

        int minInt = Integer.parseInt(min);
        int maxInt = Integer.parseInt(max);

        if (minInt > maxInt)
            throw new BuildException("Min is bigger than max");

        int randomInt = calculateRandom(minInt, maxInt);

        getProject().setNewProperty(property, String.valueOf(randomInt));
    }

    protected int calculateRandom(int minInt, int maxInt) {
        return minInt + random.nextInt(maxInt - minInt + 1);
    }

    public void setMin(String min) {
        this.min = min;
    }

    public void setMax(String max) {
        this.max = max;
    }

    public void setProperty(String property) {
        this.property = property;
    }
}

After compiling and building a jar we should copy that jat to ANT_HOME/lib and define the new task in our build.xml, lets call it “random”:

    <taskdef name="random" classname="your.package.RandomTask"></taskdef>

Let’s try if it works. We will generate a rundom integer from the range <4;10>, assign to a property and print it to console:

    <target name="testRandom">
        <random min="4" max="10" property="randomValue"></random>
        <echo>Random:${randomValue}</echo>
    </target>

Popularity: 29% [?]

Comments

GWT - first impression

I had a look at Google Web Toolkit today and just wanted to share my first impression. Me, as a person who really hates JavaScript, the idea of writing web pages in Java and later compiling this to html pages flavoured with JavaScript to get AJAX style of websites, sounds excellent. I had a quick look at API and examples and it looks pretty straitforward. So, if we want to create static site with cool AJAX interface it is easy. But what about more realistic use cases? I will have to investigate how easy would be using GWT with some MVC framework and of couse some back-end. Spring MVC, Spring, Hibernate + GWT? It would be a killer! But lets back to reality and my first bad impression from actually using GWT. It has different versions for Windows XP/2000 and Linux (GTK+ 2.2.1+). As a result we have different binaries and probably we cannot easily develop on different platforms. That’s one thing. Another is lack of integration with Ant. This bothers me even more.

But I will give it a chance. I created a project on Google Code Hosting for my sandbox for GWT.

I always wanted to created something what looks cool though*.

* the true is that I prefer server-side and even GWT cannot change this!

Popularity: 11% [?]

Comments

Groovy - write less code

I recently came across Scott Hickey’s article in IBM DeveloperWorks comparing writing code in Java and Groovy. Simply code written in Groovy has simplified syntax comparing to what we are used to. Hence is less noisy, easier to read and understand. Isn’t it great to focus on solving a problem than on typing curly brackets all the time? What I just realized is that we are boxed by our behaviour, we are get actually used to type the brackets, for loops with index, iterator. Type, type, type… And we think that’s the way software development should look like. Thank’s to good IDEs helping us to generate that noise we are not frustrated completely yet. In Java 5 we have got already some goodies like simplified loops, autoboxing, generics. That is a good change I believe. And I don’t care that generics can have bad influence on performance (see an article on TSS). Coming back to Groovy I will try to have a look how can I incorporate it to my current development behaviour. I’ve read an article about using Groovy for unit testing of java code. Sounds promising to me.

Popularity: 15% [?]

Comments

Next entries »

Close
E-mail It