com.alibaba.otter.shared.common.utils.OtterToStringStyle.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.otter.shared.common.utils.OtterToStringStyle.java

Source

/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * 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.alibaba.otter.shared.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.builder.ToStringStyle;

/**
 * OtterToStringStyle
 * 
 * <pre>
 * Style?
 * Person[name=John Doe,age=33,smoker=false ,time=2010-04-01 00:00:00]
 * </pre>
 * 
 * @author jianghang 2010-6-18 ?11:35:27
 */
public class OtterToStringStyle extends ToStringStyle {

    private static final long serialVersionUID = -6568177374288222145L;

    private static final String DEFAULT_TIME = "yyyy-MM-dd HH:mm:ss";
    private static final String DEFAULT_DAY = "yyyy-MM-dd";

    /**
     * <pre>
     * ?
     * Person[name=John Doe,age=33,smoker=false ,time=2010-04-01 00:00:00]
     * </pre>
     */
    public static final ToStringStyle TIME_STYLE = new OtterDateStyle(DEFAULT_TIME);

    /**
     * <pre>
     * ?
     * Person[name=John Doe,age=33,smoker=false ,day=2010-04-01]
     * </pre>
     */
    public static final ToStringStyle DAY_STYLE = new OtterDateStyle(DEFAULT_DAY);

    /**
     * <pre>
     * ?
     * Person[name=John Doe,age=33,smoker=false ,time=2010-04-01 00:00:00]
     * </pre>
     */
    public static final ToStringStyle DEFAULT_STYLE = OtterToStringStyle.TIME_STYLE;

    // =========================== style =============================

    /**
     * ??ToStringStyle
     * 
     * @author li.jinl
     */
    private static class OtterDateStyle extends ToStringStyle {

        private static final long serialVersionUID = 5208917932254652886L;

        // format?
        private String pattern;

        public OtterDateStyle(String pattern) {
            super();
            this.setUseShortClassName(true);
            this.setUseIdentityHashCode(false);
            // format?
            this.pattern = pattern;
        }

        protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
            // date?
            if (value instanceof Date) {
                value = new SimpleDateFormat(pattern).format(value);
            }
            // ???
            buffer.append(value);
        }
    }
}