Saturday, December 27, 2008

Switching Dialog

Thanks for the answer, i think that then i have to do it for myself, for now i have one dialog similar to that, only that whith vertical scroll.

package es.telefonica.ui.menus;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.Dialog;
import es.telefonica.cmcore.ComunicadorCore;
import es.telefonica.common.MensajesLog;
import es.telefonica.countryinfo.CountryResource;
import es.telefonica.icons.IconosResource;
import es.telefonica.ui.componentes.BotonContactos;
import es.telefonica.ui.componentes.ImagenException;

/**
* Dialogo que sirve para seleccionar los posibles estados de presencia
* Ejemplo:Ausente, Conectado, etc..-
* @author jose
*
*/
public class DialogoEstados extends Dialog
{

private static ResourceBundle countryResource = ResourceBundle.getBundle(
CountryResource.BUNDLE_ID, CountryResource.BUNDLE_NAME);

private static ResourceBundle iconResource = ResourceBundle.getBundle(
IconosResource.BUNDLE_ID, IconosResource.BUNDLE_NAME);


private BotonContactos desconectado;
private BotonContactos conectado;
private BotonContactos ocupado;
private BotonContactos vuelvoEnseguida;
private BotonContactos ausente;
private BotonContactos alTelefono;

/**
* El constructor ya crea un dialogo con los botones y el titulo correctos
*/
public DialogoEstados()
{
super(countryResource.getString(CountryResource.Title_Status_Dialog),null,null,0,null,Manager.VERTICAL_SCROLL);
try
{
desconectado=new BotonContactos();
desconectado.setImagenEstado(ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Disconnected)));
// TODO Auto-generated catch block
desconectado.setLabelNombre(countryResource.getString(CountryResource.Disconnected));

this.add(desconectado);

conectado=new BotonContactos();
conectado.setImagenEstado(ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Connected)));
conectado.setLabelNombre(countryResource.getString(CountryResource.Connected));

this.add(conectado);

ocupado=new BotonContactos();
ocupado.setImagenEstado(ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Busy)));
ocupado.setLabelNombre(countryResource.getString(CountryResource.Busy));

this.add(ocupado);

vuelvoEnseguida=new BotonContactos();
vuelvoEnseguida.setImagenEstado(ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Back_Right_Away)));
vuelvoEnseguida.setLabelNombre(countryResource.getString(CountryResource.BackRightAway));

this.add(vuelvoEnseguida);

ausente=new BotonContactos();
ausente.setImagenEstado(ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Absent)));
ausente.setLabelNombre(countryResource.getString(CountryResource.Absent));

this.add(ausente);

alTelefono=new BotonContactos();
alTelefono.setImagenEstado(ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_To_The_Phone)));
alTelefono.setLabelNombre(countryResource.getString(CountryResource.ToThePhone));

this.add(alTelefono);
}
catch (ImagenException e)
{
e.printStackTrace();
}

}

/**
* Metodo que se ejecuta cuando se pulsa la rueda
*/
protected boolean navigationClick(int status, int time)
{
onSeleccion();
return super.navigationUnclick(status, time);
}

/**
* MEtodo que se ejecuta cuando se pulsa una tecla, en este caso no interesa solo el Enter y el Escape
*/
protected boolean keyChar(char key, int status, int time)
{
// TODO Auto-generated method stub
if(key==Keypad.KEY_ENTER)
{
onSeleccion();
}
else if(key==Keypad.KEY_ESCAPE)
{
this.close();
}
return super.keyChar(key, status, time);
}

/**
* Este metodo es el que extrae el boton que actualmente esta seleccionado, y ejecuta el cambio de estado
*/
private void onSeleccion()
{
Field panelVertical = this.getFieldWithFocus();
if(panelVertical instanceof Manager)
{
Manager manager=(Manager)panelVertical;
Field campoSeleccionado = manager.getFieldWithFocus();
campoSeleccionado.getBorderBottom();

EncodedImage imagenEstado=null;
if(campoSeleccionado instanceof BotonContactos)
{
try
{
BotonContactos boton=(BotonContactos)campoSeleccionado;
String id = boton.getLabelNombre();
if(id.equals(countryResource.getString(CountryResource.Disconnected)))
{
imagenEstado=ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Disconnected));
MensajesLog.debug(DialogoEstados.class, "Seleccionado: "+countryResource.getString(CountryResource.Disconnected));
}
else if(id.equals(countryResource.getString(CountryResource.Connected)))
{
imagenEstado=ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Connected));
MensajesLog.debug(DialogoEstados.class, "Seleccionado: "+countryResource.getString(CountryResource.Connected));
}
else if(id.equals(countryResource.getString(CountryResource.Busy)))
{
imagenEstado=ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Busy));
MensajesLog.debug(DialogoEstados.class, "Seleccionado: "+countryResource.getString(CountryResource.Busy));
}
else if(id.equals(countryResource.getString(CountryResource.BackRightAway)))
{
imagenEstado=ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Back_Right_Away));
MensajesLog.debug(DialogoEstados.class, "Seleccionado: "+countryResource.getString(CountryResource.BackRightAway));
}
else if(id.equals(countryResource.getString(CountryResource.Absent)))
{
imagenEstado=ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_Absent));
MensajesLog.debug(DialogoEstados.class, "Seleccionado: "+countryResource.getString(CountryResource.Absent));
}
else if(id.equals(countryResource.getString(CountryResource.ToThePhone)))
{
imagenEstado=ComunicadorCore.getGestorImagenes().getImagen(iconResource.getString(IconosResource.Image_To_The_Phone));
MensajesLog.debug(DialogoEstados.class, "Seleccionado: "+countryResource.getString(CountryResource.ToThePhone));
}

//TODO cambiar el icono del usuario
if(imagenEstado!=null)
{

}
}
catch(ImagenException e)
{
e.printStackTrace();
}
finally
{
this.close();
}
}
}
}
}

thanks

Blackberry Virtual Keyboard

Well I have finally resolved this by developing my own home grown virtual keyboard. This should work until RIM fixes the bug with their virtual keyboard. In case anyone else is having the same problem, I have provide the code for it here:

public class MyVirtualKeyboard extends VerticalFieldManager
{
// 1---2---3
// 4---5---6
// 7---8---9
// .---0---x
private ButtonField[] numbers = new ButtonField[]
{
new ButtonField("1", ButtonField.CONSUME_CLICK),
new ButtonField("2", ButtonField.CONSUME_CLICK),
new ButtonField("3", ButtonField.CONSUME_CLICK),
new ButtonField("4", ButtonField.CONSUME_CLICK),
new ButtonField("5", ButtonField.CONSUME_CLICK),
new ButtonField("6", ButtonField.CONSUME_CLICK),
new ButtonField("7", ButtonField.CONSUME_CLICK),
new ButtonField("8", ButtonField.CONSUME_CLICK),
new ButtonField("9", ButtonField.CONSUME_CLICK),
new ButtonField(".", ButtonField.CONSUME_CLICK),
new ButtonField("0", ButtonField.CONSUME_CLICK),
new ButtonField("x", ButtonField.CONSUME_CLICK),
};
private ButtonField close = new ButtonField("Close",
ButtonField.CONSUME_CLICK);
private FieldChangeListener listener = new FieldChangeListener()
{
public void fieldChanged(Field f, int ctx)
{
if (ctx == FieldChangeListener.PROGRAMMATIC)
return;
String val = ((ButtonField) f).getLabel();
if (Character.isDigit(val.charAt(0)) || val.equals(".":smileywink:)
caller.setCharacter(val);
else if (val.equals("x":smileywink:)
caller.removeLastCharacter();
}
};
private Caller caller;

public MyVirtualKeyboard(Caller caller)
{
this.caller = caller;
// 4 rows of buttons will be displayed
int buttonIndex = 0;
for (int i = 0; i < 4; i++)
{
HorizontalFieldManager row = new HorizontalFieldManager();
for (int j = 0; j < 3; j++)
{
numbers[buttonIndex].setChangeListener(listener);
row.add(numbers[buttonIndex++]);
}
add(row);
}
close.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int ctx)
{
MyVirtualKeyboard.this.caller.closeMe();
}
});
add(close);
}
}

Here is the interface I created callled 'Caller':

public interface Caller
{
public void closeMe();
public void setCharacter(String character);
public void removeLastCharacter();
}

Here is how I invoke it (this code is in my MainScreen subclass, which is implementing FocusChangeListener and my call back interface Caller):

public void focusChanged(Field field, int ctx) {
currentlyEditing = null;
if (field != checkAmt && field != taxAmt)
return;
if (ctx == FocusChangeListener.FOCUS_GAINED)
{
currentlyEditing = (BasicEditField) field;
MyVirtualKeyboard keyboard = new MyVirtualKeyboard(this);
popup = new PopupScreen(keyboard);
getUiEngine().pushScreen(popup);
}
}

Here is the callback methods on the main screen class that take the input from the popup keyboard:

public void setCharacter(String character)
{
String currText = currentlyEditing.getText();
currText += character;
currentlyEditing.setText(currText);
}

public void removeLastCharacter()
{
String currText = currentlyEditing.getText();
if (currText.length() == 0)
return;
currentlyEditing.setText(currText.substring(0, currText.length() - 1));
}

public void closeMe()
{
getUiEngine().popScreen(popup);
}

Also, I have a menu item where if the user needs to display the keyboard, they can:

showKeyboard = new MenuItem("Show Keyboard", 3, 12) {
public void run()
{
MyVirtualKeyboard keyboard = new MyVirtualKeyboard(
MyScreen.this);
popup = new PopupScreen(keyboard);
getUiEngine().pushScreen(popup);
}
};

Here is what the popup virtual keyboard looks like:

virtual keyboard for floating point

Hopefully this will help someone else.

Message Edited by stevewarsa on 12-07-2008 08:02 AM

Tuesday, December 9, 2008

Redmine

Redmine

Redmine is a flexible project management web application. Written using Ruby on Rails framework, it is cross-platform and cross-database.

Redmine is open source and released under the terms of the GNU General Public License v2 (GPL).

Overview

  • Multiple projects support
  • Flexible role based access control.
  • Flexible issue tracking system
  • Gantt chart and calendar
  • News, documents & files management
  • Feeds & email notifications.
  • Per project wiki
  • Per project forums
  • Simple time tracking functionality
  • Custom fields for issues, projects and users
  • SCM integration (SVN, CVS, Git, Mercurial, Bazaar and Darcs)
  • Multiple LDAP authentication support
  • User self-registration support
  • Multilanguage support
  • Multiple databases support

Read more about Redmine features

Documentation

You can read the Redmine guide (work in progress).

Others resources:

They are using Redmine

This page lists some companies/projects using Redmine.

Support & getting help

For getting help or discussing about Redmine, you can browse the Redmine forums hosted right here in Redmine. Or, join #redmine on the freenode IRC network. Prior to March 7, 2008, the Redmine forums were hosted at Rubyforge. You can search the old forum archives as well.

Before submitting a bug report here, please read this: SubmittingBugs.

Thursday, November 20, 2008

The Power User's BlackBerry Guide: 121 CrackBerry Tips

The Power User's BlackBerry Guide: 121 CrackBerry Tips, Hacks and Resources

Article Tools
Related Articles

CrackBerry addicts, are you ready to take your BlackBerry to the next level? In this list, you'll find some of the most awesome tips, hacks, how-tos and guides for the BlackBerry, making it easy for just about anyone to become a power user.

Tips

These BlackBerry tips will help you work easier, better and faster.

  1. Delete Multiple Messages: Search for messages, then press Delete Prior to delete all of the messages in the results.
  2. Read "A Wired-In Guy Getting Set for a Big Date": Stick your wet BlackBerry, or any other doomed device, (turned off) in a bowl of uncooked rice overnight, and it will suck out the moisture.
  3. Navigate Your Calendar Quickly: First, enable Quick Entry, then press "D" for day, "W" for week, "M" for month and "A" for agenda.
  4. Separate SMS (Short Message Service) Messages and Emails: Select Separate in the SMS and Email Inboxes field under General Options.
  5. Select Text on a Page: To highlight text, press Shift, then move the wheel up or down.
  6. Delete by Date: Click a date and press Delete Prior to restore order to your mailbox while still retaining your most recent messages.
  7. Get BlackBerry Tips, Tricks and Key-Press Shortcuts: Learn important keys, message tips and more.
  8. Permanently Delete Emails: To ensure that you're permanently deleting emails, delete them from your email client rather than using the Delete Prior feature.
  9. Capitalize Letters: Press and hold letters down to make them capitals.
  10. Turn Off Messages: You can choose to turn off messages by going through your security options and modifying your firewall settings.
  11. Get Better Coverage with Your Radio: If your coverage is not doing well, try turning your radio off and on to force a network scan.
  12. Scroll Down: Use your spacebar to scroll down a page, then press Shift and the spacebar simultaneously to move back up.
  13. Get BlackBerry Calendar Tricks: This post covers popular tricks for the BlackBerry's calendar.
  14. Sync Specific Folders: Set up Folder Redirection to sync only selected email folders to your BlackBerry.
  15. Dial Letter Numbers: Call lettered numbers like 1-800-GOOG-411 by pressing Alt, hen typing the letters.
  16. Empty Your Phone-Call Log: Enable Call Logging, then delete items in your Phone Call Logs folder.
  17. Insert the "@" Symbol in an Email Message. Press the spacebar to enter an "@" symbol in a message or press it twice to insert a period.
  18. Dry Your Device with Silica Gel: If you've dropped your BlackBerry in water, dry it out with silica-gel packets, which often come in shoeboxes. This also works with silica cat litter, which is generally labeled as crystal.
  19. Select Special Characters: Press and hold a letter key, then move the thumb wheel up or down to get the appropriate character.
  20. Enable Content Protection: Store data on your device securely by enabling content protection.
  21. Stop a Page from Loading. Press the Escape button to keep Web pages from loading.
  22. Reboot Your BlackBerry: You can reboot your BlackBerry without pulling out your battery by pressing Alt, Caps and Delete simultaneously.
  23. Dry Your BlackBerry with Alcohol: Another drying option that also disinfects is 100 percent alcohol. Turn your BlackBerry off, soak it in alcohol and take it apart as much as possible. The alcohol should evaporate with exposure to air.
  24. Read the Top 10 Phone Tips from BlackBerry: This list includes simple tips for changing volume, speed-dialing and more.
  25. Use Your BlackBerry as a Flashlight: The BlackBerry makes a great flashlight for middle-of-the-night runs to the fridge or the bathroom.
  26. Delete Mail-Client Emails from Your BlackBerry: Select Purge Deleted Items under Email Reconciliation to delete emails on your mail client.

Guides

Learn how to turn your BlackBerry into a supertool with these guides.

  1. BlackBerry on Crack: 25 Tools to Supercharge Your Lifeline: This guide offers useful tools for BlackBerry business users and more.
  2. BlackBerry Software and Utilities: If you're looking for any aftermarket BlackBerry tweaks, this is the place to start.
  3. Mac OS X Tethered Modem Solution for BlackBerry: Find out how to use a BlackBerry as a modem in OS X.
  4. Voice-to-Text Voice Mail Services and Third-Party Alpha Pager Dispatch Services: Check out these services that will transcribe your BlackBerry voice mails.
  5. What Are the Methods of Receiving Email on BlackBerry?: Read this article to figure out your email options.
  6. BlackBerry for the Deaf and Hard of Hearing: This article explains why BlackBerry devices are great for those who are hearing impaired.
  7. The Agony of "BlackBerry Thumb": Learn about BlackBerry-related thumb pain and what you can do about it.
  8. Why BlackBerry?: Find out why the BlackBerry is a superior choice for some people.
  9. BlackBerry HTML Web-Browsing FAQ: Get the lowdown on HTML browsing here.
  10. Basic BlackBerry Etiquette: Here, you'll find a basic list of dos and don'ts for being a polite BlackBerry user.
  11. Buying a BlackBerry on eBay: Check out this guide to learn how to avoid getting scammed when purchasing a BlackBerry from a seller on eBay.
  12. Implementing GTD with Your BlackBerry: This guide recommends applications and more items for helping you get things done on the BlackBerry.
  13. Transfer a File Using Bluetooth Technology Between Two BlackBerry Smart Phones: This guide will show you how to transfer files easily.
  14. Gmail Receiving "Sent Items" You Send On Your BlackBerry: Learn how to turn off this syncing feature for Gmail and more.
  15. BlackBerry Chat Apps/FAQ: Learn about awesome IM (instant messaging) applications and more in this guide.
  16. Remove an IT Policy from a BlackBerry: Here, you'll find out how to rid yourself of a pesky IT policy.
  17. Easy BlackBerry Backup: Use this guide to back up your BlackBerry in just a minute.
  18. The First 10 Things You Should Do With Your New BlackBerry: Before you do anything else with your BlackBerry, do these 10 things.
  19. Appear to Be in the Office While Actually Working from Home: Fool everyone by making these tweaks using your BlackBerry and more.
  20. BlackBerry Messenger: What Is It and How Does It Work?: Get the lowdown on this BlackBerry-to-BlackBerry messaging service.
  21. Maximize the Battery Life of Your New BlackBerry: Follow this tutorial to learn how to get the most out of your battery.

How-Tos

These how-tos will guide you through step-by-step hacks and tricks for the BlackBerry.

  1. How to Use Your BlackBerry to Transfer Between Windows and Mac OS X: In this guide, you'll learn how to port tasks, notes, appointments and contacts from Mac OS X to Windows, and vice versa.
  2. How to Turn on Standby Mode Using the Mute Key: Use this method to avoid pressing keys by accident.
  3. How to Edit a Profile and Set Notifications: Here, you'll find out how to change your BlackBerry's message and email notifications.
  4. How to Restore Your BlackBerry to Default Settings: Perform a security wipe on your device with this quick tutorial.
  5. How to Free Up Memory on Your BlackBerry: Keep your BlackBerry from enabling the low-memory management feature with this tutorial.
  6. How to Get Images onto Your BlackBerry: This quick tutorial offers a few easy ways to get images on your device.
  7. How to Remove "Sent from my BlackBerry": This tutorial will walk you through the steps to changing your signature.
  8. How to Use BlackBerry as Modem For Laptop: Give your laptop full Internet access using a BlackBerry.
  9. How to Increase the Speed of the BlackBerry Browser: Speed things up with these simple tips.
  10. How to Configure Full Internet Access on BlackBerry: Learn how to get full Internet access on your BlackBerry with this tutorial.
  11. How to Install Third-Party Applications (Mac OS X): This guide will walk you through installing apps on your BlackBerry through Mac OS X.
  12. BlackBerry 101: The How-To Lecture Series: This series covers purchasing a BlackBerry, checking out the controls, third-party apps andmuch more.
  13. How to Disassemble a BlackBerry Pearl: Whether you're taking your Pearl apart for fun, hacking or just to air it out, here's how to do the job.
  14. How to Secure Your BlackBerry: Keep your data safe by following the tips in this guide.
  15. How to Clean Your BlackBerry's Trackball: Get your trackball cleaned up with this how-to.
  16. How to Stop the Blinking: Here you'll find out what various blinking lights mean and what you can do about them.
  17. Installing Third-Party Applications: This guide for Windows users walks you through the process of installing of apps.
  18. How to Get BlackBerry Internet Access without a Data Plan: Avoid data plans with this hack.
  19. How to Set up Gmail as a Free Spam Filter for Your CrackBerry: Use Gmail to keep spam out of your BlackBerry.
  20. How to Pair a Bluetooth GPS Receiver with Your BlackBerry: Check out this Bluetooth hack to pair your BlackBerry with a GPS receiver.
  21. How to Transfer Files to Your BlackBerry: This guide will teach you a few different methods of file transfers.
  22. How to Use BerryDialer to Save a Lot of Money on Voice Calls: Use this dialing hack to save some cash.
  23. Turn a Full Length DVD Movie into a Pearl Movie File: This guide shows you how to turn a DVD into an .avi file.
  24. How to Use Your BlackBerry as an Emergency Flashlight: Learn what you need to do to turn your BlackBerry into a flashlight.
  25. How to Remove and Clean the Trackball: Get your trackball squeaky clean with this guide.
  26. How to Put a Full-Blown Windows Desktop Environment on Your BlackBerry: Use your microSD card slot to get Windows on your BlackBerry.

Cheat Sheets

Make shortcuts and formats with these BlackBerry cheat sheets.

  1. Hot Keys: BlackBerry Browser: Use these hot keys when viewing Web pages.
  2. BlackBerry Cheat Sheet: This sheet is full of useful shortcuts.
  3. Compare Cellular: Compare Cellular offers guides to getting started as well as quick reference guides for a number of BlackBerry models.
  4. Supported Document Types: This list offers a glimpse at the file formats that a BlackBerry can handle.
  5. BlackBerry Shortcut Collection: This handy guide has lots of shortcuts for the BlackBerry.
  6. Tip: Helpful Keyboard Shortcuts: This cheat sheet offers lots of shortcuts for phoning, messaging and more.
  7. RIM BlackBerry Quick Reference Guide: Get this handy printable sheet for an easy, quick reference for your BlackBerry.
  8. Cheat Sheet to Share: Here, you'll find loads of shortcuts.
  9. Hot Keys: Calendar: Create appointments and view dates more quickly with these hot keys.
  10. SMS Quick Reference: Get this quick-reference guide to learn about SMS shortcuts for the BlackBerry.
  11. Free SMS Messages: Avoid messaging fees by sending carrier-specific emails.
  12. What Is My Carrier's BIS (BlackBerry Internet Service) Web Site URL?: Here, you'll find BIS URLs for providers worldwide.
  13. The BlackBerry Glossary: Find out what various acronyms stand for in this BlackBerry Glossary.
  14. BlackBerry PIN Quick-Reference Guide: Have everything you need to know about BlackBerry PINs handy with this guide.
  15. Hot Keys: View Word Document: Use these hot keys when working in Word.

Tools

Put these tools to work on your BlackBerry for loads of increased functionality.

  1. BlackBerry Operating System Downloads: Find places to download a new OS here.
  2. Berry Bloglines: Access Bloglines from your BlackBerry device using this RSS reader.
  3. Google Maps Mobile: Get traffic, listings, maps and more with Google Maps for your BlackBerry.
  4. Mobile Desktop: With this software, you can access your desktop PC from anywhere using your BlackBerry.
  5. Mobylo! MultiAlarm: Get ultramanageable alarms with this neat tool.
  6. Black & Whitelist: Keep harassing phone calls at bay using this blacklist application for the BlackBerry.
  7. TryPhone: If you're shopping for a BlackBerry, use TryPhone to find out exactly how your model will function.
  8. Naggie: This reminder application works with your BlackBerry's GPS to nag you about tasks when you get close to a location.
  9. Google Mobile: Sync up your Google Calendar and more with this service.
  10. RepliGo Professional: Using RepliGo Professional, you can view documents as if they were on your desktop computer.
  11. Empower HTML Mail Viewer: See your emails in HTML with graphics, links and more with this tool.
  12. Digby: Shop a number of online stores from your BlackBerry using Digby.
  13. Spot: Get GPS on your BlackBerry with this software.
  14. Necho Expense BlackBerry Edition: This tool makes expense reports a breeze.
  15. Secure Password Manager: With this manager, you can safely store all of your important passwords and numbers on your BlackBerry.
  16. BBTetris: Entertain yourself with this classic game for the BlackBerry (link opens download folder).
  17. ABC Amber BlackBerry Convertor: Transfer contacts, emails, logs and lots more onto your desktop computer with this convertor.
  18. iSkoot: Use this Skype client to get cheap calls on your BlackBerry.
  19. IM+ All-in-One Mobile Messenger: IM on several different accounts and applications with this neat tool.
  20. MicroStrategy Mobile: MicroStrategy Mobile automatically reformats documents so that they can be easily viewed on your BlackBerry.
  21. Mobile SSH: Get access to your server from your BlackBerry using this cool tool.
  22. Windows Live Search Mobile: Get local information, maps and searching capabilities with Windows Live Search Mobile.
  23. StockView: Track stock prices with this free viewer (link opens download folder).
  24. Translator+: Use this tool to get easy foreign translations on the go.
  25. BeamBerry: Make your BlackBerry compatible with PDFs, Word documents, text files and more with this software (link opens download folder).
  26. Beyond411: Get supercharged 411 listings using Beyond411, a service that gives you local prices, listings and GPS searches.
  27. BlackBerry Unite!: Collaborate with up to five users by sharing remote access, calendars, documents and more with BlackBerry Unite!.
  28. Backpack Mobile: Get organized on the go with Backback for the BlackBerry.
  29. BlackBerry Tools: In this suite of BlackBerry tools, you'll find a number of useful applications.

Books

Find out even more in-depth tips and hacks in these BlackBerry reference books.

  1. "BlackBerry Hacks": You'll learn how to supercharge your BlackBerry with this book.
  2. "Professional BlackBerry": Learn how to extend your BlackBerry's functionality, get maximum uptime and more in this book.
  3. "Hacking BlackBerry: Extreme Tech": This book discusses some of the many ways that you can hack your BlackBerry.
  4. "How to Do Everything with Your BlackBerry": Take your BlackBerry way beyond email with this book.

Tuesday, November 11, 2008

Pegged-To-Primary Orders

Pegged-To-Primary Orders

Description

A Pegged-to-Primary Order allows you to enter a more aggressive price than the current market price, and have the entered price stay within a fixed interval to the market, should prices deteriorate. A Buy order is pegged to the bid (instead of the best offer) and a Sell order is pegged to the offer. You can enter an offset amount that is added to a buy order price and subtracted from a sell price.

For a buy order, the order is submitted at the best bid, with the offset amount (if used) added to the best bid. If the best bid moves before the order executes, your order price is modified to match to the new best bid with the offset amount added.

For a sell order, the order is submitted at the best offer, with the offset amount (if used) subtracted from the best offer. If the best offer moves before the order executes, your order price is modified to match to the new best offer, with the offset amount subtracted.

TWS Links

For information on how to create Pegged-to-Primary orders, please refer to the TWS User's Guide.

Example
This order type is less aggressive than the pegged-to-market order.

If the market is $50-50.10 and a customer enters a pegged-to-primary buy with an offset of $.05, the order would be submitted at the NBB + offset amount, or $50.05. If the NBB moves to $50.10, your order is resubmitted at $50.15 (NBB + offset of $.05). If the NBB drops to $50.05, the buy order is resubmitted at $50.10.

"SMO (stop market order)" and "SLO (stop limit order)"

Dear all,

This is a question with regard to the way to handle the following type of orders in FIX:
"SMO (stop market order)" and "SLO (stop limit order)"
Does anybody know the usage of FIX tags for the orders???

==================
1.Stop Market Order
A market order to buy or sell a certain quantity of a certain security if a specified price (the stop price) is reached or passed. The stop price must be below (above) the current price if the order is a sell (buy).

2.Stop Limit Order
This is a limit order to buy or sell at the specified limit price if the specified stop price is reached or passed. The stop price must be below (above) the current price if the order is a sell (buy).
==================

#The direct reason is the 2nd biggest Japanese exchange market, OSE #Osaka Exchange Market)announced to adopt this type of order though...

I would be really happy if I get clues for this.

Best,
Hiromasa

************************************************************************************

Hiromasa,

In FIX 4.4:
- a stop order is indicated using the OrdType (40) = 3 (Stop)
- a stop limit order is indicated using OrdType (40) = 4 (Stop limit)

The stop price is carried in the StopPx (99) tag (required for OrdType = 3 and 4).

Good luck!

************************************************************************************

Hi K. Mahesh,

Yes, it requires three prices.
The following is an example of "SLO" what Osaka Stock Exchange calls.

0) Current market condition
Market price: JPY 11,460
Code: Nikkei 225

1) A trader orders a buy �Stop Limit Order�. On this occasion, she/he must indicate �limit price�, �trigger price� and �new price� in advance.

e.g.)
Limit price: JPY 11,420
Trigger Price: JPY 11,470
New price: JPY 11,480

2) Market price moves up to the �trigger price�. (by other participants)

3) Then, the first order is automatically changed to �new price�. (JPY 11,420 --> JPY 11,480)

Is it clear?

I probably will use custom tags for this order.

Thanks,
Hiromasa

************************************************************************************

Hi again Hiromasa,

I assume your stop orders are executable even before the stop price is reached (a kind of "Limit Stop Limit" or "Limit Stop" order that just changes price when the stop price is reached).

That third price (the new limit price) is currently not in FIX. However, there is a proposal which covers it (and a number of other issues). It is currently posted at the GTC page: http://www.fixprotocol.org/documents/2442/ReserveOrd-PegInst-OrderTriggers%20proposal.pdf

A modified version of the proposal is expected to be published any day as part of the "OMX Order Routing Requirements". Hopefully it can be added to a service pack later this year.

Regards

Rikard

************************************************************************************

************************************************************************************

************************************************************************************

************************************************************************************

************************************************************************************

************************************************************************************


Friday, October 24, 2008

Automated Forex Execution Software Directory Menu Automated Execution Software Forex Blogs Forex Brokers Central Banks Charting Software Contests Dat

Automated Forex Execution Software

Directory Menu

  • GoForex - GoForex offers third-party programming services for system design and automation of your strategy into any major broker, custom designed automated trading platforms, or complete turn-key technical solutions for setting up your own brokerage with a MetaTrader to ECN bridge. Click Here for More
    • FXTradeStream - Takes signals from emails and executes them in the MetaTrader platform.
    • HyperOrder - Takes signals from TradeStation, MetaStock, ESignal or a custom application and sends them to a broker's API including FXCM, MB Trading, MetaTrader, Interactive Brokers and more.
    • MetaTrader 4 - Run automated systems and execute them into supporting brokers
    • Neoticker - Create automated systems and execute them into MB Trading, Interactive Brokers or FXCM
    • NinjaTrader - Sends signals from TradeStation, ESignal or a custom application into Interactive Brokers, MB Trading or Gain Capital
    • Robotic Forex - Executes systems into MB Trading, CMS Forex, FXCM and more.
    • SnapDragon - Sends signals from TradeStation to Oanda's API
    • Thinking Stuff - Backtest and automate your trading system into Gain Capital and Oanda with the click of a mouse. No programming knowledge required.
    • Trade Bolt - Takes signals from TradeStation, ESignal, MetaStock or WealthBuilder and sends them to Interactive Brokers
    • TradeBullet - Sends orders from TradeStation, eSignal and custom applications into Interactive Brokers and MB Trading
    • TradeCompanion - Automates your trading systems written in TradeStation, Excel, Visual Basic or any other proprietary platform and executes them into the BGCFX trading platform.
    • TradeItself - Takes trading signals from emails and sends them to the FXCM Trading Station
    • TWS Link - Sends signals from TradeStation, ESignal, MetaTrader and more to the Interactive Brokers trading platform
  • Wednesday, September 3, 2008

    The Java IAQ: Infrequently Answered Questions by Peter Norvig Q: What is an Infrequently Answered Question? A question is infrequently answered either

    The Java IAQ:
    Infrequently Answered Questions

    by Peter Norvig


    Q: What is an Infrequently Answered Question?

    A question is infrequently answered either because few people know the answer or because it is about an obscure, subtle point (but a point that may be crucial to you). I thought I had invented the term, but it also shows up at the very informative About.com Urban Legends site. There are lots of Java FAQs around, but this is the only Java IAQ. (There are a few Infrequently Asked Questions lists, including a satirical one on C.)

    Q:The code in a finally clause will never fail to execute, right?

    Well, hardly ever. But here's an example where the finally code will not execute, regardless of the value of the boolean choice:

      try {
    if (choice) {
    while (true) ;
    } else {
    System.exit(1);
    }
    } finally {
    code.to.cleanup();
    }


    Q:Within a method m in a class C, isn't this.getClass() always C?

    No. It's possible that for some object x that is an instance of some subclass C1 of C either there is no C1.m() method, or some method on x called super.m(). In either case, this.getClass() is C1, not C within the body of C.m(). If C is final, then you're ok.

    Q: I defined an equals method, but Hashtable ignores it. Why?

    equals methods are surprisingly hard to get right. Here are the places to look first for a problem:
    1. You defined the wrong equals method. For example, you wrote:

      public class C {
      public boolean equals(C that) { return id(this) == id(that); }
      }

      But in order for table.get(c) to work you need to make the equals method take an Object as the argument, not a C:

      public class C {
      public boolean equals(Object that) {
      return (that instanceof C) && id(this) == id((C)that);
      }
      }

      Why? The code for Hashtable.get looks something like this:

      public class Hashtable {
      public Object get(Object key) {
      Object entry;
      ...
      if (entry.equals(key)) ...
      }
      }

      Now the method invoked by entry.equals(key) depends upon the actual run-time type of the object referenced by entry, and the declared, compile-time type of the variable key. So when you as a user call table.get(new C(...)), this looks in class C for the equals method with argument of type Object. If you happen to have defined an equals method with argument of type C, that's irrelevent. It ignores that method, and looks for a method with signature equals(Object), eventually finding Object.equals(Object). If you want to over-ride a method, you need to match argument types exactly. In some cases, you may want to have two methods, so that you don't pay the overhead of casting when you know you have an object of the right class:

      public class C {
      public boolean equals(Object that) {
      return (this == that)
      || ((that instanceof C) && this.equals((C)that));
      }

      public boolean equals(C that) {
      return id(this) == id(that); // Or whatever is appropriate for class C
      }
      }

    2. You didn't properly implement equals as an equality predicate: equals must be symmetric, transitive, and reflexive. Symmetric means a.equals(b) must have the same value as b.equals(a). (This is the one most people mess up.) Transitive means that if a.equals(b) and b.equals(c) then a.equals(c) must be true. Reflexive means that a.equals(a) must be true, and is the reason for the (this == that) test above (it's also often good practice to include this because of efficiency reasons: testing for == is faster than looking at all the slots of an object, and to partially break the recursion problem on objects that might have circular pointer chains).
    3. You forgot the hashCode method. Anytime you define a equals method, you should also define a hashCode method. You must make sure that two equal objects have the same hashCode, and if you want better hashtable performance, you should try to make most non-equal objects have different hashCodes. Some classes cache the hash code in a private slot of an object, so that it need be computed only once. If that is the case then you will probably save time in equals if you include a line that says if (this.hashSlot != that.hashSlot) return false.
    4. You didn't handle inheritance properly. First of all, consider if two objects of different class can be equal. Before you say "NO! Of course not!" consider a class Rectangle with width and height fields, and a Box class, which has the above two fields plus depth. Is a Box with depth == 0 equal to the equivalent Rectangle? You might want to say yes. If you are dealing with a non-final class, then it is possible that your class might be subclassed, and you will want to be a good citizen with respect to your subclass. In particular, you will want to allow an extender of your class C to use your C.equals method using super as follows:

      public class C2 extends C {

      int newField = 0;

      public boolean equals(Object that) {
      if (this == that) return true;
      else if (!(that instanceof C2)) return false;
      else return this.newField == ((C2)that).newField && super.equals(that);
      }

      }

      To allow this to work, you have to be careful about how you treat classes in your definition of C.equals. For example, check for that instanceof C rather than that.getClass() == C.class. See the previous IAQ question to learn why. Use this.getClass() == that.getClass() if you are sure that two objects must be of the same class to be considered equals.

    5. You didn't handle circular references properly. Consider:

      public class LinkedList {

      Object contents;
      LinkedList next = null;

      public boolean equals(Object that) {
      return (this == that)
      || ((that instanceof LinkedList) && this.equals((LinkedList)that));
      }

      public boolean equals(LinkedList that) { // Buggy!
      return Util.equals(this.contents, that.contents) &&
      Util.equals(this.next, that.next);
      }

      }

      Here I have assumed there is a Util class with:

        public static boolean equals(Object x, Object y) {
      return (x == y) || (x != null && x.equals(y));
      }

      I wish this method were in Object; without it you always have to throw in tests against null. Anyway, the LinkedList.equals method will never return if asked to compare two LinkedLists with circular references in them (a pointer from one element of the linked list back to another element). See the description of the Common Lisp function list-length for an explanation of how to handle this problem in linear time with only two words of extra storge. (I don't give the answer here in case you want to try to figure it out for yourself first.)


    Q: I tried to forward a method to super, but it occasionally doesn't work. Why?

    This is the code in question, simplified for this example:

    /** A version of Hashtable that lets you do
    * table.put("dog", "canine");, and then have
    * table.get("dogs") return "canine". **/

    public class HashtableWithPlurals extends Hashtable {

    /** Make the table map both key and key + "s" to value. **/
    public Object put(Object key, Object value) {
    super.put(key + "s", value);
    return super.put(key, value);
    }
    }

    You need to be careful when passing to super that you fully understand what the super method does. In this case, the contract for Hashtable.put is that it will record a mapping between the key and the value in the table. However, if the hashtable gets too full, then Hashtable.put will allocate a larger array for the table, copy all the old objects over, and then recursively re-call table.put(key, value). Now, because Java resolves methods based on the runtime type of the target, in our example this recursive call within the code for Hashtable will go to HashtableWithPlurals.put(key, value), and the net result is that occasionally (when the size of the table overflows at just the wrong time), you will get an entry for "dogss" as well as for "dogs" and "dog". Now, does it state anywhere in the documentation for put that doing this recursive call is a possibility? No. In cases like this, it sure helps to have source code access to the JDK.


    Q: Why does my Properties object ignore the defaults when I do a get?

    You shouldn't do a get on a Properties object; you should do a getProperty instead. Many people assume that the only difference is that getProperty has a declared return type of String, while get is declared to return an Object. But actually there is a bigger difference: getProperty looks at the defaults. get is inherited from Hashtable, and it ignores the default, thereby doing exactly what is documented in the Hashtable class, but probably not what you expect. Other methods that are inherited from Hashtable (like isEmpty and toString) will also ignore defaults. Example code:

    Properties defaults = new Properties();
    defaults.put("color", "black");

    Properties props = new Properties(defaults);

    System.out.println(props.get("color") + ", " +
    props.getProperty(color));
    // This prints "null, black"

    Is this justified by the documentation? Maybe. The documentation in Hashtable talks about entries in the table, and the behavior of Properties is consistent if you assume that defauls are not entries in the table. If for some reason you thought defaults were entries (as you might be led to believe by the behavior of getProperty) then you will be confused.


    Q:Inheritance seems error-prone. How can I guard against these errors?

    The previous two questions show that a programmer neeeds to be very careful when extending a class, and sometimes just in using a class that extends another class. Problems like these two lead John Ousterhout to say "Implementation inheritance causes the same intertwining and brittleness that have been observed when goto statements are overused. As a result, OO systems often suffer from complexity and lack of reuse." (Scripting, IEEE Computer, March 1998) and Edsger Dijkstra to allegedly say "Object-oriented programming is an exceptionally bad idea which could only have originated in California." (from a collection of signature files). I don't think there's a general way to insure being safe, but there are a few things to be aware of:
    • Extending a class that you don't have source code for is always risky; the documentation may be incomplete in ways you can't foresee.
    • Calling super tends to make these unforeseen problems jump out.
    • You need to pay as much attention to the methods that you don't over-ride as the methods that you do. This is one of the big fallacies of Object-Oriented design using inheritance. It is true that inheritance lets you write less code. But you still have to think about the code you don't write.
    • You're especially looking for trouble if the subclass changes the contract of any of the methods, or of the class as a whole. It is difficult to tell when a contract is changed, since contracts are informal (there is a formal part in the type signature, but the rest appears only in comments). In the Properties example, it is not clear if a contract is being broken, because it is not clear if the defaults are to be considered "entries" in the table or not.

    Q:What are some alternatives to inheritance?

    Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

    For the HashtableWithPlurals example, delegation would give you this (note: as of JDK 1.2, Dictionary is considered obsolete; use Map instead):

    /** A version of Hashtable that lets you do
    * table.put("dog", "canine");, and then have
    * table.get("dogs") return "canine". **/

    public class HashtableWithPlurals extends Dictionary {

    Hashtable table = new Hashtable();

    /** Make the table map both key and key + "s" to value. **/
    public Object put(Object key, Object value) {
    table.put(key + "s", value);
    return table.put(key, value);
    }

    ... // Need to implement other methods as well
    }

    The Properties example, if you wanted to enforce the interpretation that default values are entries, would be better done with delegation. Why was it done with inheritance, then? Because the Java implementation team was rushed, and took the course that required writing less code.


    Q: Why are there no global variables in Java?

    Global variables are considered bad form for a variety of reasons:
    • Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables).
    • State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.
    • When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.
    For these reasons, Java decided to ban global variables.

    Q: I still miss global variables. What can I do instead?

    That depends on what you want to do. In each case, you need to decide two things: how many copies of this so-called global variable do I need? And where would be a convenient place to put it? Here are some common solutions:

    If you really want only one copy per each time a user invokes Java by starting up a Java virtual machine, then you probably want a static instance variable. For example, you have a MainWindow class in your application, and you want to count the number of windows that the user has opened, and initiate the "Really quit?" dialog when the user has closed the last one. For that, you want:
    // One variable per class (per JVM)
    public Class MainWindow {
    static int numWindows = 0;
    ...
    // when opening: MainWindow.numWindows++;
    // when closing: MainWindow.numWindows--;
    }
    In many cases, you really want a class instance variable. For example, suppose you wrote a web browser and wanted to have the history list as a global variable. In Java, it would make more sense to have the history list be an instance variable in the Browser class. Then a user could run two copies of the browser at once, in the same JVM, without having them step on each other.
    // One variable per instance
    public class Browser {
    HistoryList history = new HistoryList();
    ...
    // Make entries in this.history
    }
    Now suppose that you have completed the design and most of the implementation of your browser, and you discover that, deep down in the details of, say, the Cookie class, inside the Http class, you want to display an error message. But you don't know where to display the message. You could easily add an instance variable to the Browser class to hold the display stream or frame, but you haven't passed the current instance of the browser down into the methods in the Cookie class. You don't want to change the signatures of many methods to pass the browser along. You can't use a static variable, because there might be multiple browsers running. However, if you can guarantee that there will be only one browser running per thread (even if each browser may have multiple threads) then there is a good solution: store a table of thread-to-browser mappings as a static variable in the Browser class, and look up the right browser (and hence display) to use via the current thread:
    // One "variable" per thread
    public class Browser {
    static Hashtable browsers = new Hashtable();
    public Browser() { // Constructor
    browsers.put(Thread.currentThread(), this);
    }
    ...
    public void reportError(String message) {
    Thread t = Thread.currentThread();
    ((Browser)Browser.browsers.get(t))
    .show(message)
    }
    }
    Finally, suppose you want the value of a global variable to persist between invocations of the JVM, or to be shared among multiple JVMs in a network of machines. Then you probably should use a database which you access through JDBC, or you should serialize data and write it to a file.


    Q: Can I write sin(x) instead of Math.sin(x)?

    Short answer: Before Java 1.5, no. As of Java 1.5, yes, using static imports; you can now write import static java.lang.Math.* and then use sin(x) with impunity. But note the warning from Sun: "So when should you use static import? Very sparingly!"

    Here are some of the options that could be used before Java 1.5:

    If you only want a few methods, you can put in calls to them within your own class:
    public static double sin(double x) { return Math.sin(x); }
    public static double cos(double x) { return Math.cos(x); }
    ...
    sin(x)
    Static methods take a target (thing to the left of the dot) that is either a class name, or is an object whose value is ignored, but must be declared to be of the right class. So you could save three characters per call by doing:
    // Can't instantiate Math, so it must be null.
    Math m = null;
    ...
    m.sin(x)
    java.lang.Math is a final class, so you can't inherit from it, but if you have your own set of static methods that you would like to share among many of your own classes, then you can package them up and inherit them:
    public abstract class MyStaticMethods {
    public static double mysin(double x) { ... }
    }

    public class MyClass1 extends MyStaticMethods {
    ...
    mysin(x)
    }

    Peter van der Linden, author of Just Java, recommends against both of the last two practices in his FAQ. I agree with him that Math m = null is a bad idea in most cases, but I'm not convinced that the MyStaticMethods demonstrates "very poor OOP style to use inheritance to obtain a trivial name abbreviation (rather than to express a type hierarchy)." First of all, trivial is in the eye of the beholder; the abbreviation may be substantial. (See an example of how I used this approach to what I thought was good effect.) Second, it is rather presumptuous to say that this is very bad OOP style. You could make a case that it is bad Java style, but in languages with multiple inheritance, this idiom would be more acceptable.

    Another way of looking at it is that features of Java (and any language) necessarily involve trade-offs, and conflate many issues. I agree it is bad to use inheritance in such a way that you mislead the user into thinking that MyClass1 is inheriting behavior from MyStaticMethods, and it is bad to prohibit MyClass1 from extending whatever other class it really wants to extend. But in Java the class is also the unit of encapsulation, compilation (mostly), and name scope. The MyStaticMethod approach scores negative points on the type hierarchy front, but positive points on the name scope front. If you say that the type hierarchy view is more important, I won't argue with you. But I will argue if you think of a class as doing only one thing, rather than many things at once, and if you think of style guides as absolute rather than as trade-offs.


    Q: Is null an Object?

    Absolutely not. By that, I mean (null instanceof Object) is false. Some other things you should know about null:
    1. You can't call a method on null: x.m() is an error when x is null and m is a non-static method. (When m is a static method it is fine, because it is the class of x that matters; the value is ignored.)
    2. There is only one null, not one for each class. Thus, ((String) null == (Hashtable) null), for example.
    3. It is ok to pass null as an argument to a method, as long as the method is expecting it. Some methods do; some do not. So, for example, System.out.println(null) is ok, but string.compareTo(null) is not. For methods you write, your javadoc comments should say whether null is ok, unless it is obvious.
    4. In JDK 1.1 to 1.1.5, passing null as the literal argument to a constructor of an anonymous inner class (e.g., new SomeClass(null) { ...} caused a compiler error. It's ok to pass an expression whose value is null, or to pass a coerced null, like new SomeClass((String) null) { ...}
    5. There are at least three different meanings that null is commonly used to express:
      • Uninitialized. A variable or slot that hasn't yet been assigned its real value.
      • Non-existant/not applicable. For example, terminal nodes in a binary tree might be represented by a regular node with null child pointers.
      • Empty. For example, you might use null to represent the empty tree. Note that this is subtly different from the previous case, although some people make the mistake of confusing the two cases. The difference is whether null is an acceptable tree node, or whether it is a signal to not treat the value as a tree node. Compare the following three implementations of binary tree nodes with an in-order print method:

    // null means not applicable
    // There is no empty tree.

    class Node {
    Object data;
    Node left, right;

    void print() {
    if (left != null)
    left.print();
    System.out.println(data);
    if (right != null)
    right.print();
    }
    }
    // null means empty tree
    // Note static, non-static methods

    class Node {
    Object data;
    Node left, right;

    void static print(Node node) {
    if (node != null) node.print();
    }

    void print() {
    print(left);
    System.out.println(data);
    print(right);
    }
    }
    // Separate class for Empty
    // null is never used

    interface Node { void print(); }

    class DataNode implements Node{
    Object data;
    Node left, right;

    void print() {
    left.print();
    System.out.println(data);
    right.print();
    }
    }

    class EmptyNode implements Node {
    void print() { }
    }


    Q: How big is an Object? Why is there no sizeof?

    C has a sizeof operator, and it needs to have one, because the user has to manage calls to malloc, and because the size of primitive types (like long) is not standardized. Java doesn't need a sizeof, but it would still have been a convenient aid. Since it's not there, you can do this:

    static Runtime runtime = Runtime.getRuntime();
    ...
    long start, end;
    Object obj;
    runtime.gc();
    start = runtime.freememory();
    obj = new Object(); // Or whatever you want to look at
    end = runtime.freememory();
    System.out.println("That took " + (start-end) + "
    bytes.");

    This method is not foolproof, because a garbage collection could occur in the middle of the code you are instrumenting, throwing off the byte count. Also, if you are using a just-in-time compiler, some bytes may come from generating code.

    You might be surprised to find that an Object takes 16 bytes, or 4 words, in the Sun JDK VM. This breaks down as follows: There is a two-word header, where one word is a pointer to the object's class, and the other points to the instance variables. Even though Object has no instance variables, Java still allocates one word for the variables. Finally, there is a "handle", which is another pointer to the two-word header. Sun says that this extra level of indirection makes garbage collection simpler. (There have been high performance Lisp and Smalltalk garbage collectors that do not use the extra level for at least 15 years. I have heard but have not confirmed that the Microsoft JVM does not have the extra level of indirection.)

    An empty new String() takes 40 bytes, or 10 words: 3 words of pointer overhead, 3 words for the instance variables (the start index, end index, and character array), and 4 words for the empty char array. Creating a substring of an existing string takes "only" 6 words, because the char array is shared. Putting an Integer key and Integer value into a Hashtable takes 64 bytes (in addition to the four bytes that were pre-allocated in the Hashtable array): I'll let you work out why.


    Q: In what order is initialization code executed? What should I put where?

    Instance variable initialization code can go in three places within a class:

    In an instance variable initializer for a class (or a superclass).
    class C {
    String var = "val";

    In a constructor for a class (or a superclass).
        public C() { var = "val"; }

    In an object initializer block. This is new in Java 1.1; its just like a static initializer block but without the keyword static.
        { var = "val"; }
    }

    The order of evaluation (ignoring out of memory problems) when you say new C() is:

    1. Call a constructor for C's superclass (unless C is Object, in which case it has no superclass). It will always be the no-argument constructor, unless the programmer explicitly coded super(...) as the very first statement of the constructor.
    2. Once the super constructor has returned, execute any instance variable initializers and object initializer blocks in textual (left-to-right) order. Don't be confused by the fact that javadoc and javap use alphabetical ordering; that's not important here.
    3. Now execute the remainder of the body for the constructor. This can set instance variables or do anything else.
    In general, you have a lot of freedom to choose any of these three forms. My recommendation is to use instance variable initailizers in cases where there is a variable that takes the same value regardless of which constructor is used. Use object initializer blocks only when initialization is complex (e.g. it requires a loop) and you don't want to repeat it in multiple constructors. Use a constructor for the rest.

    Here's another example:

    Program:
    class A {
    String a1 = ABC.echo(" 1: a1");
    String a2 = ABC.echo(" 2: a2");
    public A() {ABC.echo(" 3: A()");}
    }

    class B extends A {
    String b1 = ABC.echo(" 4: b1");
    String b2;
    public B() {
    ABC.echo(" 5: B()");
    b1 = ABC.echo(" 6: b1 reset");
    a2 = ABC.echo(" 7: a2 reset");
    }
    }

    class C extends B {
    String c1;
    { c1 = ABC.echo(" 8: c1"); }
    String c2;
    String c3 = ABC.echo(" 9: c3");

    public C() {
    ABC.echo("10: C()");
    c2 = ABC.echo("11: c2");
    b2 = ABC.echo("12: b2");
    }
    }

    public class ABC {
    static String echo(String arg) {
    System.out.println(arg);
    return arg;
    }

    public static void main(String[] args) {
    new C();
    }
    }

    Output:
     1: a1
    2: a2
    3: A()
    4: b1
    5: B()
    6: b1 reset
    7: a2 reset
    8: c1
    9: c3
    10: C()
    11: c2
    12: b2


    Q: What about class initialization?

    It is important to distinguish class initialization from instance creation. An instance is created when you call a constructor with new. A class C is initialized the first time it is actively used. At that time, the initialization code for the class is run, in textual order. There are two kinds of class initialization code: static initializer blocks (static { ... }), and class variable initializers (static String var = ...).

    Active use is defined as the first time you do any one of the following:

    1. Create an instance of C by calling a constructor;
    2. Call a static method that is defined in C (not inherited);
    3. Assign or access a static variable that is declared (not inherited) in C. It does not count if the static variable is initialized with a constant expression (one involving only primitive operators (like + or ||), literals, and static final variables), because these are initialized at compile time.

    Here is an example:

    Program:
    class A {
    static String a1 = ABC.echo(" 1: a1");
    static String a2 = ABC.echo(" 2: a2");
    }

    class B extends A {
    static String b1 = ABC.echo(" 3: b1");
    static String b2;
    static {
    ABC.echo(" 4: B()");
    b1 = ABC.echo(" 5: b1 reset");
    a2 = ABC.echo(" 6: a2 reset");
    }
    }

    class C extends B {
    static String c1;
    static { c1 = ABC.echo(" 7: c1"); }
    static String c2;
    static String c3 = ABC.echo(" 8: c3");

    static {
    ABC.echo(" 9: C()");
    c2 = ABC.echo("10: c2");
    b2 = ABC.echo("11: b2");
    }
    }

    public class ABC {
    static String echo(String arg) {
    System.out.println(arg);
    return arg;
    }

    public static void main(String[] args) {
    new C();
    }
    }

    Output:
     1: a1
    2: a2
    3: b1
    4: B()
    5: b1 reset
    6: a2 reset
    7: c1
    8: c3
    9: C()
    10: c2
    11: b2


    Q: I have a class with six instance variables, each of which could be initialized or not. Should I write 64 constructors?

    Of course you don't need (26) constructors. Let's say you have a class C defined as follows:

    public class C { int a,b,c,d,e,f; }

    Here are some things you can do for constructors:

    1. Guess at what combinations of variables will likely be wanted, and provide constructors for those combinations. Pro: That's how it's usually done. Con: Difficult to guess correctly; lots of redundant code to write.

    2. Define setters that can be cascaded because they return this. That is, define a setter for each instance variable, then use them after a call to the default constructor:

      public C setA(int val) { a = val; return this; }
      ...
      new C().setA(1).setC(3).setE(5);

      Pro: This is a reasonably simple and efficient approach. A similar idea is discussed by Bjarne Stroustrop on page 156 of The Design and Evolution of C++. Con: You need to write all the little setters, they aren't JavaBean-compliant (since they return this, not void), they don't work if there are interactions between two values.

    3. Use the default constructor for an anonymous sub-class with a non-static initializer:

      new C() {{ a = 1; c = 3; e = 5; }}

      Pro: Very concise; no mess with setters. Con: The instance variables can't be private, you have the overhead of a sub-class, your object won't actually have C as its class (although it will still be an instanceof C), it only works if you have accessible instance variables, and many people, including experienced Java programmers, won't understand it. Actually, its quite simple: You are defining a new, unnamed (anonymous) subclass of C, with no new methods or variables, but with an initialization block that initializes a, c, and e. Along with defining this class, you are also making an instance. When I showed this to Guy Steele, he said "heh, heh! That's pretty cute, all right, but I'm not sure I would advocate widespread use..." As usual, Guy is right. (By the way, you can also use this to create and initialize a vector. You know how great it is to create and initialize, say, a String array with new String[] {"one", "two", "three"}. Now with inner classes you can do the same thing for a vector, where previously you thought you'd have to use assignement statements: new Vector(3) {{add("one"); add("two"); add("three")}}.)

    4. You can switch to a language that directly supports this idiom.. For example, C++ has optional arguments. So you can do this:

      class C {
      public: C(int a=1, int b=2, int c=3, int d=4, int e=5);
      }
      ...
      new C(10); // Construct an instance with defaults for b,c,d,e

      Common Lisp and Python have keyword arguments as well as optional arguments, so you can do this:

      C(a=10, c=30, e=50)            # Construct an instance; use defaults for b and d.


    Q:When should I use constructors, and when should I use other methods?

    The glib answer is to use constructors when you want a new object; that's what the keyword new is for. The infrequent answer is that constructors are often over-used, both in when they are called and in how much they have to do. Here are some points to consider
    • Modifiers: As we saw in the previous question, one can go overboard in providing too many constructors. It is usually better to minimize the number of constructors, and then provide modifier methods, that do the rest of the initialization. If the modifiers return this, then you can create a useful object in one expression; if not, you will need to use a series of statements. Modifiers are good because often the changes you want to make during construction are also changes you will want to make later, so why duplicate code between constructors and methods.
    • Factories: Often you want to create something that is an instance of some class or interface, but you either don't care exactly which subclass to create, or you want to defer that decision to runtime. For example, if you are writing a calculator applet, you might wish that you could call new Number(string), and have this return a Double if string is in floating point format, or a Long if string is in integer format. But you can't do that for two reasons: Number is an abstract class, so you can't invoke its constructor directly, and any call to a constructor must return a new instance of that class directly, not of a subclass. A method which returns objects like a constructor but that has more freedom in how the object is made (and what type it is) is called a factory. Java has no built-in support or conventions for factories, but you will want to invent conventions for using them in your code.
    • Caching and Recycling: A constructor must create a new object. But creating a new object is a fairly expensive operation. Just as in the real world, you can avoid costly garbage collection by recycling. For example, new Boolean(x) creates a new Boolean, but you should almost always use instead (x ? Boolean.TRUE : Boolean.FALSE), which recycles an existing value rather than wastefully creating a new one. Java would have been better off if it advertised a method that did just this, rather than advertising the constructor. Boolean is just one example; you should also consider recycling of other immutable classes, including Character, Integer, and perhaps many of your own classes. Below is an example of a recycling factory for Numbers. If I had my choice, I would call this Number.make, but of course I can't add methods to the Number class, so it will have to go somewhere else.

        public Number numberFactory(String str) throws NumberFormatException {
      try {
      long l = Long.parseLong(str);
      if (l >= 0 && l < cachedLongs.length) {
      int i = (int)l;
      if (cachedLongs[i] != null) return cachedLongs[i];
      else return cachedLongs[i] = new Long(str);
      } else {
      return new Long(l);
      }
      } catch (NumberFormatException e) {
      double d = Double.parseDouble(str);
      return d == 0.0 ? ZERO : d == 1.0 ? ONE : new Double(d);
      }
      }

      private Long[] cachedLongs = new Long[100];
      private Double ZERO = new Double(0.0);
      private Double ONE = new Double(1.0);

    We see that new is a useful convention, but that factories and recycling are also useful. Java chose to support only new because it is the simplest possibility, and the Java philosophy is to keep the language itself as simple as possible. But that doesn't mean your class libraries need to stick to the lowest denominator. (And it shouldn't have meant that the built-in libraries stuck to it, but alas, they did.)

    Q: Will I get killed by the overhead of object creation and GC?

    Suppose the application has to do with manipulating lots of 3D geometric points. The obvious Java way to do it is to have a class Point with doubles for x,y,z coordinates. But allocating and garbage collecting lots of points can indeed cause a performance problem. You can help by managing your own storage in a resource pool. Instead of allocating each point when you need it, you can allocate a large array of Points at the start of the program. The array (wrapped in a class) acts as a factory for Points, but it is a socially-conscious recycling factory. The method call pool.point(x,y,z) takes the first unused Point in the array, sets its 3 fields to the specified values, and marks it as used. Now you as a programmer are responsible for returning Points to the pool once they are no longer needed. There are several ways to do this. The simplest is when you know you will be allocating Points in blocks that are used for a while, and then discarded. Then you do int pos = pool.mark() to mark the current position of the pool. When you are done with the section of code, you call pool.restore(pos) to set the mark back to the position. If there are a few Points that you would like to keep, just allocate them from a different pool. The resource pool saves you from garbage collection costs (if you have a good model of when your objects will be freed) but you still have the initial object creation costs. You can get around that by going "back to Fortran": using arrays of x,y and z coordinates rather than individual point objects. You have a class of Points but no class for an individual point. Consider this resource pool class:

     
    public class PointPool {
    /** Allocate a pool of n Points. **/
    public PointPool(int n) {
    x = new double[n];
    y = new double[n];
    z = new double[n];
    next = 0;
    }
    public double x[], y[], z[];

    /** Initialize the next point, represented as in integer index. **/
    int point(double x1, double y1, double z1) {
    x[next] = x1; y[next] = y1; z[next] = z1;
    return next++;
    }

    /** Initialize the next point, initilized to zeros. **/
    int point() { return point(0.0, 0.0, 0.0); }

    /** Initialize the next point as a copy of a point in some pool. **/
    int point(PointPool pool, int p) {
    return point(pool.x[p], pool.y[p], pool.z[p]);
    }

    public int next;
    }
    You would use this class as follows:

     
    PointPool pool = new PointPool(1000000);
    PointPool results = new PointPool(100);
    ...
    int pos = pool.next;
    doComplexCalculation(...);
    pool.next = pos;

    ...

    void doComplexCalculation(...) {
    ...
    int p1 = pool.point(x, y, z);
    int p2 = pool.point(p, q, r);
    double diff = pool.x[p1] - pool.x[p2];
    ...
    int p_final = results.point(pool,p1);
    ...
    }

    Allocating a million points took half a second for the PointPool approach, and 6 seconds for the straightforward approach that allocates a million instances of a Point class, so that's a 12-fold speedup.

    Wouldn't it be nice if you could declare p1, p2 and p_final as Point rather than int? In C or C++, you could just do typedef int Point, but Java doesn't allow that. If you're adventurous, you can set up make files to run your files through the C preprocessor before the Java compiler, and then you can do #define Point int.


    Q: I have a complex expression inside a loop. For efficiency, I'd like the computation to be done only once. But for readability, I want it to stay inside the loop where it is used. What can I do?

    Let's assume an example where match is a regular expression pattern match routine, and compile compiles a string into a finite state machine that can be used by match:

    for(;;) {
    ...
    String str = ...
    match(str, compile("a*b*c*"));
    ...
    }

    Since Java has no macros, and little control over time of execution, your choices are limited here. One possibility, although not very pretty, is to use an inner interface with a variable initializer:

    for(;;) {
    ...
    String str = ...
    interface P1 {FSA f = compile("a*b*c*);}
    match(str, P1.f);
    ...
    }

    The value for P1.f gets initialized on the first use of P1, and is not changed, since variables in interfaces are implicitly static final. If you don't like that, you can switch to a language that gives you better control. In Common Lisp, the character sequence #. means to evaluate the following expression at read (compile) time, not run time. So you could write:

    (loop
    ...
    (match str #.(compile "a*b*c*"))
    ...)


    Q: What other operations are surprisingly slow?

    Where do I begin? Here are a few that are most useful to know about. I wrote a timing utility that runs snippets of code in a loop, reporting the results in terms of thousands of iterations per second (K/sec) and microseconds per iteration (uSecs). Timing was done on a Sparc 20 with the JDK 1.1.4 JIT compiler. I note the following:
    • These were all done in 1998. Compilers have changed since then.
    • Counting down (i.e. for (int i=n; i>0; i--)) is twice as fast as counting up: my machine can count down to 144 million in a second, but up to only 72 million.
    • Calling Math.max(a,b) is 7 times slower than (a > b) ? a : b. This is the cost of a method call.
    • Arrays are 15 to 30 times faster than Vectors. Hashtables are 2/3 as fast as Vectors.
    • Using bitset.get(i) is 60 times slower than bits & 1 <<>. This is the cost of a synchronized method call, mostly. Of course, if you want more than 64 bits, you can't use my bit-twiddling example. Here's a chart of times for getting and setting elements of various data structures:

        K/sec     uSecs          Code           Operation
      ========= ======= ==================== ===========
      147,058 0.007 a = a & 0x100; get element of int bits
      314 3.180 bitset.get(3); get element of Bitset
      20,000 0.050 obj = objs[1]; get element of Array
      5,263 0.190 str.charAt(5); get element of String
      361 2.770 buf.charAt(5); get element of StringBuffer
      337 2.960 objs2.elementAt(1); get element of Vector
      241 4.140 hash.get("a"); get element of Hashtable

      336 2.970 bitset.set(3); set element of Bitset
      5,555 0.180 objs[1] = obj; set element of Array
      355 2.810 buf.setCharAt(5,' ') set element of StringBuffer
      308 3.240 objs2.setElementAt(1 set element of Vector
      237 4.210 hash.put("a", obj); set element of Hashtable

    • Java compilers are very poor at lifting constant expressions out of loops. The C/Java for loop is a bad abstraction, because it encourages re-computation of the end value in the most typical case. So for(int i=0; i is three times slower than int len = str.length(); for(int i=0; i


    Q: Can I get good advice from books on Java?

    There are a lot of Java books out there, falling into three classes:

    Bad. Most Java books are written by people who couldn't get a job as a Java programmer (since programming almost always pays more than book writing; I know because I've done both). These books are full of errors, bad advice, and bad programs. These books are dangerous to the beginner, but are easily recognized and rejected by a programmer with even a little experience in another language.

    Excellent. There are a small number of excellent Java books. I like the official specification and the books by Arnold and Gosling, Marty Hall, and Peter van der Linden. For reference I like the Java in a Nutshell series and the online references at Sun (I copy the javadoc APIs and the language specification and its amendments to my local disk and bookmark them in my browser so I'll always have fast access.)

    Iffy. In between these two extremes is a collection of sloppy writing by people who should know better, but either haven't taken the time to really understand how Java works, or are just rushing to get something published fast. One such example of half-truths is Edward Yourdon's Java and the new Internet programming paradigm from Rise and Resurrection of the American Programmer [footnote on Yourdon]. Here's what Yourdon says about how different Java is:

    • "Functions have been eliminated" It's true that there is no "function" keyword in Java. Java calls them methods (and Perl calls them subroutines, and Scheme calls them procedures, but you wouldn't say these languages have eliminated functions). One could reasonably say that there are no global functions in Java. But I think it would be more precise to say that there are functions with global extent; its just that they must be defined within a class, and are called "static method C.f" instead of "function f".
    • "Automatic coercions of data types have been eliminated" It's true that there are limits in the coercions that are made, but they are far from eliminated. You can still say (1.0 + 2) and 2 will be automatically coerced to a double. Or you can say ("one" + 2) and 2 will be coerced to a string.
    • "Pointers and pointer arithmetic have been eliminated" It's true that explicit pointer arithmetic has been eliminated (and good riddance). But pointers remain; in fact, every reference to an object is a pointer. (That's why we have NullPointerException.) It is impossible to be a competent Java programmer without understanding this. Every Java programmer needs to know that when you do:
          int[] a = {0, 1, 2};
      int[] b = a;
      b[0] = 99;
      then a[0] is 99 because a and b are pointers (or references) to the same object.
    • "Because structures are gone, and arrays and strings are represented as objects, the need for pointers has largely disappeared." This is also misleading. First of all, structures aren't gone, they're just renamed "classes". What is gone is programmer control over whether structure/class instances are allocated on the heap or on the stack. In Java all objects are allocated on the heap. That is why there is no need for syntactic markers (such as *) for pointers--if it references an object in Java, it's a pointer. Yourdan is correct in saying that having pointers to the middle of a string or array is considered good idiomatic usage in C and assembly language (and by some people in C++), but it is neither supported nor missed in other languages.
    • Yourdon also includes a number of minor typos, like saying that arrays have a length() method (instead of a length field) and that modifiable strings are represented by StringClass (instead of StringBuffer). These are annoying, but not as harmful as the more basic half-truths.