source: josm/trunk/src/com/drew/metadata/Metadata.java@ 4231

Last change on this file since 4231 was 4231, checked in by stoecker, 13 years ago

add signpost and metadata extractor code to repository directly

File size: 4.3 KB
Line 
1/*
2 * Metadata.java
3 *
4 * This class is public domain software - that is, you can do whatever you want
5 * with it, and include it software that is licensed under the GNU or the
6 * BSD license, or whatever other licence you choose, including proprietary
7 * closed source licenses. Similarly, I release this Java version under the
8 * same license, though I do ask that you leave this header in tact.
9 *
10 * If you make modifications to this code that you think would benefit the
11 * wider community, please send me a copy and I'll post it on my site.
12 *
13 * If you make use of this code, I'd appreciate hearing about it.
14 * drew.noakes@drewnoakes.com
15 * Latest version of this software kept at
16 * http://drewnoakes.com/
17 *
18 * Created on 28 April 2002, 17:40
19 * Modified 04 Aug 2002
20 * - Adjusted javadoc
21 * - Added
22 * Modified 29 Oct 2002 (v1.2)
23 * - Stored IFD directories in separate tag-spaces
24 * - iterator() now returns an Iterator over a list of TagValue objects
25 * - More get*Description() methods to detail GPS tags, among others
26 * - Put spaces between words of tag name for presentation reasons (they had no
27 * significance in compound form)
28 */
29package com.drew.metadata;
30
31import java.io.Serializable;
32import java.util.ArrayList;
33import java.util.HashMap;
34import java.util.Iterator;
35
36/**
37 * Result from an exif extraction operation, containing all tags, their
38 * values and support for retrieving them.
39 * @author Drew Noakes http://drewnoakes.com
40 */
41public final class Metadata implements Serializable
42{
43 /**
44 *
45 */
46 private final HashMap directoryMap;
47
48 /**
49 * List of Directory objects set against this object. Keeping a list handy makes
50 * creation of an Iterator and counting tags simple.
51 */
52 private final ArrayList directoryList;
53
54 /**
55 * Creates a new instance of Metadata. Package private.
56 */
57 public Metadata()
58 {
59 directoryMap = new HashMap();
60 directoryList = new ArrayList();
61 }
62
63
64// OTHER METHODS
65
66 /**
67 * Creates an Iterator over the tag types set against this image, preserving the order
68 * in which they were set. Should the same tag have been set more than once, it's first
69 * position is maintained, even though the final value is used.
70 * @return an Iterator of tag types set for this image
71 */
72 public Iterator getDirectoryIterator()
73 {
74 return directoryList.iterator();
75 }
76
77 /**
78 * Returns a count of unique directories in this metadata collection.
79 * @return the number of unique directory types set for this metadata collection
80 */
81 public int getDirectoryCount()
82 {
83 return directoryList.size();
84 }
85
86 /**
87 * Returns a <code>Directory</code> of specified type. If this <code>Metadata</code> object already contains
88 * such a directory, it is returned. Otherwise a new instance of this directory will be created and stored within
89 * this Metadata object.
90 * @param type the type of the Directory implementation required.
91 * @return a directory of the specified type.
92 */
93 public Directory getDirectory(Class type)
94 {
95 if (!Directory.class.isAssignableFrom(type)) {
96 throw new RuntimeException("Class type passed to getDirectory must be an implementation of com.drew.metadata.Directory");
97 }
98 // check if we've already issued this type of directory
99 if (directoryMap.containsKey(type)) {
100 return (Directory)directoryMap.get(type);
101 }
102 Object directory;
103 try {
104 directory = type.newInstance();
105 } catch (Exception e) {
106 throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString());
107 }
108 // store the directory in case it's requested later
109 directoryMap.put(type, directory);
110 directoryList.add(directory);
111 return (Directory)directory;
112 }
113
114 /**
115 * Indicates whether a given directory type has been created in this metadata
116 * repository. Directories are created by calling getDirectory(Class).
117 * @param type the Directory type
118 * @return true if the metadata directory has been created
119 */
120 public boolean containsDirectory(Class type)
121 {
122 return directoryMap.containsKey(type);
123 }
124}
Note: See TracBrowser for help on using the repository browser.