Google AJAX Search API - Search Result Limit Increase

Tagged:  

Google has announced that it's AJAX Search API now supports up to 32 search results as well as pagination through result sets. This is a 4x increase in the number of available search results.

You can see the new code in action in their samples that are based on the default search control:

The pagination logic is built into the default search control. If you like to code your own search control, their Raw Searcher sample has a few lines of code that implements result pagination.

One interesting thing to note is that this annoucement concludes with:

For a number of complex reasons, this feature is not implemented in Blog Search or in Local Search.

So, for a number of complex reasons, this post must end.

Making the Most of Java 5.0: Enum Tricks

Tagged:  

So your organization has finally moved to Java 5.0...and you are wondering what you can do with some of its new features. In this first of a series of articles on Java 5.0, I'll show you some of the interesting ways that you can exploit the new language features to improve the way you code Java.

First off, enumerations.

Introduction to Enums

The enumeration is a way of creating self-documenting code, a big improvement over the "magic" integer or string constants which are often used to represent a defined set of values. Enumerations abound in object models, and examples are not hard to find: days of the week, colors, card suits, etc. The idea of enumerations as an actual programming language type goes back to Pascal. Many programmers, though, are likely to be more familiar with the C/C++ version of it as the enum, which is just a decorated integer:

//use optional assignment to ints to illustrate
enum suit {
   SPADES = 1,
   HEARTS = 2,
   DIAMONDS = 3,
   CLUBS = 4
};

Figure 1: C/C++ enum

Enums in Java

In Java, the introduction of enumerations started informally with Joshua Bloch's type-safe enumeration pattern. The pattern boils down to essentially this: a Java class with a private constructor that declares public static final versions of itself.

public final class Suit {
     private Suit() {}
     public static final Suit SPADES = new Suit();
     public static final Suit HEARTS = new Suit();
     public static final Suit DIAMONDS = new Suit();
     public static final Suit CLUBS = new Suit();
}

Figure 2: The type-safe enumeration pattern

The advantage of this style of enumeration over the C/C++-style enum or constants is that they are "type-safe", meaning that, for example, if you define a method

public void setSuit(Suit suit) { ... }

the caller cannot pass in a value that does not correspond to an enumeration value. With a constant or C/C++-style enum, you could pass in a random value (like -1) that would not. Java-style enumerations have the additional advantage that, since they are classes, you can "associate" other data with them. This idea will be illustrated a little later. Since these enumeration instances are all effectively singletons, they can be compared for equality using identity ("==").

Enums in Java 5.0

In Java 5.0, the enum keyword is introduced as a special type of class that always extends java.lang.Enum. These closely resemble an abbreviated version of Bloch's type-safe enumeration pattern.

public enum Suit {
     SPADES,
     HEARTS,
     DIAMONDS,
     CLUBS        //optionally can end with ";"
}

Figure 3: The Java 5.0 enum

The difference is that this version includes utility methods like name() that returns the declared variable name as a String, ordinal() that returns the position of the enum as it is declared in the class, and valueOf() which allows you to look up the enum using its declared variable name as a String. Java 5.0 enumerations also can be used in switch statements, a nice convenience. Note that the enumeration values are still static class members, though not declared as such.

So, going beyond the basics, what can you do with Java enumerations? Here are a few handy tricks...

Enums and Template Methods

Remember that the enum is basically a special class type, and can have methods and fields just like any other class. Remember also that each declared field is also an instance of the enum. So, applying the "Template Method" design pattern, one can create enumerations that are factories or command objects, but a defined set of objects. Here is a simple example of a "command" enumeration:

public enum Toy {
     DOLL() {
          @Override public void execute() { 
               System.out.println("I'm a doll."); 
          }
     },
     SOLDIER() {
          @Override public void execute() { 
               System.out.println("I'm a soldier."); 
          }
     };
     //template method
     public abstract void execute();
}

Figure 4: The enum with template method

With the use of static imports, the client code calling this enumeration would look like:

SOLDIER.execute();
DOLL.execute();
//or better...
getToy().execute();

The resulting code is clear and self-documenting. Using this pattern is a great alternative in many cases to the more common if(toy=="soldier"){...} else if(toy=="doll"){...} else{...} logic since it is easier to read, extend, and maintain.

Reverse Lookups

Often in your object model it is common to have data that is naturally "associated" with an enumeration. Since an enum is a class, it is easy to represent this associated information as class fields. Often it is desirable to "lookup" the associated enumeration using the field value. This is easy to do using a static java.util.Map. Take, for example, a Status enum that has an associated status code.

public enum Status 
{
     WAITING(0),
     READY(1),
     SKIPPED(-1),
     COMPLETED(5);

     private static final Map<Integer,Status> lookup 
          = new HashMap<Integer,Status>();

     static {
          for(Status s : EnumSet.allOf(Status.class))
               lookup.put(s.getCode(), s);
     }

     private int code;

     private Status(int code) {
          this.code = code;
     }

     public int getCode() { return code; }

     public static Status get(int code) { 
          return lookup.get(code); 
     }
}

Figure 5: The enum reverse lookup

The static get(int) method here provides the reverse lookup by simply getting the value from the Map. The static block to populate the Map uses a specialized implementation of Set, java.util.EnumSet, that "probably" (according to the javadocs) has better performance than java.util.HashSet. Java 5.0 also provides java.util.EnumMap, a specialized implementation of Map for enumerations that is more compact than java.util.HashMap.

Conclusion

The enum in Java 5.0 represents a cleaner, more self-documenting, richer, and in many cases, more efficient way of coding certain object model concepts than was possible using the older idiom of static final constants. Even if you are still using Java 1.4.x or earlier, you can still gain many of the benefits of the enum by using the type-safe enumeration pattern; this will also make the code easier to migrate once you do go to Java 5.0. Older client code that uses constants can still be adapted to use the enum by associating constant values with the enumerations themselves (as seen in the Status class, Figure 5). So, in other words, there is no reason not to convert your old static constants to enumerations. The guy who has to maintain your code will thank you.

Update: there's a second part to this article here.

PHP Ajax Chat Tutorial

Tagged:  

Jack Herrington has put together a starter tutorial for building a web-based chat application using PHP, MySQL, DHTML, Ajax, and the Prototype.js library. This by no means is a finished or polished application, but is intended to be a starter-kit for a chat application.

An interesting thing to note is that the tutorial does a good job of showing a potential pitfall of polling in Ajax applications. Polling can become a performance issue very quickly, especially if the amount of data returned by each request is ever increasing. The tutorial does provide a solution to help with performance along with all the required source code to build this starter application.

Suggestions the author has for extending this application are:

  • Track users: Put a list of the people actively engaged in the conversation alongside the chat. Doing so lets people know who is at the party and when they come and go.
  • Allow multiple conversations: Allow multiple conversations on different topics to go on simultaneously.
  • Allow emoticons: Translate character groupings such as :-) into the appropriate image of a smiley face.
  • Use URL parsing: Use regular expressions in the client-side JavaScript code to find URLs and turn them into hyperlinks.
  • Handle the Enter key: Instead of having an Add button, watch for users pressing the Enter or Return key by hooking onto the onkeydown event in the textarea.
  • Show when a user is typing: Alert the server when a user has started to type, so that other participants can see that a reply is pending. This lessens the perception that the conversation has died off to a minimum if you have slow typists.
  • Limit the size of a posted message: Another way to keep the conversation going is to keep messages small. Limit the maximum number of characters in the textarea —again by trapping onkeydown —to help speed up conversations.

Click here to check out the tutorial and download the source code.

Keep your JavaScript Small with Dojo ShrinkSafe

If you are a developer that has used the Dojo toolkit you have probably heard of ShrinkSafe. ShinkSafe is a great free open source tool that can reduce your code even if you don't use Dojo.

ShinkSafe uses a JavaScript interpreter to better compress code. Below is what the makers say about it.

What is ShrinkSafe? What makes it "safe"?

ShrinkSafe is a JavaScript "compression" system. It can typically reduce the size of your scripts by a third or more, depending on your programming style.

Many other tools also shrink JavaScript files, but ShrinkSafe is different. Instead of relying on brittle regular expressions, ShrinkSafe is based on Rhino, a JavaScript interpreter. This allows ShrinkSafe to transform the source of a file with much more confidence that the resulting script will function identically to the file you uploaded.

Best of all, ShrinkSafe will never change a public variable or API. That means that you can drop the compressed version of your JavaScript into your pages without changing the code that uses it.

ShrinkSafe does a great job of reducing the size especially when you have a lot of code. I highly recommend it.

DWR Joins DOJO Foundation

Tagged:  

DWR, a popular open source framework, which will be very useful in push down methodology. In more technical words, it is a RPC library which makes it easy to call Java methods from JavaScript and to call JavaScript functions from Java. It has number of features like call batching, marshalling of virtually any data structure between Java and JavaScript. It also has integrations with Server side technologies like Spring, Guice etc. More details about DWR is coming soon.

When Joe, Founder of DWR, shows his interest of moving DWR under DOJO foundation because the foundation will take care of legal issues, hosting etc. There are lot of debates went on the user community. Some of them preferred to go under Apache. The main concern in going through Apache is, Apache insists that everything is in the package org.domain and the domain apache.org and they generally share the similar look and feel. Where as, DOJO foundation provide support for the things that are important like CLAs and website hosting and don't get involved in things like package naming.

DOJO will not enforce DWR that Dojo toolkit will be tightly integrated with DWR. Dojo toolkit will be one of the toolkits will be supported by DWR. DOJO foundation does not enforce anything on the DWR side.

DOJO foundation answered all of the questions raised by the DWR user group about DOJO foundation.DWR is going to write a proposal to the DOJO foundation that they accept DWR. Hope it will be a good move to DWR and brings lot of visibility to it.

OpenID 2.0's Final Signatures

In the comments of my last post on OpenID I received some interesting news regarding OpenID 2.0. Below you can see an excerpt of the original comment from Macskeeball.

Macskeeball
03|Dec|2007

Actually, apparently OpenID 2.0’s final signatures will be happening today at something called the Internet Identity Conference.

First I wanted to say that this is great news and I am looking forward to it (especially as I will soon be able to use it on Digg). Plus more big news about OpenID 2.0 is that both Microsoft and Google announced their support (click here to read about it). This is great and I am happily surprised about Microsoft's support.

Also, with the final signatures happening today I thought I would post a link to the most recent OpenID 2.0 draft that I could find. So, click here to read the draft.

As soon as I find the final OpenID 2.0 draft I'll post a link to it. Waiting for it to be final is kind of like being a kid waiting for a present the day before Christmas.

JavaScript Array Remove Method

Tagged:  

John Resig just published a handy little JavaScript method to remove elements from an array.

Here's his method:

   1. // Array Remove - By John Resig (MIT Licensed)
   2. Array.prototype.remove = function(from, to) {
   3.   var rest = this.slice((to || from) + 1 || this.length);
   4.   this.length = from < 0 ? this.length + from : from;
   5.   return this.push.apply(this, rest);
   6. };

And here are some examples of its use:


   1. // Remove the second item from the array
   2. array.remove(1);
   3. // Remove the second-to-last item from the array
   4. array.remove(-2);
   5. // Remove the second and third items from the array
   6. array.remove(1,2);
   7. // Remove the last and second-to-last items from the array
   8. array.remove(-2,-1);

Here are the goals he had for the method:

  • It had to add an extra method to an array object that would allow me to remove an item by index (e.g. array.remove(1) to remove the second item).
  • It had to be able to remove items by negative index (e.g. array.remove(-1) to remove the last item in the array).
  • It had to be able to remove a group of items by index, and negative index (e.g. array.remove(0,2) to remove the first three items and array.remove(-2,-1) to remove the last two items).
  • It had to be destructive (modifying the original array).
  • It had to behave like other destructive array methods (returning the new array length - like how push and unshift work).

To get more information about this method, click here to visit John's blog.

A Preview of HTML 5

Tagged:  

In an article on A List Apart, Lachlan Hunt discusses new features in HTML 5, which is currently under development.

Here is an excerpt from the article on the new structure presented in version 5.

HTML 5 introduces a whole set of new elements that make it much easier to structure pages. Most HTML 4 pages include a variety of common structures, such as headers, footers and columns and today, it is fairly common to mark them up using div elements, giving each a descriptive id or class.

Diagram illustrates a typical two-column layout marked up using divs with id and class attributes. It contains a header, footer, and horizontal navigation bar below the header. The main content contains an article and sidebar on the right.

The use of div elements is largely because current versions of HTML 4 lack the necessary semantics for describing these parts more specifically. HTML 5 addresses this issue by introducing new elements for representing each of these different sections.

The div elements can be replaced with the new elements: header, nav, section, article, aside, and footer.

The markup for that document could look like the following:

<body>
  <header>...</header>
  <nav>...</nav>
  <article>
    <section>
      ...
    </section>
  </article>
  <aside>...</aside>
  <footer>...</footer>
</body>



Click here to read the full article

jMaki - A Java Ajax Framework

Tagged:  

jMaki a lightweight framework to build Ajax-enabled web applications using standards-based technologies.

jMaki Screencast

[Video] Take a deep dive into jMaki, a client-side framework that makes it easy to build Ajax-based web applications. In this session hosted by Ed Ort from the java.sun.com team, jMaki Project lead and Sun Ajax architect Greg Murray discusses some of the important elements of jMaki and demonstrates building a web application that incorporates jMaki.

Click here for more information, and click here to download jMaki

Ext 2.0 Final Released

If you haven't heard of Ext before, click here to learn all about it.

Here's the annoucement right from the Ext JS Blog:

The Ext team is proud to announce that the official release of Ext v2.0 is available for download. This new version of the Ext framework is the culmination of many long hours of work and dedication by the Ext Core team as well as our community of testers and supporters. Ext 2.0 is a dramatic step forward from all previous versions of Ext, providing increased performance, ease of configurations, flexibility and UI capabilities.

We’ve also made learning how to use Ext much easier with a completely revamped document center and expanded & better organized samples. All of this without a significant library size increase in this new version.

New features include:

  • Grouping & Group Summary
  • Ext 2.0 introduces highly configurable single-level column grouping capabilities as well as summary rollups at the group level.

  • Scrolling Tabs
  • The new Ext 2.0 scrolling tabs are truly amazing and provide for a much more flexible UI then traditional static-based tabs.

  • Anchor Layout
  • The team extended the FormPanel component to allow form controls (and other components) to be anchored to a specific size within a specific container.

  • Column Tree
  • The Ext tree panel can be customized to add support for columns in the nodes.

There's also a New API Documentation Center, including:

Click here to download Ext 2.0

Syndicate content