KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > query > QueryKit


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.storage.query;
27
28 import java.util.*;
29
30 /**
31  * Kit which applies Queries to Lists
32  *
33  * @author stephan
34  * @version $Id: QueryKit.java 864 2003-05-23 10:47:26Z stephan $
35  */

36
37 public class QueryKit {
38   /**
39    * Sort a list with a comparator. The result list size can be
40    * limited.
41    *
42    * @param list List to query
43    * @param c Comparator which defines the sorting order
44    * @param size How many elements should be returned
45    */

46   public static List querySorted(List list, Comparator c, int size) {
47     ArrayList result = new ArrayList(list);
48     Collections.sort(result, c);
49     return result.subList(0, Math.min(size, result.size()));
50   }
51
52   /**
53    * Query and sort a list with a comparator and a given query.
54    *
55    * @param list List to query
56    * @param query Query which defines what elements to return
57    * @param c Comparator which defines the sorting order
58    * @param size How many elements should be returned
59    */

60   public static List querySorted(List list, Query query, Comparator c) {
61     List result = query(list, query);
62     Collections.sort(result, c);
63     return result;
64   }
65
66   /**
67    * Query and sort a list with a comparator and a given query.
68    * The result list size can be limited.
69    *
70    * @param list List to query
71    * @param query Query which defines what elements to return
72    * @param c Comparator which defines the sorting order
73    * @param size How many elements should be returned
74    */

75   public static List querySorted(List list, Query query, Comparator c, int size) {
76     List result = query(list, query);
77     Collections.sort(result, c);
78     return result.subList(0, Math.min(size, result.size()));
79   }
80   /**
81    * Query a list with a given query.
82    *
83    * @param list List to query
84    * @param query Query which defines what elements to return
85    */

86   public static List query(List list, Query query) {
87     Iterator iterator = list.iterator();
88     List result = new ArrayList();
89     while (iterator.hasNext()) {
90       Object JavaDoc object = iterator.next();
91       if (query.fit(object)) {
92         result.add(object);
93       }
94     }
95     return result;
96   }
97 }
98
Popular Tags