View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.hl7v2.storage;
17  
18  import net.sf.ehcache.Ehcache;
19  import net.sf.ehcache.Element;
20  import org.apache.commons.lang3.Validate;
21  
22  import static java.util.Objects.requireNonNull;
23  
24  /**
25   * A storage of HL7 v2 unsolicited fragmentation accumulators.
26   * @author Dmytro Rud
27   */
28  public class EhcacheUnsolicitedFragmentationStorage implements UnsolicitedFragmentationStorage {
29  
30      private final Ehcache ehcache;
31  
32      public EhcacheUnsolicitedFragmentationStorage(Ehcache ehcache) {
33          requireNonNull(ehcache);
34          this.ehcache = ehcache;
35      }
36  
37      public void put(String key, StringBuilder accumulator) {
38          Element element = new Element(key, accumulator);
39          ehcache.put(element);
40      }
41  
42      public StringBuilder getAndRemove(String key) {
43          Element element = ehcache.get(key);
44          if (element != null) {
45              ehcache.remove(key);
46              return (StringBuilder) element.getObjectValue();
47          }
48          return null;
49      }
50  }