Archive for category tips
Some great startup advice
Posted by Stephen Downey in entrepreneur, Law of attraction, tips on July 26, 2009
I just came across this already very successful blog tonight and can’t get over the amount of great advice on it. (Not sure how I have not seen it before.)
I have gone to many seminars and read many books on being successful and one thing they all advice is talk to someone that has already achieved what you desire.
So if your dream is to make the transition from a Software developer to software entrepreneur then start reading this blog by Rob Walling. In another example of the law of attraction, I started to read the E-myth Revisited about a week ago and Robs first tip is from Michael Gerber.
I really like the second realization in his post:
Realization #2: Market Comes First, Marketing Second, Aesthetic Third, and Functionality a Distant Fourth
I met up with a friend that works with the Hothouse program in DIT a few weeks back and he made this exact point to me. As I come from a development background, my first instinct is to get as many cool features into an application as possible. Big mistake!!! If you have no customers, your features are wasted and chances are that your customers will be able to tell you exactly what features they want anyway. Start by identifying your market and marking sure they know, who you are and what you can offer them.
I would highly recommend adding Rob’s blog to your RSS reader. On a Irish note, James Kennedy also has a great blog for peple that would like to generate some automated income from the Internet.
Add an Icon to your website….
Posted by Stephen Downey in General, Passion Development, tips, Web Dev on June 15, 2006
I came across this handy web tool for converting image files into icons. There are also tips for adding the icon to your website. I gave it a go with my Passion Development logo and it worked great.
Reflection Tip – Dynamically calling methods
Posted by Stephen Downey in java, Reflection, tips on April 4, 2006
There are often times when you need to call a method but you do not know the name of the method until runtime. I came across this issue during my Thesis. Our problem was as follows:
How do we dynamically map between an object type and a method in the sub class of PublishableServer that processes that type?
Here is some background to the problem:
PublishableServer is an abstract class that can connects to our distributed system and receives requests for lists of PublishedData objects. A subclasses of PublishableServer must implement methods to process various PublishedData objects it receives.

