4 September 2007

I could disable Macker-check

Do you know Macker by Paul Cantrell? It's a build-time architectural rule checking utility for Java developers. Quite old, but we have been using it for years and it still works great for us.

As I am the code cop here, I tend to torture colleagues with forbidden references ;-) So one made this little modification to a comic from xkcd by Randall Munroe. I came across it recently while organising some project folders... So here it is:

I could disable Macker-check instead
How bad can it be?

18 August 2007

Java Unicode Constants

Java has a strong Unicode support since always. That's nice and is supposed to save us some headache with encodings and code pages as well as allowing us to write real i18n applications (and using fancy symbols). So let's imagine you are working on your revolutionary new application which does some symbolic computations and you need to display an arrow. Maybe you know that it's just '\u2192' or you found it in the tables of the Unicode Database. Rosetta Stone However, by putting it into your code you will introduce a 'magic' character code. Magic numbers are a coding flaw and should not occur in your code. They need to be defined in some place with some reasonable name. So you end up defining all kind of Unicode letters and symbols you need.

Instead you might want to use these Java UniCode Constants (UCC). Using a small Ruby script these constants were derived directly from the Unicode Database textual representation. For every character there is a constant with its official name and corresponding char or int value. All characters of the Unicode version 4.2.0 up to \u1FFFF are covered except CJK Ideographs. For each Unicode block, e.g. Basic Latin (\u0000..\u007F) or Aegean Numbers (\u10100..\u1013F), there is a separate interface with the block's name defining all code-points defined in this block. First you need to import the blocks, e.g. import unicode.AegeanNumbers. Then you can use the constants in your code like here:
Character.charCount(BasicLatin.DIGIT_NINE)) // 1
Character.getNumericValue(BasicLatin.DIGIT_NINE)) // 9
Character.charCount(NumberForms.ROMAN_NUMERAL_FIVE_HUNDRED)) // 1
Character.getNumericValue(NumberForms.ROMAN_NUMERAL_FIVE_HUNDRED)) // 500
Character.charCount(AegeanNumbers.NUMBER_EIGHT)) // 2
Character.getNumericValue(AegeanNumbers.NUMBER_EIGHT)) // 8
(And yes, I know, interfaces are a poor place for constants. They should only be used to model a behaviour of a class. See the AvoidConstantsInterface rule. But I was young and needed the money... ;-)

Download and Installation
Download UCC 1.00 (330 KB), together with source. Extract the ucc-*.zip and add ucc.jar to your classpath. UCC is JDK 1.1 compliant and does not depend on any other libraries. To use characters beyond \u10000, called code-points, you need Java 1.5 or newer. UCC is Open Source under the GPL license.

22 May 2007

Java Astro Library

Astrology for Dogs Ten years ago I had a little interest in astrology. I studied some books and had the idea for an astro-chart real-time watch, some kind of watch that shows the wheel chart for the actual time just like a simple wristwatch. After releasing my Zodiac Watch applet in 2000 I got a lot of requests for its source. So I created AstroLib as branch from Zodiac Watch applet's source in 2003. I removed all painting and user interface routines to create a library.

This is AstroLib
... a free Java library for astro charting calculations. There are house system and planet position calculation methods together with date/time and location conversion routines. For a given time the positions of the planets in the zodiac are calculated. There is no code for graphical representations or drawing a chart. The library contains just methods for calculating the data needed to build a chart. Some methods were simply ported from Walter 'Cruiser1' Pullen's Astrolog 5.40 (which is written in C) and converted to Java. Part of the code is commented in German, but variables and methods are all named in English.

The library is used as shown in the Demo class' main method:
   // get a horoscop instance
TextHoroscop horoscop = new TextHoroscop();
// set your desired planet position calculation algorithm
horoscop.setPlanet(new PlanetAA0());
// set your desired house system calculation algorithm
// may be anything from the at.kugel.zodiac.house package.
horoscop.setHouse(new HousePlacidus());
// set your user data time value
horoscop.setTime(1, 12, 1953, 20);
// set your user data location value
horoscop.setLocationDegree(-16 - 17.0/60, 48 + 4.0/60);
// calculate the values
horoscop.calcValues();
// do something with the data, e.g. output raw data
System.out.println(horoscop.toString());
In case you want to add or change something, there are the following packages:
  • The main package contains the TextHoroscop class, which uses the house system and planet calculation classes to calculate the astro-chart data.
  • util contains some independent calculation utility functions and classes.
  • planet contains the different planet position calculation algorithms. This package is only dependent on the util package. All planet position algorithms are subclasses of PlanetBasic which itself implements PlanetInt. New and more accurate planet position algorithms must implement PlanetInt and may wish to extend PlanetBasic to inherit common functionality, but are not forced to do so. There is only PlanetAA0 available at the moment, which is not very accurate.
  • house contains the different house systems. All house system calculation algorithms are subclasses of HouseBasic which itself implements HouseInt.
Download and Installation
Download AstroLib 1.13 (70 KB), together with source. Extract the astrolib-*.zip and add astrolib.jar to your classpath. AstroLib is JDK 1.1 compliant and does not depend on any other libraries. AstroLib is Open Source under the GPL license.

The library is not actively developed any more, in fact it never was. The last changes I made were adding the Demo class in 2004 and fixing a bug using time zones in 2005 (thanks to Rastislav Szabo for reporting it). Today I reformatted the code and fixed some warnings. That's it folks.

FAQ
Q: Are there any chart drawing functions as in Zodiac Watch? A: Unfortunately the drawing routines are tightly coupled to the applet code and the browser user interface. I was able to extract the calculation routines and put them into a separate package, but this is not possible for the drawing code itself.

Q: The co-ordinates of the planets are within 1 minute difference from other available chart generation programs. But for the co-ordinates of the moon there is a 10 to 20 degree difference and the positions of the houses are absurd. What is wrong? A: Using a wrong time-zone results in a wrong local time. This affects the moon and the houses, but not the slower moving objects. Also make sure to enter negative longitude in the east zone and negative latitude below equator.

Q: Is there any code for computing the nodes of the moon? A: No, AstroLib can't do that.

Q: What kind of zodiac is calculated: Actual constellation divisions or the classic 30 degree each division? A: Traditional 30 degree per sign.

My First Astrology Computer

Update 24 April 2009

Jastrolog - Astrolog Port

Christian just convinced me to start a new project on SourceForge: Jastrolog, an Astrolog Port. Astrolog is a free astrology software program since 1991. Astrolog can create horoscopes, natal charts, and calculate current planetary positions in sidereal, traditional, and heliocentric formats. We believe it to be a bit of a cultural legacy. It deserves to live into the 21st century. As long as it is in C/C++ it has no chance. We might use AstroLib as a starting in porting the whole Astrolog and/or Astrolog32 to Java.