Gridarta Editor
FilterConfigEncoder.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.filter;
21 
22 import java.util.Map.Entry;
23 import java.util.TreeMap;
24 import java.util.TreeSet;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
32 public class FilterConfigEncoder {
33 
37  @NotNull
38  private final StringBuilder sb = new StringBuilder();
39 
44  @NotNull
46 
47  @Override
48  public void visit(@NotNull final NamedFilterConfig filterConfig) {
49  final int position = sb.length();
50  sb.append("(");
51  boolean first = true;
52 
53  if (filterConfig.isEnabled()) {
54  sb.append("enabled");
55  first = false;
56  }
57 
58  if (filterConfig.isInverted()) {
59  if (first) {
60  first = false;
61  } else {
62  sb.append(",");
63  }
64  sb.append("inverted");
65  }
66 
67  for (final Entry<String, FilterConfig<?, ?>> entry : new TreeMap<>(filterConfig.getEntries()).entrySet()) {
68  final int savedPosition = sb.length();
69  final boolean savedFirst = first;
70  if (first) {
71  first = false;
72  } else {
73  sb.append(",");
74  }
75  FilterCodecUtils.encodeString(sb, entry.getKey());
76  sb.append("=");
77  final int position2 = sb.length();
78  encodeInternal(entry.getValue());
79  if (position2 == sb.length()) {
80  sb.setLength(savedPosition);
81  first = savedFirst;
82  }
83  }
84  if (first) {
85  sb.setLength(position);
86  } else {
87  sb.append(")");
88  }
89  }
90 
91  @Override
92  public void visit(@NotNull final NamedGameObjectMatcherFilterConfig filterConfig) {
93  final int position = sb.length();
94  sb.append("(");
95  boolean first = true;
96 
97  if (filterConfig.isEnabled()) {
98  sb.append("enabled");
99  first = false;
100  }
101 
102  for (final String key : new TreeSet<>(filterConfig.getProperties())) {
103  if (first) {
104  first = false;
105  } else {
106  sb.append(",");
107  }
109  sb.append("=");
110  final String value = filterConfig.getProperty(key);
111  if (value == null) {
112  throw new AssertionError("no value for key " + key);
113  }
114  FilterCodecUtils.encodeString(sb, value);
115  }
116  if (first) {
117  sb.setLength(position);
118  } else {
119  sb.append(")");
120  }
121  }
122 
123  };
124 
129  @Nullable
131 
137  @NotNull
138  public String encode(@NotNull final FilterConfig<?, ?> filterConfig) {
139  sb.setLength(0);
140  encodeInternal(filterConfig);
141  return sb.toString();
142  }
143 
148  private void encodeInternal(@NotNull final FilterConfig<?, ?> filterConfig) {
149  final FilterConfig<?, ?> prevFilterConfig = this.filterConfig;
150  try {
151  this.filterConfig = filterConfig;
152  filterConfig.accept(visitor);
153  } finally {
154  this.filterConfig = prevFilterConfig;
155  }
156  }
157 
158 }
static void encodeString(@NotNull final StringBuilder sb, @NotNull final String string)
Encodes a string configuration to a string builder.
void encodeInternal(@NotNull final FilterConfig<?, ?> filterConfig)
Encodes a file configuration to sb.
FilterConfig<?, ?> filterConfig
The FilterConfig instance being encoded.
Utility class for codec related functions.
Converts FilterConfig into string representation.
String encode(@NotNull final FilterConfig<?, ?> filterConfig)
Export the filter configuration settings.
void accept(@NotNull FilterConfigVisitor visitor)
Visits the appropriate.
Interface for visitors of filter configs.
final FilterConfigVisitor visitor
The FilterConfigVisitor for converting filterConfig instances to string representation.
final StringBuilder sb
The string representation.
Filter configuration of NamedGameObjectMatcherFilter instances.