/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.hyperic.sigar.ptql;

import java.util.Map;
import org.hyperic.sigar.util.ReferenceMap;

public class ProcessQueryFactory {
    
    private static Map cache =
        ReferenceMap.synchronizedMap();

    public ProcessQueryFactory() {}

    public static ProcessQuery getInstance(String query)
        throws MalformedQueryException {

        if (query == null) {
            throw new MalformedQueryException("null query");
        }

        if (query.length() == 0) {
            throw new MalformedQueryException("empty query");
        }

        ProcessQuery pQuery = (ProcessQuery)cache.get(query);

        if (pQuery != null) {
            return pQuery;
        }

        pQuery = new SigarProcessQuery();
        ((SigarProcessQuery)pQuery).create(query);
        cache.put(query, pQuery);
        return pQuery;
    }
}

@@ -777,19 +777,24 @@
 va.util.
-Map
+Iterator
 ;%0Aimport
@@ -798,40 +798,44 @@
 ort 
-org.hyperic.sigar.util.Reference
+java.util.Map;%0Aimport java.util.Hash
 Map;
@@ -899,59 +899,384 @@
 tic 
-Map cache =%0A        ReferenceMap.synchronizedMap();
+ProcessQueryFactory instance = null;%0A%0A    private Map cache = new HashMap();%0A%0A    public ProcessQueryFactory() %7B%7D%0A%0A    public void clear() %7B%0A        for (Iterator it=this.cache.values().iterator();%0A             it.hasNext();)%0A        %7B%0A            SigarProcessQuery query = (SigarProcessQuery)it.next();%0A            query.destroy();%0A        %7D%0A        this.cache.clear();%0A    %7D
 %0A%0A  
@@ -1276,32 +1276,39 @@
   %7D%0A%0A    public 
+static 
 ProcessQueryFact
@@ -1314,57 +1314,181 @@
 tory
+ getInstance
 () %7B
-%7D%0A
 %0A
+ 
     
-public static ProcessQuery getInstance
+   if (instance == null) %7B%0A            instance = new ProcessQueryFactory();%0A        %7D%0A        return instance;%0A    %7D%0A%0A    public ProcessQuery getQuery
 (Str
@@ -1793,16 +1793,21 @@
 ssQuery)
+this.
 cache.ge
@@ -1986,16 +1986,21 @@
         
+this.
 cache.pu
@@ -2045,11 +2045,219 @@
 y;%0A    %7D
+%0A%0A    /**%0A     * @deprecated%0A     * @see getQuery%0A     */%0A    public static ProcessQuery getInstance(String query)%0A        throws MalformedQueryException %7B%0A%0A        return getInstance().getQuery(query);%0A    %7D
 %0A%7D%0A

add method to clear cache