Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.SchemaMapper;
import org.jsonschema2pojo.exception.ClassAlreadyExistsException;
import org.jsonschema2pojo.util.MakeUniqueClassName;
import org.jsonschema2pojo.util.NameHelper;
import org.jsonschema2pojo.util.ParcelableHelper;
import org.jsonschema2pojo.util.TypeUtil;
Expand Down Expand Up @@ -456,7 +457,7 @@ private String makeUnique(String className, JPackage _package) {
_package.remove(_class);
return className;
} catch (JClassAlreadyExistsException e) {
return makeUnique(className + "_", _package);
return makeUnique(MakeUniqueClassName.makeUnique(className), _package);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.util;

import org.apache.commons.lang3.StringUtils;

public class MakeUniqueClassName {

/**
* When the class name is not unique we will use two underscore '__' and a digit representing the number of time
* this class was found
*/
public static String makeUnique(String className) {
String returnClassName = className;
// Last character is a digit and there is 2 underscore in the className
if (Character.isDigit(className.charAt(className.length() - 1)) && StringUtils.contains(className, "__")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check is a little too flexible. I think we should replace this with a regex like .+\_\_(\d+)$, and use the capture group to pull out the current number.

// get the number
String strNumber = className.substring(StringUtils.indexOf(className, "__") + 2);
Integer number = Integer.parseInt(strNumber);
// replace the number in the string with +1
number = number + 1;
returnClassName = returnClassName.substring(0, StringUtils.indexOf(returnClassName, "__") + 2) + number;
} else {
returnClassName = returnClassName + "__1";
}
return returnClassName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.util;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class MakeUniqueClassNameTest {

@Test
public void testClassNameStrategy() {
assertThat(MakeUniqueClassName.makeUnique("NodeMode"), equalTo("NodeMode__1"));
assertThat(MakeUniqueClassName.makeUnique("NodeMode__5"), equalTo("NodeMode__6"));
assertThat(MakeUniqueClassName.makeUnique("NodeMode__10"), equalTo("NodeMode__11"));
assertThat(MakeUniqueClassName.makeUnique("NodeMode__100"), equalTo("NodeMode__101"));
}

}