Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csv_io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
csv_io
Commits
17639937
Commit
17639937
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Converts between delimited strings and lists (or sets) of strings
Closes
#4
parent
5f07781f
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+54
-2
54 additions, 2 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java
+131
-0
131 additions, 0 deletions
src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java
with
185 additions
and
2 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+
54
−
2
View file @
17639937
...
@@ -61,8 +61,6 @@ public class CSVReaderWriter {
...
@@ -61,8 +61,6 @@ public class CSVReaderWriter {
return
destination
;
return
destination
;
}
}
// I'd like to replace this with something that uses openCSV, but it works, and openCSV doesn't seem to have
// writers that take map<string,string>
public
static
boolean
writeCSV
(
String
filename
,
Collection
<
Map
<
String
,
String
>>
data
)
{
public
static
boolean
writeCSV
(
String
filename
,
Collection
<
Map
<
String
,
String
>>
data
)
{
boolean
wroteFile
=
true
;
boolean
wroteFile
=
true
;
ClassLoader
classLoader
=
CSVReaderWriter
.
class
.
getClassLoader
();
ClassLoader
classLoader
=
CSVReaderWriter
.
class
.
getClassLoader
();
...
@@ -114,6 +112,60 @@ public class CSVReaderWriter {
...
@@ -114,6 +112,60 @@ public class CSVReaderWriter {
return
fieldArray
;
return
fieldArray
;
}
}
public
static
String
createDelimitedCollection
(
Collection
<
String
>
collection
,
String
delimiter
)
{
if
(
delimiter
.
equals
(
"\""
))
{
throw
new
IllegalArgumentException
(
"The double-quote character \" cannot be a delimiter."
);
}
StringBuilder
delimitedCollection
=
new
StringBuilder
();
int
tokensRemaining
=
collection
.
size
();
for
(
String
datum
:
collection
)
{
if
(
datum
.
contains
(
delimiter
))
{
delimitedCollection
.
append
(
"\""
).
append
(
datum
).
append
(
"\""
);
}
else
{
delimitedCollection
.
append
(
datum
);
}
if
(--
tokensRemaining
>
0
)
{
delimitedCollection
.
append
(
delimiter
);
}
}
return
delimitedCollection
.
toString
();
}
public
static
List
<
String
>
createFreeList
(
String
delimitedCollection
,
String
delimiter
)
{
return
(
List
<
String
>)
createFreeCollection
(
delimitedCollection
,
delimiter
,
new
LinkedList
<>());
}
public
static
Set
<
String
>
createFreeSet
(
String
delimitedCollection
,
String
delimiter
)
{
return
(
Set
<
String
>)
createFreeCollection
(
delimitedCollection
,
delimiter
,
new
HashSet
<>());
}
static
Collection
<
String
>
createFreeCollection
(
String
delimitedCollection
,
String
delimiter
,
Collection
<
String
>
collection
)
{
// NOTE: if there are 2n+1 quotes, where n>0, then the delimitedCollection is ambiguous
// In practice, this code treats the final quote as the floating quote (but do not guarantee it will do so)
// If this isn't what you want, then don't pass an ambiguous delimitedCollection
if
(
delimiter
.
equals
(
"\""
))
{
throw
new
IllegalArgumentException
(
"The double-quote character \" cannot be a delimiter."
);
}
List
<
String
>
preliminaryCollection
=
new
LinkedList
<>();
if
(!
Objects
.
equals
(
delimitedCollection
,
""
))
{
String
[]
splitOnQuotedSegments
=
delimitedCollection
.
split
(
" (?=\")|(?<=\") "
);
for
(
String
segment
:
splitOnQuotedSegments
)
{
String
trimmedSegment
=
segment
.
trim
();
// use .strip() if we move to Java 11
if
(!
trimmedSegment
.
equals
(
""
))
{
if
(
trimmedSegment
.
startsWith
(
"\""
)
&&
trimmedSegment
.
endsWith
(
"\""
))
{
preliminaryCollection
.
add
(
trimmedSegment
.
substring
(
1
,
trimmedSegment
.
length
()
-
1
));
}
else
{
String
[]
splitOnDelimiter
=
trimmedSegment
.
split
(
delimiter
);
preliminaryCollection
.
addAll
(
Arrays
.
asList
(
splitOnDelimiter
));
}
}
}
}
collection
.
addAll
(
preliminaryCollection
);
return
collection
;
}
/* LEGACY METHODS */
/* LEGACY METHODS */
public
static
Set
<
Map
<
String
,
String
>>
readCSV
(
String
filename
)
{
public
static
Set
<
Map
<
String
,
String
>>
readCSV
(
String
filename
)
{
return
readCSVasSet
(
filename
);
return
readCSVasSet
(
filename
);
...
...
This diff is collapsed.
Click to expand it.
src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java
+
131
−
0
View file @
17639937
...
@@ -217,4 +217,135 @@ public class CSVReaderWriterTest {
...
@@ -217,4 +217,135 @@ public class CSVReaderWriterTest {
assertTrue
(
expectedCSVStringOption1
.
equals
(
output
)
||
expectedCSVStringOption2
.
equals
(
output
));
assertTrue
(
expectedCSVStringOption1
.
equals
(
output
)
||
expectedCSVStringOption2
.
equals
(
output
));
assertThat
(
output
,
either
(
is
(
expectedCSVStringOption1
)).
or
(
is
(
expectedCSVStringOption2
)));
assertThat
(
output
,
either
(
is
(
expectedCSVStringOption1
)).
or
(
is
(
expectedCSVStringOption2
)));
}
}
@Test
public
void
testCreateDelimitedCollection
()
{
// Input
String
[]
inputArray
=
{
"foo"
,
"bar"
,
"baz plugh"
};
List
<
String
>
inputList
=
Arrays
.
asList
(
inputArray
);
// Oracle
String
expectedOutput
=
"foo bar \"baz plugh\""
;
// Output
String
actualOutput
=
CSVReaderWriter
.
createDelimitedCollection
(
inputList
,
" "
);
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateDelimitedCollectionFromTrivialList
()
{
// Input
List
<
String
>
input
=
new
ArrayList
<>(
1
);
input
.
add
(
"foo"
);
// Oracle
String
expectedOutput
=
"foo"
;
// Output
String
actualOutput
=
CSVReaderWriter
.
createDelimitedCollection
(
input
,
" "
);
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateDelimitedCollectionFromVacuousList
()
{
// Input
List
<
String
>
input
=
new
ArrayList
<>();
// Oracle
String
expectedOutput
=
""
;
// Output
String
actualOutput
=
CSVReaderWriter
.
createDelimitedCollection
(
input
,
" "
);
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateFreeCollectionSimpleCase
()
{
// Input
String
input
=
"one two three four"
;
// Oracle
String
[]
expectedOutputArray
=
{
"one"
,
"two"
,
"three"
,
"four"
};
Collection
<
String
>
expectedOutput
=
Arrays
.
asList
(
expectedOutputArray
);
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateFreeCollectionTrivialCase
()
{
// Input
String
input
=
"one"
;
// Oracle
String
[]
expectedOutputArray
=
{
"one"
};
Collection
<
String
>
expectedOutput
=
Arrays
.
asList
(
expectedOutputArray
);
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateFreeCollectionEmptyString
()
{
// Input
String
input
=
""
;
// Oracle
Collection
<
String
>
expectedOutput
=
new
ArrayList
<>();
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
assertNotNull
(
actualOutput
);
assertEquals
(
0
,
actualOutput
.
size
());
}
@Test
public
void
testCreateFreeCollectionNullString
()
{
// Input
String
input
=
""
;
// Oracle
Collection
<
String
>
expectedOutput
=
new
ArrayList
<>();
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
assertNotNull
(
actualOutput
);
assertEquals
(
0
,
actualOutput
.
size
());
}
@Test
public
void
testCreateFreeCollectionQuotedSubstring
()
{
// Input
String
input
=
"one \"two\" three four \"five\" six"
;
// Oracle
String
[]
expectedOutputArray
=
{
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
};
Collection
<
String
>
expectedOutput
=
Arrays
.
asList
(
expectedOutputArray
);
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateFreeCollectionQuotedDelimiter
()
{
// Input
String
input
=
"one \"two three\" \"four five\" six"
;
// Oracle
String
[]
expectedOutputArray
=
{
"one"
,
"two three"
,
"four five"
,
"six"
};
Collection
<
String
>
expectedOutput
=
Arrays
.
asList
(
expectedOutputArray
);
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
@Test
public
void
testCreateFreeCollectionSingleQuote
()
{
// Input
String
input
=
"one two three\" four five six"
;
// Oracle
String
[]
expectedOutputArray
=
{
"one"
,
"two"
,
"three\""
,
"four"
,
"five"
,
"six"
};
Collection
<
String
>
expectedOutput
=
Arrays
.
asList
(
expectedOutputArray
);
// Output
Collection
<
String
>
actualOutput
=
CSVReaderWriter
.
createFreeCollection
(
input
,
" "
,
new
ArrayList
<>());
// Compare
assertEquals
(
expectedOutput
,
actualOutput
);
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment