001/*
002 * Copyright 2006-2007 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.item.util;
017
018import org.springframework.batch.item.ExecutionContext;
019import org.springframework.util.Assert;
020
021/**
022 * Facilitates assigning names to objects persisting data in {@link ExecutionContext} and generating keys for
023 * {@link ExecutionContext} based on the name.
024 * 
025 * @author Robert Kasanicky
026 */
027public class ExecutionContextUserSupport {
028
029        private String name;
030
031        public ExecutionContextUserSupport() {
032                super();
033        }
034
035        public ExecutionContextUserSupport(String name) {
036                super();
037                this.name = name;
038        }
039
040        /**
041         * @return name used to uniquely identify this instance's entries in shared context.
042         */
043        protected String getName() {
044                return this.name;
045        }
046
047        /**
048         * @param name unique name used to create execution context keys.
049         */
050        public void setName(String name) {
051                this.name = name;
052        }
053
054        /**
055         * Prefix the argument with {@link #getName()} to create a unique key that can be safely used to identify data
056         * stored in {@link ExecutionContext}.
057         *
058         * @param suffix {@link String} to be used to generate the key.
059         * @return the key that was generated based on the name and the suffix.
060         */
061        public String getKey(String suffix) {
062                Assert.hasText(name, "Name must be assigned for the sake of defining the execution context keys prefix.");
063                return name + "." + suffix;
064        }
065
066}