ModelFile.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. /**
  23.  *
  24.  */
  25. package cern.accsoft.steering.jmad.domain.file;

  26. import cern.accsoft.steering.jmad.modeldefs.io.ModelFileFinder;

  27. import java.util.Optional;

  28. /**
  29.  * This interface represents the description of a file used for a model and provides the information where to find it.
  30.  *
  31.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  32.  */
  33. public interface ModelFile {


  34.     /**
  35.      * Where to search the file? In the repository (or if not found there in the repo-copy within the jar) or in the
  36.      * sourcepath
  37.      *
  38.      * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  39.      */
  40.     enum ModelFileLocation {
  41.         REPOSITORY {
  42.             @Override
  43.             public String getPathOffset(ModelPathOffsets offsets) {
  44.                 return offsets.getRepositoryOffset();
  45.             }

  46.             @Override
  47.             public String getResourcePrefix(ModelPathOffsets offsets) {
  48.                 return Optional.ofNullable(offsets.getRepositoryPrefix()).orElse(DEFAULT_REPOSITORY_PREFIX);
  49.             }
  50.         },
  51.         RESOURCE {
  52.             @Override
  53.             public String getPathOffset(ModelPathOffsets offsets) {
  54.                 return offsets.getResourceOffset();
  55.             }

  56.             @Override
  57.             public String getResourcePrefix(ModelPathOffsets offsets) {
  58.                 return Optional.ofNullable(offsets.getResourcePrefix()).orElse(DEFAULT_RESOURCE_PREFIX);
  59.             }
  60.         };

  61.         private static final String DEFAULT_REPOSITORY_PREFIX = "repdata";
  62.         private static final String DEFAULT_RESOURCE_PREFIX = "resdata";

  63.         public abstract String getPathOffset(ModelPathOffsets offsets);

  64.         public abstract String getResourcePrefix(ModelPathOffsets offsets);
  65.     }


  66.     /**
  67.      * @return the name used by the {@link ModelFileFinder} to find the file.
  68.      */
  69.     String getName();

  70.     /**
  71.      * @return the location where to search for the file.
  72.      */
  73.     ModelFileLocation getLocation();

  74. }