28 May 2011

Ninite - a great way to install programs

I recently re-installed my computer (due to a disc crash). This time I tried the Ninite (http://ninite.com/) way to install software. It is a web page where you can check what programs to install. The web page has the most popular free software. When you have selected what programs to install you download the Ninite installer and run it. The installer will download and install each software automatically. I tried it and it worked great!

14 December 2010

Cast/convert Object to byte[] array for Dbunit

If you are using Dbunit you might want to verify that the data in a BLOB is correct:

IDataSet dataSet = idatabaseConnection.createDataSet();
ITable myTable = dataSet.getTable("my_table");
Object blob = myTableDbUnit.getValue(0, "myBlob")

But now what? The blob cannot be converted or casted to a byte[] in Java! Here is solution:
    private static byte[] toByteArray(Object object) {
        int length = Array.getLength(object);
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++) {
            result[i] = Array.getByte(object, i);
        }
        return result;
    }  

byte[] blob = toByteArray(myTableDbUnit.getValue(0, "myBlob"));

16 October 2010

Sainte-Laguë method or "Uddatalsmetoden"

I created a class in Java to calculate allocation of seats in a paralament with Sainte-Laguë method.

import java.util.*;

/**
 * Method for is allocating seats proportionally for representative assemblies with party list
 * voting systems. Or to calculate an even percentage number given some proportional factor
 * 
 *  In Swedish this is called "Uddatalsmetoden"
 * 
 * @author Lennart Schedin
 *
 * @param <T> The type of object base
 */
public class SainteLagueMethod<T> {
  private static class SainteLagueObject<T> {
    private T party;
    private double votes;
    private int mandates;
    SainteLagueObject(T party, double votes) {
      this.party = party;
      this.votes = votes;
      this.mandates = 0;
    }
   
    private double getCompareNumer() {
      return votes / (2 * mandates + 1);
    }
  }
 
  public Map<T, Integer> calculateMandate(Map<T, Integer> partiesVotes, int mandates) {
    List<SainteLagueObject<T>> objectList = new ArrayList<SainteLagueObject<T>>();
    for (T party : partiesVotes.keySet()) {
      Integer votes = partiesVotes.get(party);
      objectList.add(new SainteLagueObject<T>(party, votes));
    }

    return distributeMandates(objectList, mandates);
  }

  private Map<T, Integer> distributeMandates(List<SainteLagueObject<T>> objectList, int mandates) {
    while (mandates > 0) {
      SainteLagueObject<T> winner = findOneWinner(objectList);
      winner.mandates = winner.mandates + 1;
      mandates--;
    }
   
    Map<T, Integer> resultList = new HashMap<T, Integer>();
    for (SainteLagueObject<T> object : objectList) {
      resultList.put(object.party, object.mandates);
    }
   
    return resultList;
  }
 
  private SainteLagueObject<T> findOneWinner(List<SainteLagueObject<T>> objectList) {
    SainteLagueObject<T> winner = null;
    for (SainteLagueObject<T> current : objectList) {
      if (winner == null) {
        winner = current;
      }
      if (current.getCompareNumer() > winner.getCompareNumer()) {
        winner = current;
      }
    }
    return winner;
  }
}

14 October 2010

How to create an rpm from a deb file

If your are forced to run an OS like Redhat or CentOS your are often in the situation that rpm does not exist for your faviourte program. But it probably be found as a deb file.

To create an rpm file from a deb file for the "makeself" program do this:
Download the deb file to some folder
fakeroot alien -r makeself_2.1.5-1_all.deb

13 June 2010

My slave name server at ZoneEdit.com are down

It looks like the servers ns2.zoneedit.com and ns17.zoneedit.com are down. This mean you cannot access my blog (unless using a Google search).

I have my master DNS at my own computer and uses Zonedit.com for slave name servers. Zoneedit assignes each domain I have with two name servers. Unfortunately both of them are down for "lesc.se".

08 June 2010

Protocol decoder for Internet based protocol

The ASN.1 language is a good language for specifying protocols. It is widely used by the telecommunications industry. However it is seldom good for "Internet based" protocols. For example IP/TCP/UDP/PPP.

For these kinds of protocols you have to have some other approach. One approach is to write your own language specification (grammar) and a compiler for this. From my own experience this is a risky path because you have to think really carefully how you design the grammar and compiler.

Does anyone know of any open or free implementations for a grammar and compiler that is suitable for Internet based protocols?

04 June 2010

Software update for X10 mini

Today I ran a Software update on my X10 mini

My previous version: 1.0.A.1.36
My current version: 1.0.A.1.38

I haven't noticed any difference yet.