Java tutorial
/* * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license * distributed with this file and available online at * http://www.ja-sig.org/products/cas/overview/license/ */ package com.baosight.buapx.ticketregistry; import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.jasig.cas.ticket.Ticket; import org.jasig.cas.ticket.registry.AbstractTicketRegistry; import org.springframework.util.Assert; /** * Implementation of the TicketRegistry that is backed by a ConcurrentHashMap. * * @author Scott Battaglia * @version $Revision$ $Date$ * @since 3.0 modified by lizidi. Implements the TicketCount to count the ticket */ public final class DefaultTicketRegistry extends AbstractTicketRegistry { /** A HashMap to contain the tickets. */ private final Map<String, Ticket> cache; public DefaultTicketRegistry() { this.cache = new ConcurrentHashMap<String, Ticket>(); } /** * Creates a new, empty registry with the specified initial capacity, load * factor, and concurrency level. * * @param initialCapacity - the initial capacity. The implementation * performs internal sizing to accommodate this many elements. * @param loadFactor - the load factor threshold, used to control resizing. * Resizing may be performed when the average number of elements per bin * exceeds this threshold. * @param concurrencyLevel - the estimated number of concurrently updating * threads. The implementation performs internal sizing to try to * accommodate this many threads. */ public DefaultTicketRegistry(int initialCapacity, final float loadFactor, final int concurrencyLevel) { this.cache = new ConcurrentHashMap<String, Ticket>(initialCapacity, loadFactor, concurrencyLevel); } /** * @throws IllegalArgumentException if the Ticket is null. */ public void addTicket(final Ticket ticket) { Assert.notNull(ticket, "ticket cannot be null"); if (log.isDebugEnabled()) { log.debug("Added ticket [" + ticket.getId() + "] to registry."); } this.cache.put(ticket.getId(), ticket); } public Ticket getTicket(final String ticketId) { if (ticketId == null) { return null; } if (log.isDebugEnabled()) { log.debug("Attempting to retrieve ticket [" + ticketId + "]"); } final Ticket ticket = this.cache.get(ticketId); if (ticket != null) { log.debug("Ticket [" + ticketId + "] found in registry."); } else { log.debug("Ticket [" + ticketId + "] not found in registry."); } return ticket; } public boolean deleteTicket(final String ticketId) { if (ticketId == null) { return false; } if (log.isDebugEnabled()) { log.debug("Removing ticket [" + ticketId + "] from registry"); } return (this.cache.remove(ticketId) != null); } public Collection<Ticket> getTickets() { return Collections.unmodifiableCollection(this.cache.values()); } }