View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: AbstractSaver.java
13   * Created: 2008-07-12
14   * Author: apohllo
15   * $Id: AbstractSaver.java 2232 2009-01-04 22:59:53Z apohllo $
16   */
17  
18  package pl.edu.agh.cast.model.mapper.internal;
19  
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.LinkedList;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.log4j.Logger;
27  
28  import pl.edu.agh.cast.model.base.BasePlugin;
29  import pl.edu.agh.cast.model.mapper.Mappable;
30  import pl.edu.agh.cast.model.mapper.Node;
31  import pl.edu.agh.cast.model.mapper.Saver;
32  import pl.edu.agh.cast.model.mapper.annotation.Mapping;
33  
34  /**
35   * Partial implementation of the {@link Saver} interface.
36   *
37   * @author AGH CAST Team
38   */
39  public abstract class AbstractSaver implements Saver {
40  
41  	/**
42  	 * The logger.
43  	 */
44  	protected static Logger log = BasePlugin.getLogger();
45  
46  	/**
47  	 * The object to be saved.
48  	 */
49  	protected Mappable primaryObject;
50  
51  	/**
52  	 * The force flag (should the save be forced?).
53  	 */
54  	protected boolean force;
55  
56  	private Map<Class<Mappable>, Mapping> classMap = new HashMap<Class<Mappable>, Mapping>();
57  
58  	/**
59  	 * The visited nodes set.
60  	 */
61  	protected Set<Mappable> visited = new HashSet<Mappable>();
62  
63  	/**
64  	 * The saved nodes set.
65  	 */
66  	protected Set<Mappable> stored = new HashSet<Mappable>();
67  
68  	/**
69  	 * The queue of nodes to be saved.
70  	 */
71  	protected LinkedList<Mappable> queue = new LinkedList<Mappable>();
72  
73  	/**
74  	 * Mapping: node id - node
75  	 */
76  	protected Map<Long, Node> nodes = new HashMap<Long, Node>();
77  
78  	/**
79  	 * Constructor takes the object to be saved and accepts the
80  	 * force flag (if set to true, the object is saved no matter
81  	 * if it was saved earlier.
82  	 * @param object The object to save
83  	 * @param force2 The force flag
84  	 */
85  	public AbstractSaver(Mappable object, boolean force2) {
86  		primaryObject = object;
87  		force = force2;
88  	}
89  
90  	protected Mapping getMapping(Class<Mappable> klass) {
91  		if (!classMap.containsKey(klass)) {
92  			classMap.put(klass, klass.getAnnotation(Mapping.class));
93  		}
94  		return classMap.get(klass);
95  	}
96  
97  }