com.landawn.abacus.parser.AbstractParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-util-se Show documentation
Show all versions of abacus-util-se Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (C) 2015 HaiYang Li
*
* 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.landawn.abacus.parser;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import com.landawn.abacus.exception.ParseException;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.N;
/**
* The Class AbstractParser.
*
* @author Haiyang Li
* @param
* @param
* @since 0.8
*/
abstract class AbstractParser, DC extends DeserializationConfig>> implements Parser {
/** The Constant logger. */
private static final Logger logger = LoggerFactory.getLogger(AbstractParser.class);
/** The element separator. */
static final String ELEMENT_SEPARATOR = ", ".intern();
/** The element separator char array. */
static final char[] ELEMENT_SEPARATOR_CHAR_ARRAY = ELEMENT_SEPARATOR.toCharArray();
/** The Constant NULL_STRING. */
static final String NULL_STRING = "null".intern();
/** The Constant NULL_CHAR_ARRAY. */
static final char[] NULL_CHAR_ARRAY = NULL_STRING.toCharArray();
/** The Constant TRUE. */
static final String TRUE = Boolean.TRUE.toString().intern();
/** The Constant TRUE_CHAR_ARRAY. */
static final char[] TRUE_CHAR_ARRAY = TRUE.toCharArray();
/** The Constant FALSE. */
static final String FALSE = Boolean.FALSE.toString().intern();
/** The Constant FALSE_CHAR_ARRAY. */
static final char[] FALSE_CHAR_ARRAY = FALSE.toCharArray();
// ... it has to be big enough to make it's safety to add element to
/** The Constant POOL_SIZE. */
// ArrayBlockingQueue.
static final int POOL_SIZE = 1000;
/** The Constant CLS_POOL_SIZE. */
static final int CLS_POOL_SIZE = 3000;
/**
*
* @param obj
* @return
*/
@Override
public String serialize(Object obj) {
return serialize(obj, null);
}
/**
*
* @param file
* @param obj
*/
@Override
public void serialize(File file, Object obj) {
serialize(file, obj, null);
}
/**
*
* @param os
* @param obj
*/
@Override
public void serialize(OutputStream os, Object obj) {
serialize(os, obj, null);
}
/**
*
* @param writer
* @param obj
*/
@Override
public void serialize(Writer writer, Object obj) {
serialize(writer, obj, null);
}
/**
*
* @param
* @param targetClass
* @param st
* @return
*/
@Override
public T deserialize(Class targetClass, String st) {
return deserialize(targetClass, st, null);
}
/**
*
* @param
* @param targetClass
* @param file
* @return
*/
@Override
public T deserialize(Class targetClass, File file) {
return deserialize(targetClass, file, null);
}
/**
*
* @param
* @param targetClass
* @param is
* @return
*/
@Override
public T deserialize(Class targetClass, InputStream is) {
return deserialize(targetClass, is, null);
}
/**
*
* @param
* @param targetClass
* @param reader
* @return
*/
@Override
public T deserialize(Class targetClass, Reader reader) {
return deserialize(targetClass, reader, null);
}
/**
* New prop instance.
*
* @param
* @param propClass
* @param attribeTypeClass
* @return
*/
@SuppressWarnings("unchecked")
protected static T newPropInstance(Class> propClass, Class> attribeTypeClass) {
if ((attribeTypeClass != null) && ((propClass == null) || propClass.isAssignableFrom(attribeTypeClass))) {
try {
return (T) N.newInstance(attribeTypeClass);
} catch (Exception e) {
if (logger.isInfoEnabled()) {
logger.info("Failed to new instance by type attribute: " + attribeTypeClass.getCanonicalName());
}
}
}
if (propClass != null) {
return (T) N.newInstance(propClass);
}
throw new ParseException("Failed to create property instance with property class: " + propClass + " and attribute " + attribeTypeClass);
}
/**
* Gets the concrete class.
*
* @param targetClass
* @param typeClass
* @return
*/
protected static Class> getConcreteClass(Class> targetClass, Class> typeClass) {
if (typeClass == null) {
return targetClass;
} else if (targetClass == null) {
return typeClass;
} else if (targetClass == typeClass || targetClass.isAssignableFrom(typeClass)) {
return typeClass;
} else {
return targetClass;
}
}
/**
* Collection 2 array.
*
* @param
* @param targetClass
* @param c
* @return
*/
protected static T collection2Array(Class> targetClass, final Collection> c) {
if (c == null) {
return null;
}
final Type> targetType = N.typeOf(targetClass);
if (targetType.isPrimitiveArray()) {
return (T) targetType.collection2Array(c);
} else {
// looking for the right array class.
for (Object e : c) {
if (e != null) {
if (targetClass.getComponentType().isAssignableFrom(e.getClass())) {
return (T) targetType.collection2Array(c);
} else {
return (T) c.toArray((Object[]) N.newArray(e.getClass(), c.size()));
}
}
}
}
return (T) targetType.collection2Array(c);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy