1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ReadOnlyCatalog.java,v 1.1 2008/02/07 17:12:27 mark Exp $
7 */
8
9package com.sleepycat.persist.impl;
10
11import java.util.IdentityHashMap;
12import java.util.List;
13import java.util.Map;
14import java.util.NoSuchElementException;
15
16import com.sleepycat.persist.raw.RawObject;
17
18/**
19 * Read-only catalog operations used when initializing new formats.  This
20 * catalog is used temprarily when the main catalog has not been updated yet,
21 * but the new formats need to do catalog lookups.
22 *
23 * @see PersistCatalog#addNewFormat
24 *
25 * @author Mark Hayes
26 */
27class ReadOnlyCatalog implements Catalog {
28
29    private List<Format> formatList;
30    private Map<String,Format> formatMap;
31
32    ReadOnlyCatalog(List<Format> formatList, Map<String,Format> formatMap) {
33        this.formatList = formatList;
34        this.formatMap = formatMap;
35    }
36
37    public int getInitVersion(Format format, boolean forReader) {
38        return Catalog.CURRENT_VERSION;
39    }
40
41    public Format getFormat(int formatId) {
42        try {
43            Format format = formatList.get(formatId);
44            if (format == null) {
45                throw new IllegalStateException
46                    ("Format does not exist: " + formatId);
47            }
48            return format;
49        } catch (NoSuchElementException e) {
50            throw new IllegalStateException
51                ("Format does not exist: " + formatId);
52        }
53    }
54
55    public Format getFormat(Class cls) {
56        Format format = formatMap.get(cls.getName());
57        if (format == null) {
58            throw new IllegalArgumentException
59                ("Class is not persistent: " + cls.getName());
60        }
61        return format;
62    }
63
64    public Format getFormat(String className) {
65        return formatMap.get(className);
66    }
67
68    public Format createFormat(String clsName, Map<String,Format> newFormats) {
69        throw new IllegalStateException();
70    }
71
72    public Format createFormat(Class type, Map<String,Format> newFormats) {
73        throw new IllegalStateException();
74    }
75
76    public boolean isRawAccess() {
77        return false;
78    }
79
80    public Object convertRawObject(RawObject o, IdentityHashMap converted) {
81        throw new IllegalStateException();
82    }
83}
84