KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > BasicTaskMap


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 import org.objectweb.deployment.scheduling.core.api.Task;
32
33 /**
34  * Basic implementation of the {@link TaskMap} interface.
35  */

36
37 public class BasicTaskMap extends HashMap JavaDoc implements TaskMap {
38
39   private List JavaDoc values = new ArrayList JavaDoc();
40   
41   // --------------------------------------------------------------------------
42
// Implementation of the TaskMap interface
43
// --------------------------------------------------------------------------
44

45   public Task[] getTasks () {
46     return (Task[])values.toArray(new Task[size()]);
47   }
48   
49   public Task getTask (final String JavaDoc type, final Object JavaDoc id)
50     throws NoSuchElementException JavaDoc
51   {
52     Task t = (Task)get(new Key(type, id));
53     if (t == null) {
54       throw new NoSuchElementException JavaDoc();
55     }
56     return t;
57   }
58   
59   public void addTask (final String JavaDoc type, final Object JavaDoc id, final Task task) {
60     Key key = new Key(type, id);
61     if (get(key) == null) {
62       put(key, task);
63       values.add(task);
64     }
65   }
66   
67   // --------------------------------------------------------------------------
68
// Inner classes
69
// --------------------------------------------------------------------------
70

71   static class Key {
72     
73     public final String JavaDoc type;
74     
75     public final Object JavaDoc id;
76     
77     public Key (final String JavaDoc type, final Object JavaDoc id) {
78       this.type = type;
79       this.id = id;
80     }
81     
82     public boolean equals (final Object JavaDoc o) {
83       if (o instanceof Key) {
84         Key k = (Key)o;
85         return k.type.equals(type) && k.id.equals(id);
86       }
87       return false;
88     }
89     
90     public int hashCode () {
91       return type.hashCode()*id.hashCode();
92     }
93   }
94 }
95
Popular Tags