| Conditions | 16 |
| Total Lines | 131 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 272 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like com.strider.datadefender.discoverer.FileDiscoverer.discover() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 76 | public List<FileMatchMetaData> discover() |
||
| 77 | throws FileDiscoveryException, |
||
| 78 | DataDefenderException, |
||
| 79 | IOException, |
||
| 80 | SAXException, |
||
| 81 | TikaException { |
||
| 82 | |||
| 83 | List<FileMatchMetaData> finalList = new ArrayList<>(); |
||
| 84 | |||
| 85 | for (final String sm : CollectionUtils.emptyIfNull(config.getModels())) { |
||
| 86 | log.info("********************************"); |
||
| 87 | log.info("Processing model " + sm); |
||
| 88 | log.info("********************************"); |
||
| 89 | |||
| 90 | final Model model = createModel(sm); |
||
| 91 | fileMatches = discoverAgainstSingleModel(model); |
||
| 92 | finalList = ListUtils.union(finalList, fileMatches); |
||
| 93 | } |
||
| 94 | for (final File fm : CollectionUtils.emptyIfNull(config.getFileModels())) { |
||
| 95 | log.info("********************************"); |
||
| 96 | log.info("Processing model " + fm); |
||
| 97 | log.info("********************************"); |
||
| 98 | |||
| 99 | final Model model = createModel(fm); |
||
| 100 | fileMatches = discoverAgainstSingleModel(model); |
||
| 101 | finalList = ListUtils.union(finalList, fileMatches); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Special case |
||
| 105 | List<String> specialCaseFunctions = config.getExtensions(); |
||
| 106 | boolean specialCase = CollectionUtils.isNotEmpty(specialCaseFunctions); |
||
| 107 | |||
| 108 | if (specialCase) { |
||
| 109 | Metadata metadata; |
||
| 110 | try { |
||
| 111 | log.info("**************" + specialCaseFunctions.toString()); |
||
| 112 | for (String fn : CollectionUtils.emptyIfNull(specialCaseFunctions)) { |
||
| 113 | for (final File node : directories) { |
||
| 114 | final List<File> files = (List<File>) FileUtils.listFiles(node, null, true); |
||
| 115 | |||
| 116 | for (final File fich : files) { |
||
| 117 | final String file = fich.getName(); |
||
| 118 | final String recursivedir = fich.getParent(); |
||
| 119 | |||
| 120 | log.info("Analyzing [" + fich.getCanonicalPath() + "]"); |
||
| 121 | final String ext = FilenameUtils.getExtension(fich.getName()).toLowerCase(Locale.ENGLISH); |
||
| 122 | log.debug("Extension: " + ext); |
||
| 123 | |||
| 124 | if (CollectionUtils.emptyIfNull(excludeExtensions).contains(ext)) { |
||
| 125 | log.info("Ignoring type " + ext); |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | final BodyContentHandler handler = new BodyContentHandler(-1); |
||
| 130 | final AutoDetectParser parser = new AutoDetectParser(); |
||
| 131 | |||
| 132 | metadata = new Metadata(); |
||
| 133 | |||
| 134 | String handlerString = ""; |
||
| 135 | try (final InputStream stream = new FileInputStream(fich.getCanonicalPath())) { |
||
| 136 | |||
| 137 | log.debug("Loading data into the stream"); |
||
| 138 | if (stream != null) { |
||
| 139 | parser.parse(stream, handler, metadata); |
||
| 140 | handlerString = handler.toString().replaceAll("( )+", " ").replaceAll("[\\t\\n\\r]+"," "); |
||
| 141 | |||
| 142 | String[] tokens = handlerString.split(" "); |
||
| 143 | |||
| 144 | for (int t=0; t<tokens.length; t++) { |
||
| 145 | String token = tokens[t]; |
||
| 146 | if (token.trim().length() < 1) { |
||
| 147 | continue; |
||
| 148 | } |
||
| 149 | log.info(fn); |
||
| 150 | FileMatchMetaData returnData = null; |
||
| 151 | try { |
||
| 152 | returnData = |
||
| 153 | (FileMatchMetaData)callExtension(new FileMatchMetaData(recursivedir, file), fn, token); |
||
| 154 | } catch (InvocationTargetException e) { |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | if (returnData != null) { |
||
| 158 | returnData.setModel("sin"); |
||
| 159 | returnData.setAverageProbability(1.0); |
||
| 160 | List<FileMatchMetaData> specialFileMatches = new ArrayList(); |
||
| 161 | specialFileMatches.add(returnData); |
||
| 162 | |||
| 163 | finalList = ListUtils.union(finalList, specialFileMatches); |
||
| 164 | } |
||
| 165 | log.debug(tokens[t]); |
||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | } |
||
| 170 | } catch (IOException e) { |
||
| 171 | log.info("Unable to read " + fich.getCanonicalPath() + ".Ignoring..."); |
||
| 172 | } |
||
| 173 | log.info("Finish processing " + fich.getCanonicalPath()); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } catch (IOException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | |
||
| 178 | SecurityException | SQLException | TikaException | SAXException e) { |
||
| 179 | log.error(e.toString()); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | final DecimalFormat decimalFormat = new DecimalFormat("#.##"); |
||
| 185 | |||
| 186 | log.info("List of suspects:"); |
||
| 187 | log.info(String.format("%40s %20s %20s %20s", "Directory*", "File*", "Probability*", "Model*")); |
||
| 188 | |||
| 189 | finalList = uniqueList(finalList); |
||
| 190 | |||
| 191 | Collections.sort(finalList, Comparator.comparing(FileMatchMetaData ::getFileName)); |
||
| 192 | |||
| 193 | for (final FileMatchMetaData data : finalList) { |
||
| 194 | String result = ""; |
||
| 195 | final String probability = decimalFormat.format(data.getAverageProbability()); |
||
| 196 | result = String.format("%40s %20s %20s %20s", |
||
| 197 | data.getDirectory(), |
||
| 198 | data.getFileName(), |
||
| 199 | probability, |
||
| 200 | data.getModel()); |
||
| 201 | log.info(result); |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | return Collections.unmodifiableList(fileMatches); |
||
| 207 | } |
||
| 337 | } |