org.codelabor.system.file.anyframe.idgen.UniqueFilenameStrategy.java Source code

Java tutorial

Introduction

Here is the source code for org.codelabor.system.file.anyframe.idgen.UniqueFilenameStrategy.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.codelabor.system.file.anyframe.idgen;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.anyframe.idgen.IdGenStrategy;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  ? 
 * 
 * @author Shin Sang-jae
 * 
 */
public class UniqueFilenameStrategy implements IdGenStrategy {

    private Logger logger = LoggerFactory.getLogger(UniqueFilenameStrategy.class);

    /**
     * ?
     */
    protected String prefix;

    /**
     * ? 
     */
    protected int cipers;

    /**
     *   
     */
    protected String dateAndTimePattern;

    /**
     *  ?
     */
    protected char fillChar;

    /**
     * ?
     */
    protected String delimiter;

    /**
     * ?  .
     * 
     * @param cipers
     *            ? 
     */
    public void setCipers(int cipers) {
        this.cipers = cipers;
    }

    /**
     *   ? .
     * 
     * @param dateAndTimePattern
     *              
     */
    public void setDateAndTimePattern(String dateAndTimePattern) {
        this.dateAndTimePattern = dateAndTimePattern;
    }

    /**
     *  ? .
     * 
     * @param fillChar
     *             ?
     */
    public void setFillChar(char fillChar) {
        this.fillChar = fillChar;
    }

    /**
     * ? .
     * 
     * @param delimiter
     *            ?
     */
    public void setDelimiter(String delimiter) {
        this.delimiter = delimiter;
    }

    /**
     * ? .
     * 
     * @param prefix
     *            ?
     */
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    /*
     * (non-Javadoc)
     * 
     * @see anyframe.core.idgen.IdGenStrategy#makeId(java.lang.String)
     */
    public String makeId(String originalId) {
        DateFormat dateFormat = new SimpleDateFormat(dateAndTimePattern, Locale.getDefault());
        String dateFormatString = dateFormat.format(System.currentTimeMillis());
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        if (delimiter != null) {
            sb.append(delimiter);
        }
        sb.append(dateFormatString);
        if (delimiter != null) {
            sb.append(delimiter);
        }
        String trimedId = null;
        if (originalId.length() > cipers) {
            trimedId = originalId.substring(0, cipers);
        } else {
            trimedId = originalId;
        }
        sb.append(StringUtils.leftPad(trimedId, cipers, fillChar));
        logger.debug("id: {}", sb.toString());

        return sb.toString();
    }
}