Source Code : This program demonstrates various persistence delegates.

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

This program demonstrates various persistence delegates.

  
/*
 This program is a part of the companion code for Core Java 8th ed.
 (http://horstmann.com/corejava)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.geom.Rectangle2D;
import java.beans.BeanInfo;
import java.beans.ConstructorProperties;
import java.beans.DefaultPersistenceDelegate;
import java.beans.Encoder;
import java.beans.ExceptionListener;
import java.beans.Expression;
import java.beans.Introspector;
import java.beans.PersistenceDelegate;
import java.beans.Statement;
import java.beans.XMLEncoder;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * This program demonstrates various persistence delegates.
 * 
 * @version 1.01 2007-10-03
 * @author Cay Horstmann
 */
public class PersistenceDelegateTest {
  public static class Point {
    @ConstructorProperties( { "x", "y" })
    public Point(int x, int y) {
      this.x = x;
      this.y = y;
    }

    public int getX() {
      return x;
    }

    public int getY() {
      return y;
    }

    private final int x, y;
  }

  public static void main(String[] args) throws Exception {
    PersistenceDelegate delegate = new PersistenceDelegate() {
      protected Expression instantiate(Object oldInstance, Encoder out) {
        Employee e = (Employee) oldInstance;
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(e.getHireDay());
        return new Expression(oldInstance, Employee.class, "new", new Object[] { e.getName(),
            e.getSalary(), c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE) });
      }
    };
    BeanInfo info = Introspector.getBeanInfo(Employee.class);
    info.getBeanDescriptor().setValue("persistenceDelegate", delegate);

    XMLEncoder out = new XMLEncoder(System.out);
    out.setExceptionListener(new ExceptionListener() {
      public void exceptionThrown(Exception e) {
        e.printStackTrace();
      }
    });

    out.setPersistenceDelegate(Rectangle2D.Double.class, new DefaultPersistenceDelegate(
        new String[] { "x", "y", "width", "height" }));

    out.setPersistenceDelegate(Inet4Address.class, new DefaultPersistenceDelegate() {
      protected Expression instantiate(Object oldInstance, Encoder out) {
        return new Expression(oldInstance, InetAddress.class, "getByAddress",
            new Object[] { ((InetAddress) oldInstance).getAddress() });
      }
    });

    out.setPersistenceDelegate(BitSet.class, new DefaultPersistenceDelegate() {
      protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
        super.initialize(type, oldInstance, newInstance, out);
        BitSet bs = (BitSet) oldInstance;
        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1))
          out.writeStatement(new Statement(bs, "set", new Object[] { i, i + 1, true }));
      }
    });

    out.writeObject(new Employee("Harry Hacker", 50000, 1989, 10, 1));
    out.writeObject(new Point(17, 29));
    out.writeObject(new java.awt.geom.Rectangle2D.Double(5, 10, 20, 30));
    out.writeObject(InetAddress.getLocalHost());
    BitSet bs = new BitSet();
    bs.set(1, 4);
    bs.clear(2, 3);
    out.writeObject(bs);
    out.close();
  }
}

/*
 * This program is a part of the companion code for Core Java 8th ed.
 * (http://horstmann.com/corejava)
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
class Employee {
  public Employee(String n, double s, int year, int month, int day) {
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
  }

  public Employee(String n, double s, Date h) {
    name = n;
    salary = s;
    hireDay = h;
  }

  public String getName() {
    return name;
  }

  public double getSalary() {
    return salary;
  }

  public Date getHireDay() {
    return hireDay;
  }

  public void raiseSalary(double byPercent) {
    double raise = salary * byPercent / 100;
    salary += raise;
  }

  private String name;

  private double salary;

  private Date hireDay;
}

   
    
  

Thank with us