If we look at the JDBCServer illustrated above. This class must implement methods to search for PublishedData objects in the database that it is connected to. Each method must map between attributes of the PublishedData object and the columns in the database. As the getPublishedList(PublishedData publishedData) method of PublishableServer is called we must map this publishedData object to the correct method in the sub class.
One option could be to declare the methods as abstract in the PublishableServer class and then use the instanceof operator to select the correct method.
public ArrayList getPublishedList(PublishedData publishedData)
{
if (publishedData instanceof Student)
{
return getPublishedList((Student) publishedData);
} else if (publishedData instanceof Course)
{
return getPublishedList((Course) publishedData);
}
return new ArrayList(0);
}public abstract ArrayList getPublishedList(Course publishedCourse);
public abstract ArrayList getPublishedList(Student publishedStudent);
The issue with using this approach is that we need to update the PublishableServer class every time a new type of PublishedData is added to the framework. All PublishableServer implementations would also have to implement all the search methods, even if they did not want to.
The solution to this issue is to use Reflection to dynamically call the methods in the sub class. To allow this, the methods in the subclass must have a known name. We prepend "getPublished" to the name of the PublishedData class that was passed to the getPublishedList() method. So all we need to do is get the name of the class that has been passed, append that to "getPublished" and we have the name of the method that implements that search. The publishedData object implements a mehtod called getClassName() that will return the name of a class without the package name. To create a method to search for Student objects we simple implement the following method in the JDBCServer class that extends PublishableServer.
public ArrayList getPublishedStudent(PublishedData publishedData)
The body of this method should implement a search for Student objects in the servers persistent store using the properties of the Student object passed to narrow the search results.
So how do we call this method once we have its name?
The getClass() method that all java objects inherit will return the runtime class of the current object. In our case when we call this method inside the PublishableServer class we will get the runtime class that extended our PublishableServer class.
The Class object contains a method called getMethod() which takes the name of the method and an array of Class objects which represents the list of parameters for the method. If the method does not exist a NoSuchMethodException is thrown. If this is thrown in our server we simple return an empty ArrayList as this server does implement that search.
If the method does exist then we call invoke() passing the current object and PublishedData parameter. This will dynamically call our method and return an Object class. We must cast this to an ArrayList and return it. The implementation of this can be seen below.
public ArrayList getPublishedList(PublishedData publishedData)
{
ArrayList result = new ArrayList(0);
String className = publishedData.getClassName();
String methodName = "getPublished" + className;
Class[] argTypes = new Class[]
{ PublishedData.class };
Object[] args = new Object[]
{ publishedData };
try
{
Method m = this.getClass().getMethod(methodName, argTypes);
result = (ArrayList) m.invoke(this, args);
} catch (NoSuchMethodException noSuchMethod)
{
logger.error("ERROR: " + serverName
+ " does not support searches of type: "
+ publishedData.getClass().getName());
logger.error("ERROR:"
+ this.getClass().getName()
+ " needs to implement the follwing method to preform search:");
logger.error("public ArrayList " + methodName
+ "(PublishedData publishedData){");
logger.error(" ArrayList result = new ArrayList();");
logger.error(" //Preform Search");
logger.error(" return result;");
logger.error("}");
} catch (IllegalAccessException illegalAccess)
{
logger.error(serverName + " Does not support searches of type"
+ publishedData.getClass().getName(), illegalAccess);
} catch (InvocationTargetException invocTarget)
{
logger.error(serverName + " Does not support searches of type"
+ publishedData.getClass().getName(), invocTarget);
} catch (Exception general)
{
logger.error(serverName + " Does not support searches of type"
+ publishedData.getClass().getName(), general);
}return result;
}
Ordering a list of Objects
Posted by Stephen Downey in Collections, java, tips on March 1, 2006
This Java tip relates to functionality that has been around for quite a while but a lot of people are still not aware of it.
There are many occasions when you may need to order a list of data objects. Luckily the Java Collections Framework provides utilites to allow objects to be easily ordered.
The Comparable interface declares one interface:
public int compareTo(Object o);
The data object that you wish to compare must implement this interface. This method should return either -1, 0, or 1.
When the two objects are compared, the compareTo() method is called, passing the object that you wish to compare it to.
If the object is greater a 1 is returned, -1 will indicated that it is less and 0 will indicate that they are equal. In the ComparablePerson class below we have implemented the compareTo method. We will persume that all getter and setter methods are created.
public class ComparablePerson implements Comparable { private int age; private String firstName = ""; private String secondName = ""; public ComparablePerson(String firstName, String secondName, int age) { this.firstName = firstName; this.secondName = secondName; this.age = age; } public int compareTo(Object o) { int result = -1; if (o instanceof ComparablePerson) { ComparablePerson compareObj = (ComparablePerson) o; // Compare by Age result = new Integer(this.getAge()).compareTo(new Integer( compareObj.getAge())); if (result == 0) { result = this.getSecondName().compareTo( compareObj.getSecondName()); if (result == 0) { result = this.getFirstName().compareTo( compareObj.getFirstName()); } } } return result; } } ....
In the compareTo method the we first check to see that the object that we wish to compare a ComparablePerson object. If it is not then we cannot compare the properties.
We first compare based on the age. There is no point writing a method to compare two int values as this is already implemented in the Integer compareTo method.
At this stage if the the objects are still equal we continue to compare based on the secondName property. We use the compareTo method of the String class to perform this check.
Finally if they are still equal we should check the firstName property.
This object can now be compared to another ComparablePerson object.
The Collections API provides methods that will sort or reverse a list of compareable objects. The CompareTest class will create a list of ComparablePerson objects and then sort the list.
public class CompareTest { public static void main(String[] args) { ComparablePerson john = new ComparablePerson("John", "Doe", 31); ComparablePerson paul = new ComparablePerson("Paul", "Doe", 31); ComparablePerson stephen = new ComparablePerson("Stephen", "Downey", 27); ArrayList peopleList = new ArrayList(); peopleList.add(john); peopleList.add(paul); peopleList.add(stephen); System.out.println(peopleList); Collections.sort(peopleList); System.out.println(peopleList); Collections.reverse(peopleList); System.out.println(peopleList); } } ...
The output is as follows:
[John Doe 31, Paul Doe 31, Stephen Downey 27]
[Stephen Downey 27, John Doe 31, Paul Doe 31]
[Paul Doe 31, John Doe 31, Stephen Downey 27]
Java List Tip
Posted by Stephen Downey in java, list, tips on July 29, 2005
As I mentioned before, every Java programmer must own Effective Java by Joshua Bloch. Here is one of the tips from the book. It discusses is "Minimizing the scope of local variables" (Item 29). Part of this tip, that might be of interest to developers here is: Iteration of List's In his book, Bloch discusses the use of For loops over While loops when iterating through the contents of a list. This will minimize the scope of variables assuming that the variables are not needed later in the method. For Example,
for (Iterator i = c.iterator(); i.hasNext(); )
{
doSomething(i.next() );
}
To see why this for loop preferable to the more obvious while loop, consider the following code fragment, which contains two while loops and one bug:
Iterator i = c.iterator(); while (i.hasNext() )
{
doSomething(i.next());
}
Iterator i2 = c2.iterator();
while (i.hasNext() )
{
//BUG! doSomethingElse(i2.next());
}
The second loop contains a cut and paste error: It initiates a new loop variable, i2, but uses the old one, i. This will compile with out error and also run with error. The second loop will always terminate immediately and look like c2 does not contain anything. If the same cut and paste were done with the for loop, the loop variable from the first loop would not be in scope in the second loop and it would cause a compile error:
for (Iterator i = c.iterator(); i.hasNext(); )
{
doSomething(i.next() );
……
}
//Compile-time error- the symbol i cannot be resolved
for (Iterator i2 = c2.iterator(); i.hasNext(); )
{
doSomething(i2.next() );
……
}
Here is another tip for faster access to the loop when using random access List implementations such as ArrayList and Vector: for (int i =0, n = list.size(); i )The use of the second variable is essential to the performance of the idion. Without it, the loop would have to call the size method oncer per iteration. This is very useful if you know that there may be a large amount of elements in the list and will save vital time when iterating through the list. One note that Bloch make on this is: "Using this idiom is acceptable when you are sure the list really does provide random access: otherwise it displays quadratic performance". Hope this helps when working with Lists or Arraylists. Stephen For more details on this tip see Effective Java by Joshua Bloch