All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.abubusoft.kripton.annotation.BindAdapter Maven / Gradle / Ivy

There is a newer version: 8.2.0-rc.4
Show newest version
/*******************************************************************************
 * Copyright 2015, 2017 Francesco Benincasa ([email protected]).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.abubusoft.kripton.annotation;

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

import com.abubusoft.kripton.TypeAdapter;

/**
 * 

* Defines TypeAdapter to use to the field. *

* *

* This kind of adapter is applied from java to data format (JSON/XML/etc) and * viceversa. It is not used in SharedPreference and SQLite * generation. *

* *

* Every supported java type in Kripton has a specific representation in its * persisted state. This annotation allows to persist a field of a specific type * in can be applied to a public field or a field with getter and setter. It is * not used in SharedPreference and SQLite generation. *

*

* For example: *

* *
 * @BindType
 * public class Bean {
 * 
 * 	@BindXml(xmlType = XmlType.ATTRIBUTE)
 * 	public String description;
 * 
 * 	@BindXml(xmlType = XmlType.ATTRIBUTE)
 * 	public int id;
 * 
 * 	public Date date;
 * }
 * 
*

* Its rapresentation in JSON: *

* *
 * {"date":"2017-01-23T00:35:24.728Z","description":"hello","id":0}
 * 
* * Its rapresentation in XML (indented for readibility reasons): * *
 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bean description="hello" id="0">
  <date>2017-01-23T00:35:24.728Z</date>
</bean>
 * 
* * As you notice, field date is persisted like a string * 2017-01-23T00:35:24.728Z. To persist date as a long, just change annotations * and write an adapter class named DateAdapter: * *
 * @BindType
 * public class Bean {
 * 
 * 	@BindXml(xmlType = XmlType.ATTRIBUTE)
 * 	public String description;
 * 
 * 	@BindXml(xmlType = XmlType.ATTRIBUTE)
 * 	public int id;
 * 
 * 	@BindAdapter(adapter = DateAdapter.class, dataType = Long.class)
 * 	public Date date;
 * }
 * 
* * *
 * public class DateAdapter implements BindTypeAdapter<Date, Long> {
 * 
 * 	@Override
 * 	public Date toJava(Long dataValue) throws Exception {
 * 		return new Date(dataValue);
 * 	}
 * 
 * 	@Override
 * 	public Long toData(Date javaValue) throws Exception {
 * 		return javaValue.getTime();
 * 	}
 * }
 * 
* * * Bean's rapresentation in JSON: * *
 * {"date":1485132009409,"description":"hello","id":0}
 * 
* * Its rapresentation in XML (indented for readibility reasons): * *
 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 *  <bean description="hello" id="0">
 *    <date>1485132009409</date>
 *  </bean>
 * 
* * @author Francesco Benincasa ([email protected]) * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface BindAdapter { /** * TypeAdapter used to convert data * * @return instance of class converter */ public Class> adapter(); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy