001/*
002 * Copyright 2006-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.launch.support;
017
018import org.springframework.batch.core.JobParameters;
019import org.springframework.batch.core.JobParametersBuilder;
020import org.springframework.batch.core.JobParametersIncrementer;
021import org.springframework.lang.Nullable;
022
023/**
024 * @author Dave Syer
025 * @author Mahmoud Ben Hassine
026 */
027public class RunIdIncrementer implements JobParametersIncrementer {
028
029        private static String RUN_ID_KEY = "run.id";
030
031        private String key = RUN_ID_KEY;
032
033        /**
034         * The name of the run id in the job parameters.  Defaults to "run.id".
035         *
036         * @param key the key to set
037         */
038        public void setKey(String key) {
039                this.key = key;
040        }
041
042        /**
043         * Increment the run.id parameter (starting with 1).
044         */
045        @Override
046        public JobParameters getNext(@Nullable JobParameters parameters) {
047
048                JobParameters params = (parameters == null) ? new JobParameters() : parameters;
049
050                long id = params.getLong(key, 0L) + 1;
051                return new JobParametersBuilder(params).addLong(key, id).toJobParameters();
052        }
053
054}