IPath.java

  1. /*
  2.  * @cond LICENSE
  3.  * ######################################################################################
  4.  * # LGPL License                                                                       #
  5.  * #                                                                                    #
  6.  * # This file is part of the LightJason AgentSpeak(L++)                                #
  7.  * # Copyright (c) 2015-19, LightJason (info@lightjason.org)                            #
  8.  * # This program is free software: you can redistribute it and/or modify               #
  9.  * # it under the terms of the GNU Lesser General Public License as                     #
  10.  * # published by the Free Software Foundation, either version 3 of the                 #
  11.  * # License, or (at your option) any later version.                                    #
  12.  * #                                                                                    #
  13.  * # This program is distributed in the hope that it will be useful,                    #
  14.  * # but WITHOUT ANY WARRANTY; without even the implied warranty of                     #
  15.  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                      #
  16.  * # GNU Lesser General Public License for more details.                                #
  17.  * #                                                                                    #
  18.  * # You should have received a copy of the GNU Lesser General Public License           #
  19.  * # along with this program. If not, see http://www.gnu.org/licenses/                  #
  20.  * ######################################################################################
  21.  * @endcond
  22.  */

  23. package org.lightjason.agentspeak.common;

  24. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

  25. import javax.annotation.Nonnull;
  26. import java.io.Serializable;
  27. import java.util.stream.Stream;


  28. /**
  29.  * interface of a path
  30.  *
  31.  * @note implement equals with String object, so a path object can be checked to a String
  32.  */
  33. public interface IPath extends Serializable, Comparable<IPath>
  34. {
  35.     /**
  36.      * default seperator
  37.      */
  38.     String DEFAULTSEPERATOR = "/";

  39.     /**
  40.      * empty path
  41.      **/
  42.     IPath EMPTY = new IPath()
  43.     {
  44.         /**
  45.          * serial id
  46.          */
  47.         private static final long serialVersionUID = -8529008893337445887L;

  48.         @Nonnull
  49.         @Override
  50.         public final IPath append( @Nonnull final IPath p_path )
  51.         {
  52.             return this;
  53.         }

  54.         @Nonnull
  55.         @Override
  56.         public final IPath append( @Nonnull final String p_path )
  57.         {
  58.             return this;
  59.         }

  60.         @Nonnull
  61.         @Override
  62.         public final IPath remove( final int p_index )
  63.         {
  64.             return this;
  65.         }

  66.         @Nonnull
  67.         @Override
  68.         public final IPath remove( final int p_start, final int p_end )
  69.         {
  70.             return this;
  71.         }

  72.         @Override
  73.         public final boolean endswith( @Nonnull final IPath p_path )
  74.         {
  75.             return false;
  76.         }

  77.         @Override
  78.         public final boolean empty()
  79.         {
  80.             return true;
  81.         }

  82.         @Nonnull
  83.         @Override
  84.         public final String get( final int p_index )
  85.         {
  86.             return "";
  87.         }

  88.         @Nonnull
  89.         @Override
  90.         public final String path( final String p_separator )
  91.         {
  92.             return "";
  93.         }

  94.         @Nonnull
  95.         @Override
  96.         public final String path()
  97.         {
  98.             return "";
  99.         }

  100.         @Nonnull
  101.         @Override
  102.         public final String separator()
  103.         {
  104.             return "";
  105.         }

  106.         @Nonnull
  107.         @Override
  108.         public final IPath separator( @Nonnull final String p_separator )
  109.         {
  110.             return this;
  111.         }

  112.         @Nonnull
  113.         @Override
  114.         public final IPath lower()
  115.         {
  116.             return this;
  117.         }

  118.         @Nonnull
  119.         @Override
  120.         public final IPath upper()
  121.         {
  122.             return this;
  123.         }

  124.         @Nonnull
  125.         @Override
  126.         public final IPath subpath( final int p_fromindex )
  127.         {
  128.             return this;
  129.         }

  130.         @Nonnull
  131.         @Override
  132.         public final IPath subpath( final int p_fromindex, final int p_toindex )
  133.         {
  134.             return this;
  135.         }

  136.         @Nonnull
  137.         @Override
  138.         public final String suffix()
  139.         {
  140.             return "";
  141.         }

  142.         @Nonnull
  143.         @Override
  144.         public final IPath pushback( @Nonnull final IPath p_path )
  145.         {
  146.             return this;
  147.         }

  148.         @Nonnull
  149.         @Override
  150.         public final IPath pushback( @Nonnull final String p_path )
  151.         {
  152.             return this;
  153.         }

  154.         @Nonnull
  155.         @Override
  156.         public final IPath pushfront( @Nonnull final String p_path )
  157.         {
  158.             return this;
  159.         }

  160.         @Nonnull
  161.         @Override
  162.         public final IPath pushfront( @Nonnull final IPath p_path )
  163.         {
  164.             return this;
  165.         }

  166.         @Nonnull
  167.         @Override
  168.         public final String removesuffix()
  169.         {
  170.             return "";
  171.         }

  172.         @Nonnull
  173.         @Override
  174.         public final IPath reverse()
  175.         {
  176.             return this;
  177.         }

  178.         @Override
  179.         public final int size()
  180.         {
  181.             return 0;
  182.         }

  183.         @Override
  184.         public final boolean startswith( final IPath p_path )
  185.         {
  186.             return false;
  187.         }

  188.         @Override
  189.         public final boolean startswith( final String p_path )
  190.         {
  191.             return false;
  192.         }

  193.         @Nonnull
  194.         @Override
  195.         public final Stream<String> stream()
  196.         {
  197.             return Stream.empty();
  198.         }

  199.         @Override
  200.         public final int compareTo( @Nonnull final IPath p_path )
  201.         {
  202.             return Integer.compare( p_path.hashCode(), this.hashCode() );
  203.         }

  204.         @Override
  205.         public final int hashCode()
  206.         {
  207.             return 0;
  208.         }

  209.         @Override
  210.         @SuppressFBWarnings( "EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS" )
  211.         public final boolean equals( final Object p_object )
  212.         {
  213.             return ( ( p_object instanceof IPath ) && ( this.hashCode() == p_object.hashCode() ) )
  214.                    || ( ( p_object instanceof String ) && ( this.path().hashCode() == p_object.hashCode() ) );
  215.         }
  216.     };

  217.     /**
  218.      * appends a path at the current and returns a new object
  219.      *
  220.      * @param p_path path
  221.      * @return new path
  222.      */
  223.     @Nonnull
  224.     IPath append( @Nonnull final IPath p_path );

  225.     /**
  226.      * appends a string at the current path and returns the new object
  227.      *
  228.      * @param p_path string with path
  229.      * @return new path
  230.      */
  231.     @Nonnull
  232.     IPath append( @Nonnull final String p_path );

  233.     /**
  234.      * removes an element
  235.      *
  236.      * @param p_index index position
  237.      * @return return the changed object
  238.      */
  239.     @Nonnull
  240.     IPath remove( final int p_index );

  241.     /**
  242.      * removes all elements from start index until end index (exclusive)
  243.      *
  244.      * @param p_start start index
  245.      * @param p_end end index (exclusive)
  246.      * @return return the changed object
  247.      */
  248.     @Nonnull
  249.     IPath remove( final int p_start, final int p_end );

  250.     /**
  251.      * check of a path ends with another path
  252.      *
  253.      * @param p_path path
  254.      * @return boolean
  255.      */
  256.     boolean endswith( @Nonnull final IPath p_path );

  257.     /**
  258.      * check if the path is empty
  259.      *
  260.      * @return empty flag
  261.      */
  262.     boolean empty();

  263.     /**
  264.      * returns an part of the path
  265.      *
  266.      * @param p_index index position (negativ index is element from the end)
  267.      * @return element
  268.      */
  269.     @Nonnull
  270.     String get( final int p_index );

  271.     /**
  272.      * returns the full path as string with an individual separator
  273.      *
  274.      * @param p_separator separator
  275.      * @return string path
  276.      */
  277.     @Nonnull
  278.     String path( final String p_separator );

  279.     /**
  280.      * returns the full path as string
  281.      *
  282.      * @return string path
  283.      */
  284.     @Nonnull
  285.     String path();

  286.     /**
  287.      * returns the separator
  288.      *
  289.      * @return separator
  290.      */
  291.     @Nonnull
  292.     String separator();

  293.     /**
  294.      * sets the separator
  295.      *
  296.      * @param p_separator separator
  297.      * @return path object
  298.      */
  299.     @Nonnull
  300.     IPath separator( @Nonnull final String p_separator );

  301.     /**
  302.      * changes all elements to lower-case
  303.      *
  304.      * @return object
  305.      */
  306.     @Nonnull
  307.     IPath lower();

  308.     /**
  309.      * changes all elements to uppercase
  310.      *
  311.      * @return object
  312.      */
  313.     @Nonnull
  314.     IPath upper();

  315.     /**
  316.      * creates a path of the start index until the end
  317.      *
  318.      * @param p_fromindex start index
  319.      * @return path
  320.      */
  321.     @Nonnull
  322.     IPath subpath( final int p_fromindex );

  323.     /**
  324.      * creates a path of the indices
  325.      *
  326.      * @param p_fromindex start index
  327.      * @param p_toindex end index (exclusive) / negative values from the end
  328.      * @return path
  329.      */
  330.     @Nonnull
  331.     IPath subpath( final int p_fromindex, final int p_toindex );

  332.     /**
  333.      * returns the last part of the path
  334.      *
  335.      * @return string
  336.      */
  337.     @Nonnull
  338.     String suffix();

  339.     /**
  340.      * adds a path at the end
  341.      *
  342.      * @param p_path path
  343.      * @return return the changed object
  344.      */
  345.     @Nonnull
  346.     IPath pushback( @Nonnull final IPath p_path );

  347.     /**
  348.      * adds a path at the end
  349.      *
  350.      * @param p_path string path
  351.      * @return return the changed object
  352.      */
  353.     @Nonnull
  354.     IPath pushback( @Nonnull final String p_path );

  355.     /**
  356.      * adds a path at the front
  357.      *
  358.      * @param p_path string path
  359.      * @return return the changed object
  360.      */
  361.     @Nonnull
  362.     IPath pushfront( @Nonnull final String p_path );

  363.     /**
  364.      * adds a path to the front of the path
  365.      *
  366.      * @param p_path path
  367.      * @return return the changed object
  368.      */
  369.     @Nonnull
  370.     IPath pushfront( @Nonnull final IPath p_path );

  371.     /**
  372.      * remove the suffix from the path
  373.      *
  374.      * @return last item of the path
  375.      */
  376.     @Nonnull
  377.     String removesuffix();

  378.     /**
  379.      * reverse path
  380.      *
  381.      * @return return the changed object
  382.      */
  383.     @Nonnull
  384.     IPath reverse();

  385.     /**
  386.      * returns the number of path elements
  387.      *
  388.      * @return size
  389.      */
  390.     int size();

  391.     /**
  392.      * check of a path starts with another path
  393.      *
  394.      * @param p_path path
  395.      * @return boolean
  396.      */
  397.     boolean startswith( final IPath p_path );

  398.     /**
  399.      * check of a path starts with another path
  400.      *
  401.      * @param p_path path
  402.      * @return boolean
  403.      */
  404.     boolean startswith( final String p_path );

  405.     /**
  406.      * stream over elements
  407.      *
  408.      * @return sequential stream
  409.      */
  410.     @Nonnull
  411.     Stream<String> stream();

  412. }