alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (PetServiceImpl.java)

This example Spring Framework source code file (PetServiceImpl.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

bo, date, dog, initializingbean, integer, integer, macaw, pet, pet, poodle, simpledateformat, simpledateformat, string, string, text, util

The Spring Framework PetServiceImpl.java source code

package org.springframework.samples.petportal.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.samples.petportal.domain.Pet;

/**
 * The PetService implementation.
 *
 * @author John A. Lewis
 * @author Mark Fisher
 * @author Juergen Hoeller
 */
public class PetServiceImpl implements InitializingBean, PetService {

	private SortedMap pets = Collections.synchronizedSortedMap(new TreeMap());

	private int initPets = -1;

	public void afterPropertiesSet() throws BeansException, ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		if (initPets < 0 || initPets > 0) {
			addPet("Dog", "Poodle", "Fido", dateFormat.parse("05/21/1997"));
		}
		if (initPets < 0 || initPets > 1) {
			addPet("Cat", "Calico", "Boots", dateFormat.parse("11/07/2003"));
		}
		if (initPets < 0 || initPets > 2) {
			addPet("Bird", "Macaw", "Polly", dateFormat.parse("03/17/2001"));
		}
		if (initPets < 0 || initPets > 3) {
			addPet("Snake", "Boa", "Bo", dateFormat.parse("09/03/2005"));
		}
	}

	public Pet getPet(Integer key) {
		synchronized (pets) {
			return (Pet) this.pets.get(key);
		}
	}

	public Pet getPet(int key) {
		return getPet(new Integer(key));
	}

	public int getPetCount() {
		synchronized (pets) {
			return this.pets.size();
		}
	}

	public SortedSet getAllPets() {
		synchronized (pets) {
			return new TreeSet(this.pets.values());
		}
	}

	public int addPet(Pet pet) {
		int key;
		synchronized (pets) {
			if (pets.isEmpty()) {
				key = 1;
			}
			else {
				key = ((Integer) pets.lastKey()).intValue() + 1;
			}
			Integer keyObj = new Integer(key);
			pet.setKey(keyObj);
			this.pets.put(keyObj, pet);
		}
		return key;
	}

	public int addPet(String species, String breed, String name, Date birthdate) {
		Pet pet = new Pet(species, breed, name, birthdate);
		return addPet(pet);
	}

	public void savePet(Pet pet) {
		synchronized (pets) {
			this.pets.put(pet.getKey(), pet);
		}
	}

	public void deletePet(Integer key) {
		synchronized (pets) {
			this.pets.remove(key);
		}
	}

	public void deletePet(Pet pet) {
		deletePet(pet.getKey());
	}

	public void deletePet(int key) {
		deletePet(new Integer(key));
	}

	public void setInitPets(int initPets) {
		this.initPets = initPets;
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework PetServiceImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.