001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.flatfile.dsl;
018
019import org.apache.commons.lang3.ClassUtils;
020import org.apache.commons.lang3.StringUtils;
021
022/**
023 * Default implementation.
024 * @version $Revision: 1301246 $ $Date: 2012-03-15 17:18:16 -0500 (Thu, 15 Mar 2012) $
025 */
026public class DefaultEntityNameStrategy implements EntityNameStrategy {
027    private static final String NULL = "null";
028
029    /**
030     * If <code>o</code> is {@code null}, returns {@literal "null"}.
031     * If <code>o</code> is a {@link CharSequence}, its {@link String} value is returned.
032     * If <code>o</code> is a {@link Class}, its uncapitalized short classname is returned.
033     * Otherwise returns uncapitalized short classname of {@code o}.
034     * @param o Object.
035     * @return String entity name.
036     * @see ClassUtils#getShortClassName(Class)
037     */
038    public String getEntityName(Object o) {
039        if (o == null) {
040            return NULL;
041        }
042        if (o instanceof CharSequence) {
043            return o.toString();
044        }
045        final Class<?> c = o instanceof Class<?> ? (Class<?>) o : o.getClass();
046        return StringUtils.uncapitalize(ClassUtils.getShortClassName(c));
047    }
048
049}