001/* 002 * Copyright 2023 the original author or authors. 003 * <p> 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 * <p> 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * <p> 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 de.cuioss.tools.io; 017 018import java.io.File; 019import java.io.Serializable; 020import java.nio.file.Path; 021 022import de.cuioss.tools.string.Splitter; 023import lombok.EqualsAndHashCode; 024import lombok.Getter; 025import lombok.ToString; 026 027/** 028 * Helper class for splitting / interacting with Filename and corresponding 029 * suffixes. 030 * 031 * @author Oliver Wolff 032 */ 033@EqualsAndHashCode 034@ToString 035public class StructuredFilename implements Serializable { 036 037 private static final long serialVersionUID = 7473756881958645393L; 038 039 @Getter 040 private final String originalName; 041 042 @Getter 043 private final String namePart; 044 045 @Getter 046 private final String suffix; 047 048 /** 049 * Constructor. 050 * 051 * @param filename to be checked 052 */ 053 @SuppressWarnings("squid:S1871") // owolff: Although duplicate code for case 0 and case 1 I find 054 // it better readable 055 public StructuredFilename(final String filename) { 056 originalName = filename; 057 final var list = Splitter.on(".").omitEmptyStrings().splitToList(filename); 058 switch (list.size()) { 059 case 0: 060 namePart = filename; 061 suffix = null; 062 break; 063 case 1: 064 namePart = filename; 065 suffix = null; 066 break; 067 case 2: 068 namePart = list.get(0); 069 suffix = list.get(1); 070 break; 071 default: 072 suffix = list.get(list.size() - 1); 073 namePart = String.join(".", list.subList(0, list.size() - 1)); 074 break; 075 } 076 } 077 078 /** 079 * Constructor 080 * 081 * @param path to be used as source, must not be null 082 */ 083 public StructuredFilename(final Path path) { 084 this(path.getName(path.getNameCount() - 1).toString()); 085 } 086 087 /** 088 * Constructor 089 * 090 * @param file to be used as source, must not be null 091 */ 092 public StructuredFilename(final File file) { 093 this(file.toPath()); 094 } 095 096 /** 097 * @param nameSuffix to be used for appending the name part. may be null 098 * @return the created name string. if namePart = "namePart", 099 * nameSuffix=".nameSuffix" and suffix = "suffix" the resulting String 100 * will be "namePart.nameSuffix.suffix" 101 */ 102 public String getAppendedName(final String nameSuffix) { 103 final var builder = new StringBuilder(namePart); 104 if (null != nameSuffix) { 105 builder.append(nameSuffix); 106 } 107 if (null != suffix) { 108 builder.append('.'); 109 builder.append(suffix); 110 } 111 return builder.toString(); 112 } 113}