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
10acd291
Commit
10acd291
authored
Aug 13, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make CommandFactory singleton
parent
e58fd43b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
17 additions
and
46 deletions
+17
-46
InfoCommand.java
src/command/InfoCommand.java
+0
-35
CommandFactory.java
src/factory/CommandFactory.java
+12
-5
Factory.java
src/factory/Factory.java
+2
-1
ClientCommandHandler.java
src/handlers/ClientCommandHandler.java
+3
-5
No files found.
src/command/InfoCommand.java
deleted
100644 → 0
View file @
e58fd43b
package
command
;
import
model.Command
;
import
util.Response
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
public
class
InfoCommand
implements
CommandHandler
{
private
final
Configuration
configuration
=
Configuration
.
getInstance
();
@Override
public
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
{
System
.
out
.
println
(
configuration
.
getInfo
());
String
command
=
args
.
getFirst
();
if
(
command
.
equalsIgnoreCase
(
Command
.
REPLICATION
.
getValue
()))
{
Map
<
String
,
String
>
info
=
configuration
.
getInfo
();
String
response
=
info
.
entrySet
()
.
stream
()
.
map
(
data
->
data
.
getKey
()
+
":"
+
data
.
getValue
())
.
collect
(
Collectors
.
joining
());
try
{
os
.
write
(
Response
.
getResponse
(
response
));
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
\ No newline at end of file
src/factory/CommandFactory.java
View file @
10acd291
...
...
@@ -4,19 +4,26 @@ import command.*;
import
model.Command
;
public
class
CommandFactory
implements
Factory
{
private
final
Command
command
;
p
ublic
CommandFactory
(
Command
command
)
{
this
.
command
=
command
;
p
rivate
static
final
class
FactoryHolder
{
private
static
final
CommandFactory
factory
=
new
CommandFactory
()
;
}
public
CommandHandler
getInstance
()
{
public
CommandFactory
()
{
}
public
static
CommandFactory
getInstance
()
{
return
FactoryHolder
.
factory
;
}
@Override
public
CommandHandler
getCommand
(
Command
command
)
{
return
switch
(
command
)
{
case
PING
->
new
PingCommand
();
case
ECHO
->
new
EchoCommand
();
case
SET
->
new
SetCommand
();
case
GET
->
new
GetCommand
();
case
INFO
->
new
InfoCommand
();
// case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
...
...
src/factory/Factory.java
View file @
10acd291
...
...
@@ -2,7 +2,8 @@ package factory;
import
command.CommandHandler
;
import
model.Command
;
public
interface
Factory
{
CommandHandler
get
Instance
(
);
CommandHandler
get
Command
(
Command
command
);
}
src/handlers/ClientCommandHandler.java
View file @
10acd291
...
...
@@ -4,7 +4,6 @@ import command.CommandHandler;
import
command.CommandInvoker
;
import
factory.CommandFactory
;
import
model.Command
;
import
parser.CommandParser
;
import
util.CommandUtil
;
import
java.io.IOException
;
...
...
@@ -14,24 +13,23 @@ import java.util.List;
public
class
ClientCommandHandler
{
private
final
List
<
String
>
commands
;
private
final
OutputStream
os
;
private
final
CommandFactory
factory
=
CommandFactory
.
getInstance
();
public
ClientCommandHandler
(
List
<
String
>
commands
,
OutputStream
outputStream
)
{
this
.
commands
=
commands
;
this
.
os
=
outputStream
;
CommandParser
commandParser
=
new
CommandParser
();
}
public
boolean
execute
()
{
public
void
execute
()
{
Command
command
=
CommandUtil
.
getCommand
(
commands
.
getFirst
());
commands
.
removeFirst
();
CommandHandler
commandProcessor
=
new
CommandFactory
(
command
).
getInstance
(
);
CommandHandler
commandProcessor
=
factory
.
getCommand
(
command
);
try
{
CommandInvoker
.
invoke
(
commandProcessor
,
commands
,
os
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
false
;
}
...
...
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