1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ConverterReader.java,v 1.1 2008/02/07 17:12:27 mark Exp $
7 */
8
9package com.sleepycat.persist.impl;
10
11import com.sleepycat.persist.evolve.Converter;
12import com.sleepycat.persist.raw.RawObject;
13
14/**
15 * Reader for invoking a class Converter mutation.
16 *
17 * @author Mark Hayes
18 */
19public class ConverterReader implements Reader {
20
21    private static final long serialVersionUID = -305788321064984348L;
22
23    private Converter converter;
24    private transient Format oldFormat;
25
26    ConverterReader(Converter converter) {
27        this.converter = converter;
28    }
29
30    public void initializeReader(Catalog catalog,
31                                 int initVersion,
32                                 Format oldFormat) {
33        this.oldFormat = oldFormat;
34    }
35
36    public Object newInstance(EntityInput input, boolean rawAccess) {
37        /* Create the old format RawObject. */
38        return oldFormat.newInstance(input, true);
39    }
40
41    public void readPriKey(Object o, EntityInput input, boolean rawAccess) {
42        /* Read the old format RawObject's primary key. */
43        oldFormat.readPriKey(o, input, true);
44    }
45
46    public Object readObject(Object o, EntityInput input, boolean rawAccess) {
47        Catalog catalog = input.getCatalog();
48
49        /* Read the old format RawObject and convert it. */
50        o = oldFormat.readObject(o, input, true);
51        o = converter.getConversion().convert(o);
52
53        /* Convert the current format RawObject to a live Object. */
54        if (!rawAccess && o instanceof RawObject) {
55            o = catalog.convertRawObject((RawObject) o, null);
56        }
57        return o;
58    }
59}
60