Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
K
key_value-server
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
amir.yosef
key_value-server
Commits
f74874d4
Commit
f74874d4
authored
Aug 20, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding Validator class for arguments
parent
62cf7a72
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
18 deletions
+78
-18
CommandValidator.java
src/command/CommandValidator.java
+56
-0
GetCommand.java
src/command/GetCommand.java
+4
-1
SetCommand.java
src/command/SetCommand.java
+18
-17
No files found.
src/command/CommandValidator.java
0 → 100644
View file @
f74874d4
package
command
;
import
model.Command
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
public
class
CommandValidator
{
private
CommandValidator
()
{
}
private
static
final
class
InstanceHolder
{
private
static
final
CommandValidator
instance
=
new
CommandValidator
();
}
public
static
CommandValidator
getInstance
()
{
return
InstanceHolder
.
instance
;
}
public
boolean
validateSetCommand
(
List
<
String
>
args
)
{
if
(
args
==
null
||
args
.
isEmpty
())
{
return
false
;
}
if
(
args
.
size
()
<
2
)
{
return
false
;
}
if
(
args
.
size
()
>
2
)
{
Map
<
String
,
String
>
commandsMap
=
IntStream
.
iterate
(
2
,
i
->
i
<
args
.
size
(),
i
->
i
+
2
)
.
boxed
()
.
collect
(
Collectors
.
toMap
(
i
->
args
.
get
(
i
).
toLowerCase
(),
i
->
args
.
get
(
i
+
1
),
(
a
,
b
)
->
b
));
String
expiration
=
commandsMap
.
get
(
Command
.
PX
.
getValue
().
toLowerCase
());
if
(
expiration
!=
null
)
{
try
{
Long
.
parseLong
(
expiration
);
}
catch
(
NumberFormatException
e
)
{
return
false
;
}
}
}
return
true
;
}
public
boolean
validateGetCommand
(
List
<
String
>
args
)
{
return
args
!=
null
&&
args
.
size
()
==
1
;
}
}
src/command/GetCommand.java
View file @
f74874d4
...
@@ -9,6 +9,7 @@ import java.util.List;
...
@@ -9,6 +9,7 @@ import java.util.List;
public
class
GetCommand
implements
CommandExecutable
<
byte
[]>
{
public
class
GetCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
public
GetCommand
(
List
<
String
>
args
)
{
public
GetCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
this
.
args
=
args
;
...
@@ -16,7 +17,9 @@ public class GetCommand implements CommandExecutable<byte[]> {
...
@@ -16,7 +17,9 @@ public class GetCommand implements CommandExecutable<byte[]> {
@Override
@Override
public
byte
[]
execute
()
{
public
byte
[]
execute
()
{
if
(!
validator
.
validateGetCommand
(
args
))
{
return
"wrong args"
.
getBytes
();
}
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
if
(
response
==
null
||
response
.
isEmpty
()
||
response
.
isBlank
())
{
if
(
response
==
null
||
response
.
isEmpty
()
||
response
.
isBlank
())
{
return
(
"$-1\r\n"
.
getBytes
());
return
(
"$-1\r\n"
.
getBytes
());
...
...
src/command/SetCommand.java
View file @
f74874d4
...
@@ -11,6 +11,7 @@ import java.util.stream.IntStream;
...
@@ -11,6 +11,7 @@ import java.util.stream.IntStream;
public
class
SetCommand
implements
CommandExecutable
<
byte
[]>
{
public
class
SetCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
public
SetCommand
(
List
<
String
>
args
)
{
public
SetCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
this
.
args
=
args
;
...
@@ -18,7 +19,10 @@ public class SetCommand implements CommandExecutable<byte[]> {
...
@@ -18,7 +19,10 @@ public class SetCommand implements CommandExecutable<byte[]> {
@Override
@Override
public
byte
[]
execute
()
{
public
byte
[]
execute
()
{
if
(!
args
.
isEmpty
())
{
if
(!
validator
.
validateSetCommand
(
args
))
{
return
"wrong args"
.
getBytes
();
}
Map
<
String
,
String
>
commandsMap
=
IntStream
.
iterate
(
2
,
i
->
i
<
args
.
size
(),
i
->
i
+
2
)
Map
<
String
,
String
>
commandsMap
=
IntStream
.
iterate
(
2
,
i
->
i
<
args
.
size
(),
i
->
i
+
2
)
.
boxed
()
.
boxed
()
.
collect
(
Collectors
.
toMap
(
i
->
args
.
get
(
i
).
toLowerCase
(),
i
->
args
.
get
(
i
+
1
),
(
a
,
b
)
->
b
));
.
collect
(
Collectors
.
toMap
(
i
->
args
.
get
(
i
).
toLowerCase
(),
i
->
args
.
get
(
i
+
1
),
(
a
,
b
)
->
b
));
...
@@ -36,8 +40,5 @@ public class SetCommand implements CommandExecutable<byte[]> {
...
@@ -36,8 +40,5 @@ public class SetCommand implements CommandExecutable<byte[]> {
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
}
}
return
(
"+OK\r\n"
.
getBytes
());
return
(
"+OK\r\n"
.
getBytes
());
}
else
{
return
"wrong args"
.
getBytes
();
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment