Java: Numbers only String by removing non numeric characters

With Java, deleting non numeric characters (letters, symbols etc) from a string to produce a numbers-only String is a common requirement in web applications, as application users are used to insert numeric values with non-numeric characters.

For example a phone number will be entered with (-) characters like;
650-212-5710.
A price value may be entered with (,) characters like;
12,500.00

In Java, java.lang.Character class has a method; isDigit() which can be used to identify whether a character is a digit or not. Following method can be used for extracting a numbers-only string.

public static String getOnlyNumerics(String str) {

if (str == null) {
return null;
}

StringBuffer strBuff = new StringBuffer();
char c;

for (int i = 0; i < str.length() ; i++) {
c = str.charAt(i);

if (Character.isDigit(c)) {
strBuff.append(c);
}
}
return strBuff.toString();
}

Calling above method with any String will return a numbers-only string.

How to open a .war (web archive) or .jar (java archive) files

With Java we generate .JAR files to bundle a set of resources. For J2EE web applications, we generate .WAR (Web Archive) files for deployments. Both of these are ways of archiving a set of files.

Both these .jar or .war archives are in zip format. So there are plenty of ways to open those files to read the content. One way would be to use any tool that you use to open/extract a .zip file. Next will be to use the Jar command itself.

JAR Command
JAR (command) utility in Java can be used to open/extract these files as follows. Run following command from the folder where you need the content to be extracted by providing the path to the archive.

jar xf <path-to-file>

options:
x - extract the files
f - file to extract

jar xf myWebApp.war
jar xf myProject.jar

Related : Open and read a file inside a war file of a web application with Java

Performing Simple String Search Using PHP and MySQL

Performing Simple String Search Using PHP and MySQL


Here it is, a post after so long!


We see “Search” feature on almost all websites. Some employ third-party
tools (like Google Custom Search) while others, mostly CMS based websites, have
their own Search feature. The question now is, How do Search Feature Work? Answer
is, it (almost always) uses MySQL (or database server’s capability. You
all might be knowing that CMSs always store information in databases. So if
we can search that we can very well search the whole site!


So today we’re going to employ the feature of MySQL to perform simple
searching. As an example we’ll be creating an Online Phonebook to illustrate
this.


MySQL’s Simple Search (String Matching) Query


select [coulumn1] from [table] where [column2] like ‘$var%’


(For some of the basic SQL queries refer to MySQL
Commands
)


we’ll take a simple example to illustrate the above query. Suppose we
have a table with the following rows and columns:




























id column-1 column-2
1 cat there is a cat
2 dog there is a dog
3 can there is a cold drink can
4 campus i like my college campus

Now look closely what different queries will return:


1. select * from table where column-1 like
'ca%'























id column-1 column-2
1 cat there is a cat
3 can there is a cold drink can
4 campus i like my college campus

2. select * from table where column-1 like
'cam%'













id column-1 column-2
4 campus i like my college campus

3. select * from table where column-1 like
'd%'













id column-1 column-2
2 dog there is a dog

4. select * from table where column-2 like
'cat%'













id column-1 column-2
NULL NULL NULL

5. select * from table where column-2 like
'there%'























id column-1 column-2
1 cat there is a cat
2 dog there is a dog
3 can there is a cold drink can

At this time it is worth noting that, while we can use this method to search
for strings whose initial characters (words) are known but impossible to search
for words within a string (like in query no. 4).


That’s it, all we need to perform simple search is the above mentioned
query!


Below the source code for an Online Phone Book is given, it’d store phone
numbers along with names. Its search feature will search names column, hence
you can search for phone numbers by name.



<?php

/*Script: Onilne Phone Book with Search Facility

Date: 29-July-08

Copyright 2008 Arvind Gupta

http://learning-computer-programming.blogspot.com/




You are free to modify, change, publish or do whatever with this script

as long as this note is intact. Thank You!*/




//connect to MySQL

//provide your 'username' and 'pass'

$db=new mysqli('localhost','-USER-','-PASS-');


//if 'save' button was pressed

//user wants to store phone number

if(isset($_POST['save']))

{


    
$name=trim($_POST['name']);

    
$phno=trim($_POST['phno']);




    
//if data supplied are not empty

    
if(!$name=='' || !$phno=='')

    {


        
//if this is the first time

        //and database is not craeted

        
if(!$db->select_db('one'))


            
//create the database

            
$db->query('create database one');



        
//select the databasw to work with


        
$db->select_db('one');



        
//if table is not craeted, craete it

        
if(!$db->query('select * from phno'))


            
$db->query('create table phno(id int auto_increment primary key, name varchar(50), phnum varchar(20))');




        
//ready to insert data

        
$db->query("insert into phno (name, phnum) values ('$name', '$phno')");


    }

}

//show the form

?>

<html>

<head>

<title>My Phone Book</title>


</head>



<body>

<h1>My Phone Book</h1>

<h2 style="background: #000; color: #fff;">Store New Phone Number</h2>


<form name="form2" method="get" action="">

  <p style="background: #000; color: #fff;"><b>Search:</b>

    <input name="search" type="text" id="search">


    <input name="searchb" type="submit" id="searchb" value="Search">

  </p>

</form>

<p></p>

<form name="form1" id="form1" method="post" action="">


  <table width="250" border="0" cellspacing="0" cellpadding="0">

    <tr>

      <td width="83">Name</td>

      <td width="417"><input name="name" type="text" id="name" /></td>


    </tr>

    <tr>

      <td>Ph. No.</td>

      <td><input name="phno" type="text" id="phno" value=""></td>


    </tr>

    <tr>

      <td><input name="save" type="submit" id="save" value="Save" /></td>

      <td><input type="reset" name="Submit2" value="Reset" /></td>


    </tr>

  </table>

</form>

<h2 style="background: #000; color: #fff;">Previously Stored</h2>

<p>ORDER BY: <a href="?order=new">newest first </a>| <a href="?order=old">oldest


  first</a> | <a href="?order=az">a-z</a> | <a href="?order=za">z-a</a></p>

</body>

</html>


<?php

//----DISPALY PREVIOUSLY STORED PH. NUMBERS----



//create the SQL query as per the action

//if any ordering is selected


$order=$_GET['order'];

if(
$order=='new')

    
$query="select * from phno order by id desc";


elseif(
$order=='old')

    
$query="select * from phno order by id asc";

elseif(
$order=='az')


    
$query="select * from phno order by name asc";

elseif(
$order=='za')

    
$query="select * from phno order by name desc";


//or if user is searching

elseif(isset($_GET['searchb']))

    {

        
$search=$_GET['search'];


        
$query="select * from phno where name like '$search%'";

    }

else

    
//use the default query


    
$query="select * from phno";



//if database does not exits

//first time operation

if(!$db->select_db('one'))


{

    echo 
"<p><i>NONE</i></p>";

    exit;

}

//else

//do the query

$result=$db->query($query);


//find number of rows

$num_rows=$result->num_rows;

//if no rows present probably when

//searching


if($num_rows<=0)

    echo 
"<p><i>No Match Found!</i></p>";

//process all the rows one-by-one


for($i=0;$i<$num_rows;$i++)

{

    
//fetch one row

    
$row=$result->fetch_row();


    
//print the values

    
echo "<p><span style=\"font-size: 200%;\">$row[1]: </span> $row[2]</p>";

}

//close MySQL connection


$db->close();


?>

Performing Simple String Search Using PHP and MySQL


It might now be obvious that we took this phonebook example due to the limitation
of “like” query to search for words within a string. That’s
not to say it’s not possible. In fact searching within string, even longer
ones is very easy and efficient using MySQL’s built-in capability. But
we’ll discuss that in some future posts.


Previous Posts:


Web Services with Apache Axis 1.4 Tutorial: server and client sides

Web services are a handy method of integrating independent systems. Apache Axis is one of the best free tools available for implementing and deploying web services, and also for implementing the web service clients.

In this article we will create a simple, but complete web service and a client for this service step-by-step. Article will be explanatory as much as possible to succeed you in implementing it yourself alone after completing this tutorial.

Prerequisites

  • Must be familiar with Java
  • Familiar with basics on a web server like Tomcat
  • Some knowledge in configuring Axis will be an added advantage

System Configuration Requirements

We will be discussing the configuration in brief as our scope is mainly on web services. (If Axis already configured, jump to implementation). If you find any issues on the configuration part, you can refer to Apache Axis site for troubleshooting. (But if you can not solve it yourself do not worry, post the issue under the comments section in this article, and we’ll get back to you).

JDK installation
These examples have been tested on a machine with JDK 1.6 version.

Web Server
You must have a web server installed; and we will be using Tomcat (5.5 version) web server. If you are not having one, better download Tomcat here{link} and install it yourself (it is quite easy to install Tomcat). Now your CATALINA_HOME environment variable should point to the Tomcat installation directory.

Apache Axis 1.4
Download Apache Axis 1.4 here{link}. Extract the downloaded file and you’ll find a folder named “axis” inside webapps folder.
%Axis_1.4_dir%\webapps\axis
Copy this “axis” folder into your web server’s webapps folder.
%CATALINA_HOME%\webapps

CLASS PATH
Now you must add following libraries into your CLASSPATH environment variable. All of these are available under %Axis_1.4_dir%\lib folder.
  • axis.jar
  • commons-discovery.jar
  • commons-logging.jar
  • jaxrpc.jar
  • log4j-1.2.8.jar
  • saaj.jar
  • wsdl4j.jar
That’s all for configuring Axis 1.4 on your system, quite easy isn’t it? Let’s move on to the implementation part.


Implementation - web service and client

The implementation will consist of two parts. First we will implement web service part; a Calculator will be exposed as a web service. Next a client to use this Calculator web service will be implemented. (Client part starts from here).

Calculator Web Service

Implementing the web service consists of 7 steps. We will be explaining each step in detail.
  1. Functionality provider
  2. Web service’s interface
  3. Java2WSDL - Generate WSDL file
  4. WSDL2Java - Generate server side and client side classes for web service
  5. Bind Web service with Functionality provider
  6. Bundle required classes
  7. Register web service with axis

Project structure

Before starting coding, we'll have a look at the project structure. We are using a separate folder for the project, called "WS-Sample". We will be creating source (.java) files under "WS-Sample\src" folder and storing generated class (.class) files under a "WS-Sample\classes" folder.


1. Functionality provider

First we need to write class with calculator functionality before exposing it as a web service. We have implemented it as a pretty complex high end calculator class, named SimpleCalculator and it's listed below. (It is just a pretty simple class with three methods). This class has no information related to a web service and has been written as a simple independent class. So in the time this class was written, no one has thought of any web service stuff. But we will expose this class as a web service. (Yes, what you guessed is correct. Later you can expose your existing Java classes as web services.)

package org.kamal.wssample;

public class SimpleCalculator {

public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
}

We'll compile above class using following command so that the generated .class file will reside in a folder named "classes" while preserving the package structure.

WS-Sample\src> javac -d ..\classes 
org\kamal\wssample\SimpleCalculator.java

2. Web service’s interface

Now we should write an interface that defines the services that will be provided by our web service. We will expose only two methods through our service; add() and subtract() methods (although we can expose any number of services within one web service). We did choose only two methods to emphasize the fact that we can control which methods we expose. And we will write this class in a separate package; org.kamal.wssample.ws.

package org.kamal.wssample.ws;

public interface Calculator {
int add (int x, int y);
int subtract(int x, int y);
}

And compile using following command.

WS-Sample\src> javac -d ..\classes 
org\kamal\wssample\ws\Calculator.java

3. Java2WSDL - Generate WSDL file


Axis has a tool called Java2WSDL, which generates a WSDL file for a web service using a Java class. We should use the Calculator interface and generate WSDL file as follows. Java2WSDL file requires the Calculator.class file (not Calculator.java) for the operation. Also we will provide the following information.
  • o – name for WSDL file -> calculator.wsdl
  • n – target namespace -> urn:org.kamal.calculator
  • l – url of web service -> http://localhost:8080/axis/services/calculator
WS-Sample\classes> java org.apache.axis.wsdl.Java2WSDL 
-o ..\calculator.wsdl
-n urn:org.kamal.calculator
-l http://localhost:8080/axis/services/calculator
org.kamal.wssample.ws.Calculator

This command will generate a file named calculator.wsdl inside your project folder.

4. WSDL2Java - Generate server side and client side classes for web service

Axis has another tool named WSDL2Java, which can generate server side and client side Java classes using a WSDL file. These classes are needed for deploying the web service and for accessing the service by a Java client. This tool must be provided with WSDL file that we generated in the previous step. It needs the following information as well.
  • o – output folder -> src
  • p – package for generated classes -> org.kamal.wssample.ws.generated
  • s – generate server side classes as well
WS-Sample> java org.apache.axis.wsdl.WSDL2Java 
-o src
-p org.kamal.wssample.ws.generated
-s
calculator.wsdl

Generated java classes will be saved in org.kamal.wssample.ws.generated package. This tool will generate five Java classes in this case with two .wsdd files as listed below.
  • Calculator.java
  • CalculatorService.java
  • CalculatorServiceLocator.java
  • CalculatorSoapBindingImpl.java
  • CalculatorSoapBindingStub.java
  • deploy.wsdd
  • undeploy.wsdd
Now we should compile those generated classes using the following command.

WS-Sample\src> javac –d ..\classes 
org\kamal\wssample\ws\generated\*.java

5. Bind Web service with Functionality provider

As you may have noted; even though we wrote org.kamal.wssample.SimpleCalculator class at the start, we have not used it so far. Now we are going to bind it to the web service.

There is a class named CalculatorSoapBindingImpl inside org.kamal.wssample.ws.generated package. This is the class used to bind our existing SimpleCalculator class to the web service calls. In CalculatorSoapBindingImpl class, there are two methods; add() and subtract(). In this class, we can use the SimpleCalculator to call the actual methods as follows.

package org.kamal.wssample.ws.generated;

import org.kamal.wssample.SimpleCalculator;

public class CalculatorSoapBindingImpl
implements org.kamal.wssample.ws.generated.Calculator{

private SimpleCalculator calc = new SimpleCalculator();

public int add(int a, int b) throws java.rmi.RemoteException {
return calc.add(a, b);
}

public int subtract(int from, int x) throws java.rmi.RemoteException {
return calc.subtract(from, x);
}
}

Just analyze the above class, all method calls are delegated to the actual implementation class SimpleCalculator inside this binding class.

6. Bundle required classes

Now we will create a jar file with all these classes, so that we can use it for deploying our web service. Use the jar command as follows.

WS-Sample\classes> jar cvf ..\calculatorServerSide.jar 
org\kamal\wssample\*.class
org\kamal\wssample\ws\*.class
org\kamal\wssample\ws\generated\*.class

Now copy this jar file into %CATALINA_HOME%\webapps\axis\WEB-INF\lib folder.

WS-Sample> copy calculatorServerSide.jar 
"%CATALINA_HOME%\webapps\axis\WEB-INF\lib"

We will create another jar file to use in the client side. For the client side we only need the classes that were generated by the WSDL2java tool (which are located inside org\kamal\wssample\ws\generated package), except the CalculatorSoapBindingImpl class.

WS-Sample\classes> jar cvf ..\calculatorClientSide.jar 
org\kamal\wssample\ws\generated\CalculatorSoapBindingStub.class
org\kamal\wssample\ws\generated\CalculatorServiceLocator.class
org\kamal\wssample\ws\generated\CalculatorService.class
org\kamal\wssample\ws\generated\Calculator.class

7. Register web service with axis

Axis comes with a tool for registering web services with Axis; it is called AdminClient. Look into org\kamal\wssample\ws\generated folder and you will find two WSDD (web service deployment descriptor) files; deploy.wsdd and undeploy.wsdd. These files were generated by WSDL2Java tool and as used in deploying/undeploying a web service.

Note: (Tomcat) Server must be started before executing the following command.

WS-Sample\src> java org.apache.axis.client.AdminClient 
org\kamal\wssample\ws\generated\deploy.wsdd

This command will deploy the web service into axis. Now restart (Tomcat) server.

To verify our web service is deployed correctly; try following url from your browser.
http://localhost:8080/axis/services/calculator?wsdl
(change the port 8080 in url to match the port on your machine)

This will show up a complete wsdl file, and it is the complete definition of the web service that we have deployed.

Now everything on web service (server side) is completed and our web service is successfully deployed.


Web Service client

Now it’s time for us to write a client to access this web service and use provided services. For this we need the calculatorClientSide.jar file that we created in an earlier step.

For the client side we will create a new project folder named “WS-Client” with sub folders named src, classes and lib. Copy the generated calculatorClientSide.jar file into the "WS-Client\lib" folder.


We will create the client as follows. Since our web service exposed two methods, add() and subtract(); client class will use the service and call those add() and subtract() methods.

package org.kamal.wsclient;

import org.kamal.wssample.ws.generated.Calculator;
import org.kamal.wssample.ws.generated.CalculatorService;
import org.kamal.wssample.ws.generated.CalculatorServiceLocator;

public class CalcClient {

public static void main(String[] args) throws Exception {
CalculatorService service = new CalculatorServiceLocator();
Calculator calc = service.getcalculator();

System.out.println("15 + 6 = " + calc.add(15, 6));
System.out.println("15 - 6 = " + calc.subtract(15, 6));
}
}

The above class has not used even a single class that we wrote for Calculator implementation, only a few classes that WSDL2Java tool generated. We have not exposed the server side classes, but just provided a way to get the service from those classes.

Compile the class with following command.

WS-Sample-Client\src> javac 
-classpath %CLASSPATH%;..\lib\calculatorClientSide.jar
-d ..\classes
org\kamal\wsclient\CalcClient.java

Now we can run our web service client using following command.

WS-Sample-Client\classes> java 
-cp %CLASSPATH%;.;..\lib\calculatorClientSide.jar
org.kamal.wsclient.CalcClient

You would see the following as the result.

15 + 6 = 21
15 – 6 = 9

Our web service client, CalcClient has accessed the web service and received the results from the operations done by SimpleCalculator class (which is running on server side).

As you can see, generating the client side is much easier than the server side.

C++ unions : how to find currently active member

C++ unions can have member functions. And to surprise you even more, they can have constructors and a destructor. But that's not all that I intend to write about. A question straightaway comes into our mind: If unions can have member functions, how would you know which of its data member is currently 'active' or 'set' (well, this can come up even in case they don't have member functions, not very specific to them).

This is important to know because if say member1 of the union is set then that is the one that is active and you can only access its value. Accessing value of any of the other members is undefined behavior as per the standard and we all know what undefined behavior means; it may work in your case, on your machine, and it can fail you in front of a very potential client of yours or your managers when demo-ing your application.

Here is a way; you can let your member functions (is applicable to unions in general) know which data member is currently set:

[code]
union myunion
{
    int member; //if == 1 then mem1 is set, use it; if ==2 mem2 is set, use it
    struct
    {
        int struct_id; //1

        //other members
    } mem1;
    struct
    {

        int struct_id; //2
        //other members
    } mem2;
    //and so on...
    void memfunc()
    {
        //could use a switch
        if(member==1)
        {
            //use mem1
        }
        else if (member ==2)
        {
            //use mem2
        }
        //and so on...
    }
};
[/code]

The in-lined comments are self explanatory. The member variable 'member' can be used as a signifier as to which of the other struct members is currently active, reading which is not unsafe. What makes it safe, you might wonder? To understand the guarantee that the programmer is provided with, let's go through what the C++ standard has to say about it. Quote: 18.5 [class.union]/1 provides the rules that suffice the above explanations. I will quote that paragraph in full below:

[quote]
In a union, at most one of the data members can be active at any time, that is, the value of at most one of the data members can be stored in a union at any time. [Note: one special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members; see 9.2. —end note] The size of a union is sufficient to contain the largest of its data members. Each data member is allocated as if it were the sole member of a struct. A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class. An object of a non-trivial class (clause 9) shall not be a member of a union, nor shall an array of such objects. If a union contains a static data member or a member of reference type the program is ill-formed.
[/quote]

The above tells you that member functions are allowed with unions including constructors and destructors. It also makes a statement that structs inside a union that follow a common initial sequence, it is legal to inspect (i.e. query or read the value of) that common initial sequence. Also, it says that, each data member is allocated as if it were the sole member of a struct. In our example above, 'member' is such a data member. It is as if you have a struct as a member of the union with just a single data member of type 'int'.

Moreover, there is another mention in section 9.2/18 as follows:

[Quote]
A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. —end note]
[/quote]

Considering the above quotes, it is guaranteed that, each 'struct_id' (in the code sample above) shares the same layout as the first member of the union of int type "member". And hence using them (being the same initial sequence) in a union is perfectly safe, independent of which union member had been previously set (or is active).

This (the int member 'member') allows us to have a convenient way to see which data member of the union is currently active without peeping into any specific struct members (not that it is impossible) and without causing any effect on the size of the union.

Having member functions find out the active member that way is a little inextensible. What if you plan to add another struct member? You would need to enhance the switch statement, or the if-else-if conditionals to accommodate that. But then, even in case unions have no member functions, you might very well need to know that information and the above method helps.

RevResponse, Offer Freebies to Earn Money

RevResponse Affiliate Network


This is sort of a paid review. You don’t normally find me doing this
but since I truly liked RevResponse at the first sight, I’m writing this.


Overview


RevResponse
(aff. link) is one-of-a-kind of affiliate network that pays you for promoting
Free Magazines, Technical Whitepapers, Software etc the topic of these materials
range from Agriculture to Software Development and from Bio-Technology to Transportaion
& Logistics. This means most content based website/blogs won’t have
much problem finding relevant offers to promote. Since all magazines, whitepapers
etc. are free, it’s kind of a Pay-Per-Lead affiliate network. The magazines,
that many presume to be SPAM, are not at all spammy rather are really useful
and informative. And as those are all free, it means great source of information
for your visitors and income for you!


Sign-Up


The Sign-Up
(aff. link) process is pretty straightforward and doesn’t require you
to fill any lengthy forms once you are done with that, they would contact you
within a few days (in my case less that 24-hours) to tell you whether your application
is accepted or not. If you have an informative blog/website, you won’t
have any problems getting accepted even though your website/blog is on a free
host.


Promotional Materials


Image Ads: Basic ads in all standard sizes.


Text Ads: Plain text ads. They also provide pre-written paragraphs.
It’d be best if you personalize it.


Flash Widget: Beside normal image based ads they also provide
interactive flash widgets. Catches eye and would prove to be useful for your
visitors.


Co-Branded Site: Really great, like Amazon’s astore,
RevResponse provides you with a customizable website with the same look as your
existing website. It is hosted on tradepub.com. It could be customized to create
your very own Magazine Catalog.


Earning Potential


If you choose the offers wisely and promote them using genuine reviews, in
other words to give genuine value to your visitors, there nothing stopping you
from earning, at least, more than traditional advertising (AdSense etc.) they
pay between $1.50 - $20 (as far as I’ve heard), which is great. Another
great thing is unlike many other networks, RevResponse accepts leads (subscriptions,
downloads of the offers you promote) from international visitors too!


Conclusion


Pros:



  • Company behind RevResponse is quite old (since 1996)

  • Great affiliate managers, support and “personal” feeling. They
    have a blog and a forum to share and get ideas.

  • Payment via PayPal and Check.

  • Accepts international leads.

  • Offers that you can promote are FREE.


Cons:



  • Net-45 Payment scheme which means you’re paid 45-days after the month
    you earnings reach $50 (minimum payment threshold)

  • Web is so cluttered with spam that some people don’t bother for “freebies”
    anymore


I’d genuinely recommend RevResponse
(aff. link) to anyone having a content blog/website. Do check it out!

Java2WDSL & WSDL2Java - java.util.Date not handled consistently

Java2WSDL and WSDL2Java tools in Axis (1.4) are pretty handy tools. Those help you to generate a WSDL from a java class as well as generating stubs/skeletons from a WSDL. But if you are dealing with java.util.Date fields in your code, you must pay some attention.

Java2WSDL

Consider the following java interface which uses java.util.Date as a return type as well as a method parameter.

public interface MyService {
public java.util.Date getNextDate();

public void updateLastRun(java.util.Date date);
}

When the Java2WDSL tool generates a WSDL for a class with a
Date field, it will represent java.util.Date fields as dateTime fields in WSDL. The generated WSDL file would look as follows.

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://service"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://service" xmlns:intf="http://service"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<!--WSDL created by Apache Axis version: 1.4-->

<wsdl:message name="setOpeningDateResponse">
</wsdl:message>
<wsdl:message name="setOpeningDateRequest">
<wsdl:part name="in0" type="xsd:dateTime"/>
</wsdl:message>

<wsdl:message name="getOpenningDateResponse">
<wsdl:part name="getOpenningDateReturn" type="xsd:dateTime"/>
</wsdl:message>
<wsdl:message name="getOpenningDateRequest">
</wsdl:message>

<!--rest of the content removed-->
</wsdl:definitions>

I have posted only a part of the WSDL which contains the messages. If you go through the above WSDL, you will see that both java.util.Date fields are represented as "dateTime" fields (type="xsd:dateTime").

WSDL2Java

Now we can generate the Java classes using our WSDL file. WSDL2Java tool will use the WSDL file and generate a set of classes for us. Following is the generated MyService class.

package service;

public interface MyService extends java.rmi.Remote {
public java.util.Calendar getOpenningDate()
throws java.rmi.RemoteException;

public void setOpeningDate(java.util.Calendar in0)
throws java.rmi.RemoteException;
}

See the difference? The generated MyService interface has java.util.Calendar fields instead of java.util.Date.

Since your binding implementation class implements this new interface, you will have some hard time in dealing with this data type changes. May be there's a quick fix which we don't see from here.

We tried this on Axis1.4 and did not try with Axis2.

Java Sorting: Comparator vs Comparable Tutorial

Java Comparators and Comparables? What are they? How do we use them? This is a question we received from one of our readers. This article will discuss the java.util.Comparator and java.lang.Comparable in details with a set of sample codes for further clarifications.

Prerequisites

  • Basic Java knowledge

System Requirements

  • JDK installed

What are Java Comparators and Comparables?

As both names suggest (and you may have guessed), these are used for comparing objects in Java. Using these concepts; Java objects can be
sorted according to a predefined order.

Two of these concepts can be explained as follows.

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.

Do we need to compare objects?

The simplest answer is yes. When there is a list of objects, ordering these objects into different orders becomes a must in some situations. For example; think of displaying a list of employee objects in a web page. Generally employees may be displayed by sorting them using the employee id. Also there will be requirements to sort them according to the name or age as well. In these situations both these (above defined) concepts will become handy.

How to use these?

There are two interfaces in Java to support these concepts, and each of these has one method to be implemented by user.
Those are;

java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.
  1. positive – this object is greater than o1
  2. zero – this object equals to o1
  3. negative – this object is less than o1

java.util.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.
  1. positive – o1 is greater than o2
  2. zero – o1 equals to o2
  3. negative – o1 is less than o1

java.util.Collections.sort(List) and java.util.Arrays.sort(Object[]) methods can be used to sort using natural ordering of objects.
java.util.Collections.sort(List, Comparator) and java.util.Arrays.sort(Object[], Comparator) methods can be used if a Comparator is available for comparison.

The above explained Employee example is a good candidate for explaining these two concepts. First we’ll write a simple Java bean to represent the Employee.

public class Employee {
private int empId;
private String name;
private int age;

public Employee(int empId, String name, int age) {
// set values on attributes
}
// getters & setters
}

Next we’ll create a list of Employees for using in different sorting requirements. Employees are added to a List without any specific order in the following class.

import java.util.*;

public class Util {

public static List<Employee> getEmployees() {

List<Employee> col = new ArrayList<Employee>();

col.add(new Employee(5, "Frank", 28));
col.add(new Employee(1, "Jorge", 19));
col.add(new Employee(6, "Bill", 34));
col.add(new Employee(3, "Michel", 10));
col.add(new Employee(7, "Simpson", 8));
col.add(new Employee(4, "Clerk",16 ));
col.add(new Employee(8, "Lee", 40));
col.add(new Employee(2, "Mark", 30));

return col;
}
}

Sorting in natural ordering

Employee’s natural ordering would be done according to the employee id. For that, above Employee class must be altered to add the comparing ability as follows.

public class Employee implements Comparable<Employee> {
private int empId;
private String name;
private int age;

/**
* Compare a given Employee with this object.
* If employee id of this object is
* greater than the received object,
* then this object is greater than the other.
*/

public int compareTo(Employee o) {
return this.empId - o.empId ;
}
….
}

The new compareTo() method does the trick of implementing the natural ordering of the instances. So if a collection of Employee objects is sorted using Collections.sort(List) method; sorting happens according to the ordering done inside this method.

We’ll write a class to test this natural ordering mechanism. Following class use the Collections.sort(List) method to sort the given list in natural order.

import java.util.*;

public class TestEmployeeSort {

public static void main(String[] args) {
List coll = Util.getEmployees();
Collections.sort(coll); // sort method
printList(coll);
}

private static void printList(List<Employee> list) {
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}

Run the above class and examine the output. It will be as follows. As you can see, the list is sorted correctly using the employee id. As empId is an int value, the employee instances are ordered so that the int values ordered from 1 to 8.

EmpId Name Age
1 Jorge 19
2 Mark 30
3 Michel 10
4 Clerk 16
5 Frank 28
6 Bill 34
7 Simp 8
8 Lee 40

Sorting by other fields

If we need to sort using other fields of the employee, we’ll have to change the Employee class’s compareTo() method to use those fields. But then we’ll loose this empId based sorting mechanism. This is not a good alternative if we need to sort using different fields at different occasions. But no need to worry; Comparator is there to save us.

By writing a class that implements the java.util.Comparator interface, you can sort Employees using any field as you wish even without touching the Employee class itself; Employee class does not need to implement java.lang.Comparable or java.util.Comparator interface.

Sorting by name field

Following EmpSortByName class is used to sort Employee instances according to the name field. In this class, inside the compare() method sorting mechanism is implemented. In compare() method we get two Employee instances and we have to return which object is greater.

public class EmpSortByName implements Comparator<Employee>{

public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
}

Watch out: Here, String class’s compareTo() method is used in comparing the name fields (which are Strings).

Now to test this sorting mechanism, you must use the Collections.sort(List, Comparator) method instead of Collections.sort(List) method. Now change the TestEmployeeSort class as follows. See how the EmpSortByName comparator is used inside sort method.

import java.util.*;

public class TestEmployeeSort {

public static void main(String[] args) {

List coll = Util.getEmployees();
//Collections.sort(coll);
//use Comparator implementation
Collections.sort(coll, new EmpSortByName());
printList(coll);
}

private static void printList(List<Employee> list) {
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}

Now the result would be as follows. Check whether the employees are sorted correctly by the name String field. You’ll see that these are sorted alphabetically.

EmpId Name Age
6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simp 8

Sorting by empId field

Even the ordering by empId (previously done using Comparable) can be implemented using Comparator; following class
does that.

public class EmpSortByEmpId implements Comparator<Employee>{

public int compare(Employee o1, Employee o2) {
return o1.getEmpId() - o2.getEmpId();
}
}

Explore further

Do not stop here. Work on the followings by yourselves and sharpen knowledge on these concepts.
  1. Sort employees using name, age, empId in this order (ie: when names are equal, try age and then next empId)
  2. Explore how & why equals() method and compare()/compareTo() methods must be consistence.

If you have any issues on these concepts; please add those in the comments section and we’ll get back to you.

Branch and Bound Algorithm

Branch and Bound is a tree pruning technique for solving optimization problem using search technique.

Optimization problem is trying to find a path which maximize (or minimize) the profit. This solution can be represented as a state-space tree. Each branch represents all the possible next steps. The goal is to find a path which returns the maximum profit.



Knapsack problem

We have n kinds of items, 1 through n. Each item j has a value pj and a weight wj. The maximum weight that we can carry in the bag is c. How should we decide which item to pick such that the sum of value is maximized (optimized) while the sum of weight is less than c (fulfill constraint).


Minmax algorithm

2 players make moves in turn. Now is Player 1's turn, how should he choose his move in order to minimize his maximum lost when look ahead N steps.



Brute force approach
One naive approach is to layout all the possible combination of steps and calculate the profit of each path. This is exponential complexity.

So a tree pruning mechanism is usually employed to reduce the exponential explosion. Bound and branch is such a mechanism. The basic idea is to have a cheap way to compute the upper bound and lower bound of the profit of a subtree. If a subtree is know to have its upper bound lower than the lower bound of another subtree, then this subtree can be pruned because its best result is worse than the worst result of that another subtree.



Knapsack solution




Minimax solution (Alpha-Beta search)




Check out this stream