/* Copyright (c) 2008, Nathan Sweet
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
 * conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo.serializers;

import static com.esotericsoftware.minlog.Log.*;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/** Serializes objects using direct field assignment, with versioning backward compatibility. Fields can be added without
 * invalidating previously serialized bytes. Note that removing, renaming or changing the type of a field is not supported. In
 * addition, forward compatibility is not supported.
 * <p>
 * There is a little additional overhead compared to {@link FieldSerializer}. A version varint is written before each object. When
 * deserializing, the input version will be examined to decide what fields are not in the input.
 * @author Tianyi HE <hty0807@gmail.com> */
public class VersionFieldSerializer<T> extends FieldSerializer<T> {
	private int[] fieldVersion; // version of each field
	private int typeVersion = 0; // version of current type

	public VersionFieldSerializer (Kryo kryo, Class type) {
		super(kryo, type);
		// Make sure this is done before any read / write operations.
		initializeCachedFields();
	}

	@Override
	protected void initializeCachedFields () {
		CachedField[] fields = getFields();
		fieldVersion = new int[fields.length];
		for (int i = 0, n = fields.length; i < n; i++) {
			Field field = fields[i].getField();
			if (field.getAnnotation(Since.class) != null) {
				fieldVersion[i] = field.getAnnotation(Since.class).value();
				// use maximum version among fields as type version
				typeVersion = Math.max(fieldVersion[i], typeVersion);
			} else {
				fieldVersion[i] = 0;
			}
		}
		this.removedFields.clear();
		if (DEBUG) debug("Version for type " + getType().getName() + " is " + typeVersion);
	}

	@Override
	public void removeField (String fieldName) {
		super.removeField(fieldName);
		initializeCachedFields();
	}

	@Override
	public void removeField (CachedField field) {
		super.removeField(field);
		initializeCachedFields();
	}

	@Override
	public void write (Kryo kryo, Output output, T object) {
		CachedField[] fields = getFields();
		// Write type version.
		output.writeVarInt(typeVersion, true);
		// Write fields.
		for (int i = 0, n = fields.length; i < n; i++) {
			fields[i].write(output, object);
		}
	}

	@Override
	public T read (Kryo kryo, Input input, Class<T> type) {
		T object = create(kryo, input, type);
		kryo.reference(object);

		// Read input version.
		int version = input.readVarInt(true);
		CachedField[] fields = getFields();
		for (int i = 0, n = fields.length; i < n; i++) {
			// Field is not present in input, skip it.
			if (fieldVersion[i] > version) {
				if (DEBUG) debug("Skip field " + fields[i].getField().getName());
				continue;
			}
			fields[i].read(input, object);
		}
		return object;
	}

	/** Incremental modification of serialized objects must add {@link Since} for new fields. */
	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.FIELD)
	public @interface Since {
		/** Version of annotated field, default is 0, and must be incremental to maintain compatibility. */
		int value() default 0;
	}
}

@@ -2610,29 +2610,30 @@
  int
-%5B%5D field
+ type
 Version
+ = 0
 ; // 
-v
+V
 ersi
@@ -2642,18 +2642,21 @@
  of 
-each field
+current type.
 %0A%09pr
@@ -2668,30 +2668,29 @@
  int
- type
+%5B%5D field
 Version
- = 0
 ; // 
-v
+V
 ersi
@@ -2695,28 +2695,27 @@
 sion of 
-current type
+each field.
 %0A%0A%09publi
@@ -3105,28 +3105,38 @@
 Field();%0A%09%09%09
-if (
+Since since = 
 field.getAnn
@@ -3155,16 +3155,30 @@
 e.class)
+;%0A%09%09%09if (since
  != null
@@ -3207,40 +3207,13 @@
 %5D = 
-field.getAnnotation(Since.class)
+since
 .val
@@ -3225,18 +3225,22 @@
 %0A%09%09%09%09// 
-us
+Use th
 e maximu
@@ -3265,28 +3265,41 @@
 elds as 
+the entire 
 type
+'s
  version
 %0A%09%09%09%09typ
@@ -3290,16 +3290,17 @@
  version
+.
 %0A%09%09%09%09typ

Clean up.