AbstractDataStore.java

  1. /*******************************************************************************
  2.  * Copyright 2012 André Rouél
  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 net.sf.uadetector.datastore;

  17. import java.net.URL;
  18. import java.nio.charset.Charset;

  19. import javax.annotation.Nonnull;

  20. import net.sf.qualitycheck.Check;
  21. import net.sf.uadetector.datareader.DataReader;
  22. import net.sf.uadetector.internal.data.Data;
  23. import net.sf.uadetector.internal.util.UrlUtil;

  24. /**
  25.  * The abstract implementation to store <em>UAS data</em> only in the heap space.<br>
  26.  * <br>
  27.  * A store must always have an usable instance of {@link Data}. It is recommended to initialize it with the supplied UAS
  28.  * file in the <em>uadetector-resources</em> module.
  29.  *
  30.  * @author André Rouél
  31.  */
  32. public abstract class AbstractDataStore implements DataStore {

  33.     /**
  34.      * Runtime check that the passed instance of {@link Data} is not empty (respectively {@link Data#EMPTY}).
  35.      *
  36.      * @param data
  37.      *            instance of {@code Data}
  38.      * @throws IllegalStateException
  39.      *             if the passed instance is empty
  40.      */
  41.     private static Data checkData(final Data data) {
  42.         if (Data.EMPTY.equals(data)) {
  43.             throw new IllegalStateException("Argument 'data' must not be empty.");
  44.         }
  45.         return data;
  46.     }

  47.     /**
  48.      * This method reads the given {@link URL} by using an {@link DataReader}. The new created instance of {@link Data}
  49.      * will be returned.
  50.      *
  51.      * @param reader
  52.      *            data reader to read the given {@code dataUrl}
  53.      * @param url
  54.      *            URL to <em>UAS data</em>
  55.      * @param charset
  56.      *            the character set in which the data should be read
  57.      * @return an instance of {@code Data} or {@link Data#EMPTY} if an error occurred, but never {@code null}
  58.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  59.      *             if the given argument is {@code null}
  60.      */
  61.     protected static final Data readData(@Nonnull final DataReader reader, @Nonnull final URL url, @Nonnull final Charset charset) {
  62.         Check.notNull(reader, "reader");
  63.         Check.notNull(url, "url");
  64.         Check.notNull(charset, "charset");

  65.         return reader.read(url, charset);
  66.     }

  67.     /**
  68.      * Current the character set in which the <em>UAS data</em> will be read
  69.      */
  70.     private final Charset charset;

  71.     /**
  72.      * Current <em>UAS data</em>
  73.      */
  74.     private final Data data;

  75.     /**
  76.      * The {@code URL} to get <em>UAS data</em>
  77.      */
  78.     private final URL dataUrl;

  79.     /**
  80.      * The data reader to read in <em>UAS data</em>
  81.      */
  82.     private final DataReader reader;

  83.     /**
  84.      * The {@code URL} to get the latest version information of <em>UAS data</em>
  85.      */
  86.     private final URL versionUrl;

  87.     /**
  88.      * Constructs an new instance of {@link AbstractDataStore}.
  89.      *
  90.      * @param data
  91.      *            first <em>UAS data</em> which will be available in the store
  92.      * @param reader
  93.      *            data reader to read the given {@code dataUrl}
  94.      * @param dataUrl
  95.      *            URL to <em>UAS data</em>
  96.      * @param versionUrl
  97.      *            URL to version information about the given <em>UAS data</em>
  98.      * @param charset
  99.      *            the character set in which the data should be read
  100.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  101.      *             if one of the given arguments is {@code null}
  102.      */
  103.     protected AbstractDataStore(@Nonnull final Data data, @Nonnull final DataReader reader, @Nonnull final URL dataUrl,
  104.             @Nonnull final URL versionUrl, @Nonnull final Charset charset) {
  105.         Check.notNull(data, "data");
  106.         Check.notNull(reader, "reader");
  107.         Check.notNull(charset, "charset");
  108.         Check.notNull(dataUrl, "dataUrl");
  109.         Check.notNull(versionUrl, "versionUrl");

  110.         this.data = checkData(data);
  111.         this.reader = reader;
  112.         this.dataUrl = dataUrl;
  113.         this.versionUrl = versionUrl;
  114.         this.charset = charset;
  115.     }

  116.     /**
  117.      * Constructs an {@code AbstractDataStore} by reading the given {@code dataUrl} as <em>UAS data</em>.
  118.      *
  119.      * @param reader
  120.      *            data reader to read the given {@code dataUrl}
  121.      * @param dataUrl
  122.      *            URL to <em>UAS data</em>
  123.      * @param versionUrl
  124.      *            URL to version information about the given <em>UAS data</em>
  125.      * @param charset
  126.      *            the character set in which the data should be read
  127.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  128.      *             if one of the given arguments is {@code null}
  129.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  130.      *             if the given strings are not valid URLs
  131.      */
  132.     protected AbstractDataStore(final DataReader reader, final String dataUrl, final String versionUrl, final Charset charset) {
  133.         this(reader, UrlUtil.build(dataUrl), UrlUtil.build(versionUrl), charset);
  134.     }

  135.     /**
  136.      * Constructs an {@code AbstractDataStore} by reading the given {@code dataUrl} as <em>UAS data</em>.
  137.      *
  138.      * @param reader
  139.      *            data reader to read the given {@code dataUrl}
  140.      * @param dataUrl
  141.      *            URL to <em>UAS data</em>
  142.      * @param versionUrl
  143.      *            URL to version information about the given <em>UAS data</em>
  144.      * @param charset
  145.      *            the character set in which the data should be read
  146.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  147.      *             if the given argument is {@code null}
  148.      * @throws net.sf.qualitycheck.exception.IllegalStateOfArgumentException
  149.      *             if the created instance of {@link Data} is empty
  150.      */
  151.     protected AbstractDataStore(final DataReader reader, final URL dataUrl, final URL versionUrl, final Charset charset) {
  152.         this(checkData(readData(reader, dataUrl, charset)), reader, dataUrl, versionUrl, charset);
  153.     }

  154.     @Override
  155.     public Charset getCharset() {
  156.         return charset;
  157.     }

  158.     @Override
  159.     public Data getData() {
  160.         return data;
  161.     }

  162.     @Override
  163.     public DataReader getDataReader() {
  164.         return reader;
  165.     }

  166.     @Override
  167.     public URL getDataUrl() {
  168.         return dataUrl;
  169.     }

  170.     @Override
  171.     public URL getVersionUrl() {
  172.         return versionUrl;
  173.     }

  174. }