-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for AttributeConverter
- Loading branch information
Showing
3 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/dieselpoint/norm/converter/StringToIntListConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.dieselpoint.norm.converter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
|
||
@Converter | ||
public class StringToIntListConverter implements AttributeConverter<List<Integer>, String> { | ||
|
||
@Override | ||
public String convertToDatabaseColumn(List<Integer> attribute) { | ||
if (attribute == null) { | ||
return null; | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
int len = attribute.size(); | ||
|
||
for (int i = 0; i < len; i++) { | ||
if (i > 0) { | ||
sb.append(','); | ||
} | ||
sb.append(attribute.get(i).intValue()); | ||
|
||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public List<Integer> convertToEntityAttribute(String in) { | ||
// deserialize string in the form "123,456" no spaces allowed | ||
List<Integer> list = new ArrayList<>(); | ||
if (in == null || in.length() == 0) { | ||
return list; | ||
} | ||
|
||
int value = 0; | ||
for (int i = 0; i < in.length(); i++) { | ||
int digit = in.charAt(i) - '0'; | ||
if (digit >= 0 && digit <= 9) { | ||
value = (value * 10) + digit; | ||
} else { | ||
// hit a comma | ||
list.add(value); | ||
value = 0; | ||
} | ||
} | ||
list.add(value); | ||
return list; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters